### Help with indexes and sorting arrays

I need help with writing a function called swap that sorts the elements of an array in order from highest to lowest values where they descend and ascend. A particular thing about this function is that swap does NOT re-arrange elements in the array so it just uses a second array of indexes for the elements in the original array and then swap sorts the second array based on the values in the original array. A sorted version of the original array can then be produced with these sorted indexes. You can declare and initialize the original array without any user input. For example,
int arr[10] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 };
Header of the function swap must be as shown below:
void swap(int array[],int swapedIndexes [], int size, char mode)
When mode is 'a', the function sorts the array in the ascending order, and when mode is 'd', the function sorts it in the descending order.
int swappedIndexes[5];
swap(array,swappedIndeâ€¦ 5, 'a');

Example:
int array[5]={2, 4,0,-5,3};
int sortedIndexes[5];
sortMe(array,sortedIndexes, 5, 'a');

=====================================

If your function re-arranges the elements in the array array, it is wrong.
If you initialize the array sortedIndexes in the function main, it is wrong
The function must sort arrays of any sizes; otherwise, its wrong.
If you change the header of the function swap in any way, also wrong.

here is another example of how it should be with the indexes:
If I have int number[]={32,92,11,72}

Example :
Before
Numbers: 32 91 11 72
Indexes: 0 1 2 3
After
Numbers: 11 32 72 91
Indexes: 2 0 3 1

So...Can somebody please show me how to print out the indexes of the array for this?? Can you please provide a code or something? I'm not sure how to use the second array of int swappedindexes; ??? This is where I am having the most problems.

So far I have a simple swap code.
 ``123456789101112131415161718192021`` `````` #include using namespace std; int main() { int a[10], swap; for ( int i = 0; i < 10; ++i ) cin >> a[i]; for ( int i = 0; i < 10; ++i ) { for ( int j = i; j < 10; ++j ){ if( a[j] > a[i] ){ swap = a[i]; a[i] = a[j]; a[j] = swap; } } } for ( int i = 0; i < 10; ++i ) cout << a[i] << " "; return 0; }``````
Last edited on
 So...Can somebody please show me how to print out the indexes of the array for this?? Can you please provide a code or something? I'm not sure how to use the second array of int swappedindexes; ???

This should give you some idea.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455`` ``````#include #include #include const unsigned lineWidth = 80 ; const unsigned columnWidth = 10 ; const unsigned columns = lineWidth / columnWidth ; void print( const int* t, unsigned size ) { for ( unsigned i=0; i columns ) std::cout << '\n' ; } std::cout << '\n' ; } void print( const int* t, const unsigned* indices, unsigned size ) { for ( unsigned i=0; i columns ) std::cout << '\n' ; } std::cout << '\n' ; } int main() { const unsigned sz = 10 ; int arr[sz] = {1, 5, 22, 14, 6, -5, 7, 9, 12, 15 } ; unsigned arr_index[sz] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; print(arr, sz) ; print(arr, arr_index, sz) ; std::cout << '\n' ; std::sort(std::begin(arr_index), std::end(arr_index), [&](unsigned i, unsigned j){ return arr[i] < arr[j]; }) ; print(arr, sz) ; print(arr, arr_index, sz) ; std::cout << '\n' ; std::sort(std::begin(arr_index), std::end(arr_index), [&](unsigned i, unsigned j){ return arr[j] < arr[i]; }) ; print(arr,sz) ; print(arr, arr_index, sz) ; }``````

http://ideone.com/sJb4bD
Topic archived. No new replies allowed.