I wrote this algorithm a while ago, and decided to revisit it and polish it up. It basically works by comparing each number with all the other numbers once, so comparing the first number to all except itself, the second to all numbers after itself etc, to avoid double comparison. I'm sure it has an official name, but have no idea what that would be as I put it together myself.
#include <stdio.h> #include<stdlib.h>///only needed for example code #include<time.h>///only needed for example code int number,scan,totalnumbers; int main() { totalnumbers=10;///just for the example. functional code would need an input system. int array[totalnumbers][3];//one for input, second tallying values, third for transfer output. ///for the example choose random numbers : srand(time(NULL)); for (scan=0;scan<totalnumbers;scan++) { array[scan][0]=rand()%200; } //fill [2] with zeros, as will be incremented to work out location. for (scan=0;scan<totalnumbers;scan++)//formatting the whole array { array[scan][1]=0; } for(number=0;number<totalnumbers;number++) { for(scan=number+1;scan<totalnumbers;scan++)///compare to all numbers, excluding itself { if(array[number][0]>array[scan][0]) { array[number][1]++; } else { array [scan][1]++;///this includes when they are equal, and can be adjusted if you want earlier or later instanses of the same number to be displayed first } } } ///now we put each number into its place in the third 'array' for(number=0;number<totalnumbers;number++) { array[array[number][1]][2]=array[number][0];///the position in array[number] [1] tells us wher the one in array[number][0]should go, and we place it there in [2] } printf("Results\n"); for(number=0;number<totalnumbers;number++) { printf("%d\n",array[number][2]); } }