how to sort (indexes) of an array to get the original array sorted from the smallest to the biggest value by using those indexes

hussein albirouti :

for example I have this array

int[] a = {6,10,16,11,7,12,3,9,8,5};

i want to sort its indexes like this

[6,9,0,4,8,7,1,3,5,2]

so I can use indexes to sort a from the smallest to the biggest value. in my code I got instead this

[6, 9, 4, 8, 7, 4, 5, 6, 6, 6] 

this is my code

int[] a = {6,10,16,11,7,12,3,9,8,5};
int[] indeks = indekssortering(a);
System.out.println(Arrays.toString(indeks));

public static int[] indekssortering(int[] a){
    int[] indeks = new int[a.length];
    int m = 0;
    boolean finnes = false;
    boolean nyVerdi = false;
    int n = 0;
    for (int j = 0; j < a.length; j++) {
        for (int i = m+1; i < a.length ; i++) {
            if(a[m] > a[i]){
                for (int k = 0; k < j; k++) {
                    if(indeks[k] == i) finnes = true; //check if the same position is saved before
                }
                if(!finnes){ // if not so its the next minimum value
                    m = i;
                } else {
                    nyVerdi = true; // if didnt find match then the value used to compare is the next minimum
                }
            }
            finnes = false;
        }
        indeks[j] = m;
        if(nyVerdi) n=n+1;
        nyVerdi = false;
        m=0+n;
    }
    return indeks;
}

I need help to make this code work or to find a better idea than this.

what I tried to do is. compare all values with the first values, get the smallest and save the position into the array (indeks). before save it, I made for loop to check if this position is been added before. and if there is no value bigger than the value used to compare that means its the next small value. i got some of them right and others wrong. I believe that I need to change this idea and find a better solution.

Maxim :

Here a classical bubble sort algorithm implementation modified to sort indexes

What you are looking for is actually any sorting algorithm which sorts int [] array. It's endless list of implementations all over the Internet. And then just change the comparison to array[result[i]] and swap values in result not in array itseft.

static int[] sort(int[] array) {
    final int size = array.length;

    final int[] result = new int[size];
    for (int i = 0; i < size; i++)
        result[i] = i;

    boolean sorted;
    do {
        sorted = true;
        int bubble = result[0];
        for (int i = 0; i < size - 1; i++) {
            if (array[bubble] > array[result[i + 1]]) {
                result[i] = result[i + 1];
                result[i + 1] = bubble;
                sorted = false;
            } else {
                bubble = result[i + 1];
            }
        }
    } while (!sorted);

    return result;
}

result arrays for your input data is [6, 9, 0, 4, 8, 7, 1, 3, 5, 2]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=116079&siteId=1