selection_sort.c |
| #include <stdio.h> #include <stdlib.h> #include <time.h> #define ARRAY_MAX 10000 int smallest(int *a, int i, int n); void selection_sort(int *a, int n); /*find smallest value function */ int smallest(int *a, int i, int n){ int smallest; smallest=i; for(; i<n; i++){ if(a[smallest]>a[i]) smallest=i; } return smallest; } /* sort array function */ void selection_sort(int *a, int n){ int i; int j=0; int temp=0; for(i=0; i<n-1; i++){ j=smallest(a, i, n); /* swap */ temp=a[i]; a[i]=a[j]; a[j]=temp; } } main(){ int my_array[ARRAY_MAX]; int counter = 0; int i; clock_t c,d; while(1==scanf("%d", &my_array[counter]) && counter<ARRAY_MAX){ counter++; } c=clock(); selection_sort(my_array, counter); d=clock(); for(i=0;i<counter; i++) printf("%d\n", my_array[i]); fprintf(stderr,"%d\t%f\n",counter, (d-c)/(double)CLOCKS_PER_SEC); return EXIT_SUCCESS; } |
James Little |