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.