Well I tried to look for any posts on ranking arrays, but all I ever really got were stuff about Google's ranking system. Anyway, I have two arrays. One is arr[] which is the original array, and rank[] is the indexing array. So if I were to print arr[rank[0]], it would give me the smallest value of arr[]. Without further ado, here is the code!
#include <stdio.h>
int main(void)
{
// Local Declarations
int arr[5]={2,9,5,1,3};
int rank[5]={0,1,2,3,4};
int i; int j;
int smallest;
int temp;
// Statements
for(i=0;i<4;i++)
{
smallest = i;
for(j=i+1;j<=4;j++)
if(arr[rank[smallest]] > arr[rank[j]])
smallest = j;
temp = rank[i];
rank[i] = smallest;
rank[smallest] = temp;
printf("\n");
for(i=0;i<5;i++)
printf("%3d", rank[i]);
printf("\n");
}
for(i=0;i<5;i++)
printf("%3d", arr[i]);
printf("\n");
return 0;
} // main
/* Execution Results
3 1 2 0 4
2 9 5 1 3
*/
For some reason, the for loop for the ranking sort just goes through only once. Otherwise it would print the ranking array 3 more times I believe. Any help would be greatly appreciated. Thanks in advance!