i want to store the original index numbers of array elements before for sorting in ascending order.
assume if the elements in array are
x[]= {20,40,70,80,50,30};
i want another array to return with index number of the sorted form like
sorted[] = {0,5,1,4,2,3}
i tried many times. but can't figure out how to get the index numbers. everytime the index number gets changed after the swap.
Note:
- i can't use comparotor,map,Arrays.sort or anything like that. i have to use loop only.
i already searched all the websites. i cant find any specific answer. cause all of them uses comparator map or something that i am not allowed to use at the moment.
public static int[] sort(int[] numbers) { int[] sorted = new int[numbers.length]; int temp; int[] index= new int[numbers.length]; for (int i = 0; i < numbers.length; i++) { for (int j = i+1; j < numbers.length; j++) { if (numbers[i] > numbers[j]) { temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; index[i]= j; } } } return index; }