0 Members and 4 Guests are viewing this topic.
If you don't need a faster algorithm use that. However, looking at the wikipedia page turns up division by a floating point number, which is likely to be slow.
#include <stdio.h>int main();int *sort(int array[], int array_length);int check_if_num_in_array(int num, int array[], int array_length);int main(){ /* Tests sort() with a sample case */ int num_array[] = {5, 10, 12, 1, 4, 1000}; int num_array_length = sizeof(num_array) / sizeof(int); int b; for (b = 0; b < num_array_length; b++) { printf( "%d\n", sort(num_array, num_array_length)[b] ); } return 0;}int *sort(int array[], int array_length){ /* Sorts array[] of length array_length */ int *ptrArray; int sorted[array_length]; ptrArray = sorted; int i; //Big loop int u; //Small loop int maximum = 0; for (i = 0; i < array_length; i++) { for (u = 0; u < array_length; u++) { /* If bigger than current maximum and not in sorted[] already */ if ( array[u] > maximum && check_if_num_in_array(array[u], sorted, i) ==0) { maximum = array[u]; } } sorted[i] = maximum; maximum = 0; } return ptrArray;}int check_if_num_in_array(int num, int array[], int array_length){ /* Checks if number num is in array[] of length array_length */ int v; for (v = 0; v < array_length; v++) { if (array[v] == num) { return 1; } } return 0;}