0

I have an array:

int[] array = {1,2,3,4,5,6,7,8,9,10};

I want to pick a starting point for the sorting. If I choose 6 the output should be

(6,7,8,9,10,1,2,3,4,5)
Vince
  • 47
  • 7
  • 3
    Can you give another example input/output for your question, for example what if the input is: `int[] array = {10,9,8,7,6,5,4,3,2,1};` and the starting point is `6` ? – driangle Feb 20 '12 at 16:19
  • 5
    Can you define the sort a bit more? What if the array is `{3,7,2,8,4,6,1,0,5,9}` and the starting point is 4? What should the result be? – Jonathan M Feb 20 '12 at 16:20
  • sort it beginning from the specified position and append the rest. Is it what you try to tell? – Juvanis Feb 20 '12 at 16:21
  • @VincentPrayco, can you give the answer to my example? Or at least tell whose example you're giving the answer to. – Jonathan M Feb 20 '12 at 16:22
  • What sorting? It looks like circular reading or the example is bad. – Arnaud Gourlay Feb 20 '12 at 16:22

2 Answers2

5

I would go with two stages:

  1. Sort it for index one (normally)
  2. Rotate it to the desired index
MByD
  • 135,866
  • 28
  • 264
  • 277
0

If you are trying to sort based on array index position that you specified in the code, you can use the below code.

import java.util.Arrays;

public class IndexBasedSort {

public static int[] getFirstIndexedArray(int[] originalArray, int arrayIndex) {
    int[] firstIndexedArray = new int[arrayIndex];
    for(int i=0;i<arrayIndex;i++){
        firstIndexedArray[i] = originalArray[i];
    }
    return firstIndexedArray;
}

public static int[] getNextIndexedArray(int[] originalArray, int arrayIndex) {
    int[] nextIndexedArray = new int[(originalArray.length - arrayIndex)];
    for(int i=0;i<nextIndexedArray.length;i++) {
        nextIndexedArray[i] = originalArray[i+arrayIndex];
    }
    return nextIndexedArray;
}

public static int[] sortArrayElements(int[] arrayElements) {
    Arrays.sort(arrayElements);
    return arrayElements;
}

public static int[] joinArrayElements(int[] firstArray, int[] nextArray) {
    int[] joinedArray = new int[(firstArray.length + nextArray.length)];
    for(int i=0;i<firstArray.length;i++) {
        joinedArray[i] = firstArray[i];
    }
    for(int i=0;i<nextArray.length;i++) {
        joinedArray[i+firstArray.length] = nextArray[i];
    }
    return joinedArray;
}

public static void printArrayElements(int[] arrayElements) {
    System.out.print("Sorted Array: ");
    for(int i=0;i<arrayElements.length;i++) {
        System.out.print(arrayElements[i]+", ");
    }
}

public static void run(int[] originalArray, int arrayIndex){
    printArrayElements(joinArrayElements(sortArrayElements(getFirstIndexedArray
            (originalArray, arrayIndex)), sortArrayElements(getNextIndexedArray(
                    originalArray, arrayIndex))));
}

public static void main(String[] args) {
    int[] originalArray = {10,9,8,7,6,5,4,3,2,1};
    int arrayIndex = 6;
    run(originalArray, arrayIndex-1);
}

}

1218985
  • 7,531
  • 2
  • 25
  • 31