Home > OS >  C Array Sorting With Indices
C Array Sorting With Indices

Time:08-29

I need to sort a float arr[256][16] array while retaining the original indices of the elements. So, for instance, if the indices of x = [7,12,4] are 0, 1, 2, I would like to sort this array as [4,7,12] and "remember" the indices as 2,0,1. Also, the container must be a standard array, even if its easier to use structures to store the values and indices of each object. So far, I've done it using simple Bubblesort and storing the indices where elements get swapped,but it's giving me incorrect results. Any help would be great, thanks.

CodePudding user response:

What you could do is, to sort the indices, based on the values in the array. And leave the array as is. And later access the elements through the index array.

So, you compare the real float values, if one is less than the other, but then, exhange the index values in the index array.

This could look like the below

#include <iostream>
#include <algorithm>

const int NumberOfRows = 256;
const int NumberOfColumns = 16;

float arr[NumberOfRows][NumberOfColumns];
unsigned char index[NumberOfRows][NumberOfColumns];

int main() {

    // Fill index array (and create test data)
    for (int row = 0; row < NumberOfRows;   row)
        for (int col = 0; col < NumberOfColumns;   col) {
            index[row][col] = col;
            arr[row][col] = row * NumberOfColumns  (NumberOfColumns - col-1);
        }

    // Sort all 256 rows. But sort the indices only, not the arr values
    for (int row = 0; row < NumberOfRows;   row)
        std::sort(index[row], index[row]   NumberOfColumns, [&](const int i1, const int i2) {return arr[row][i1] < arr[row][i2]; });

    // Show debug output
    for (int row = 0; row < NumberOfRows;   row) {
        std::cout << "\nRow: " << row << "  -->  ";
        for (int col = 0; col < NumberOfColumns;   col)
            std::cout << arr[row][index[row][col]] << ' ';
    }
}

If you wan to sort both the values and the indices then you could write:

#include <iostream>
#include <algorithm>

const int NumberOfRows = 256;
const int NumberOfColumns = 16;

float arr[NumberOfRows][NumberOfColumns];
unsigned char index[NumberOfRows][NumberOfColumns];

int main() {

    // Fill index array (and create test data)
    for (int row = 0; row < NumberOfRows;   row)
        for (int col = 0; col < NumberOfColumns;   col) {
            index[row][col] = col;
            arr[row][col] = row * NumberOfColumns   (NumberOfColumns - col - 1);
        }

    // Sort all 256 rows. 
    for (int row = 0; row < NumberOfRows;   row) {
        std::sort(index[row], index[row]   NumberOfColumns, [&](const int i1, const int i2) {return arr[row][i1] < arr[row][i2]; });
        std::sort(arr[row], arr[row]   NumberOfColumns);
    }
    // Show debug output
    for (int row = 0; row < NumberOfRows;   row) {
        std::cout << "\nRow: " << row << "  -->  ";
        for (int col = 0; col < NumberOfColumns;   col)
            std::cout << arr[row][col] << ' ';
    }
}

Now you have the arr values sorted and the index values sorted.

  • Related