/***************** * sort_selection.c * * $ gcc sort_selection.c -o sort_selection * $ ./sort_selection * * -- selection sort -- * * test case : [3, 20, 5, 7, 8, 1, 10, 2, ] * after sort : [1, 2, 3, 5, 7, 8, 10, 20, ] * * n seconds * ------------ ------------ * 100 0.00001600 * 300 0.00011200 * 1000 0.00084500 * 3000 0.00599900 * 10000 0.06508500 * 30000 0.58306300 * 100000 6.56642100 * 300000 59.84103900 * * This algorithm is a loop i from 1..n within a loop j from i..n, * and so is O(n**2) in time complexity. * * The loop invariant, which verifies that the algorithm is correct, * is that in the outer loop, the numbers at positions 0 through i * will be in sorted, i.e. smaller than all the others and in their * correct places. * * TODO : * test each function individually, * with cases with known verifiable answers. * * Jim Mahoney | cs.bennington.college | MIT License | March 2021 *****************/ #include #include #include #include int do_assertion; // 1 => true ; 0 => false double get_secs(){ // return time since program launch clock_t ticks = clock(); double seconds = (double)ticks / (double)CLOCKS_PER_SEC; //printf(" ticks since epoch = %lu ; ", ticks); // debug //printf(" ticks per second = %d \n", CLOCKS_PER_SEC); // . //printf(" seconds = %.6f \n", seconds); // . return seconds; } int* get_randoms(int n){ // return array of n random numbers, randoms[0] to randoms[n-1]. int i; int* randoms = malloc(n * sizeof(int)); for (i=0; i numbers[j]){ printf(" OOPS - loop invariant violated !! \n"); } } } int find_min_index(int i, int n, int* numbers){ // find and return the index of the smallest of numbers[i] to numbers[n-1] int j; int i_smallest = i; // the first, at "i", is the smallest so far. for (j=i+1; j