Sort Array and Return Original Indexes of Sorted Array

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

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]

Find the original index of sorted array

You cannot unless you save some additinal data. You could e.g. put both index and value inside a class and sort by value, or you could define a array containing the indices and sort that array instead:

Integer[] indices = new Integer[item.length];
for (int i = 0; i < indices.length; i++) {
indices[i] = i;
}

Arrays.sort(indices, new Comparator<Integer>() {

public int compare(Integer i1, Integer i2) {
return item[i1].compareTo(item[i2]);
}
});

Sorted values:

item[indices[0]]
item[indices[1]]
item[indices[2]]
item[indices[3]]

Original indices

indices[0]
indices[1]
indices[2]
indices[3]

How to store the original indices of array after sorting the array in ascending ordering

The issue is that you are sorting the original array_dist when you shouldn't be sorting this at all.

You should only sort the index_array based on the comparison of the array_dist values :

// Compare values at `array_dist`, using the index array 
if (array_dist[index_array[i]] > array_dist[index_array[j]])
{
// the indices need to be swapped
x = index_arr[i];
index_arr[i] = index_arr[j];
index_arr[j] = x;
}

Once you have this, then to print out the array_dist in sorted order:

for (int i = 0; i < 4344; ++i)
{
cout << index_arr[i] << " : "
<< array_dist[index_arr[i]] << endl;
}

You use the index_array as a subscript within the original array.

Sorting an array without losing the original index in C

Since this is C and not C++, you'll need to create your own sort program since I think qsort() can be O(n^2) worst case.

Since you will be using your own sort program, you could just sort the array and the indices at the same time, treating them as pairs of elements.

If there was an existing O(n log(n)) sort function that you could call, you could create an array of indices 0 to length-1, and sort the array of indices according to the array, then copy the array to a temp array according to the sorted indices, then copy the temp array back to the original array.

void rem_sort(int array[], int index[], int size){
/* temp array */
int * tmpa = malloc(size * sizeof(array[0]));
int i;
/* generate indices to array */
for(i = 0; i < size; i++)
index[i] = i;
/* sort index according to array in O(n log(n)) time */
sort(array, index, size);
/* tmpa[] = sorted array[] */
for(i = 0; i < size; i++)
tmpa[i] = array[index[i]];
/* copy tmpa to array */
for(i = 0; i < size; i++)
array[i] = tmpa[i];
free(tmpa);
return;
}

Sorting Array but keeping Original Index

Build an array of objects that carry both the name and index, then use a custom comparison function for the sort. E.g.

var stationInfo = [];
for (var i=0; i<indexSelected.length; i++) {
var idx = indexSelected[i];
stationInfo.push({name: weather[idx]["Site Name"], idx: idx);
}

stationInfo.sort(function(a, b) {
// a & b are the array items (info objects) created above, so make 'em
// point at the `name` property we want to sort by
a = a.name;
b = b.name;
// ... then return -1/0/1 to reflect relative ordering
return a < b ? -1 : (a > b ? 1 : 0);
})

// output a name + index pair
console.log(stationInfo[0].name, stationInfo[0].idx);


Related Topics



Leave a reply



Submit