fork download
  1. #include<stdio.h>
  2. #include<time.h>
  3. #include<sys/time.h>
  4. #include<stdlib.h>
  5.  
  6. int i, j;
  7.  
  8. void generate_random(int arr[], int n) {
  9. srand(time(0));
  10. for (i = 0; i < n; i++)
  11. arr[i] = rand() % 100000;
  12. }
  13.  
  14. void selectionSort(int arr[], int n) {
  15. for (i = 0; i < n - 1; i++) {
  16. int min_idx = i;
  17. for (j = i + 1; j < n; j++)
  18. if (arr[j] < arr[min_idx])
  19. min_idx = j;
  20. int temp = arr[i];
  21. arr[i] = arr[min_idx];
  22. arr[min_idx] = temp;
  23. }
  24. }
  25.  
  26. int main() {
  27. int arr[100000], i;
  28. struct timeval t;
  29. double start, end;
  30.  
  31. for (i = 10000; i <= 80000; i = i * 2) {
  32. generate_random(arr, i);
  33. gettimeofday(&t, NULL);
  34. start = t.tv_sec + (t.tv_usec / 1000000.0);
  35. selectionSort(arr, i);
  36. gettimeofday(&t, NULL);
  37. end = t.tv_sec + (t.tv_usec / 1000000.0);
  38. printf("Input size: %d\tTime taken: %lf seconds\n", i, end - start);
  39. }
  40.  
  41. // GNUplot-related commands are commented out for online execution:
  42. // pipe = popen("gnuplot --persist", "w");
  43. // fprintf(pipe, "set title 'Run Time analysis of Selection Sort';");
  44. // fprintf(pipe, "set ylabel 'Time [m sec]'; ");
  45. // fprintf(pipe, "set xlabel 'Input';");
  46. // fprintf(pipe, "plot 'Sorting.txt' using 1:2 title 'Selection Sort' with linespoints \n");
  47.  
  48. return 0;
  49. }
Success #stdin #stdout 4.26s 5288KB
stdin
Standard input is empty
stdout
Input size: 10000	Time taken: 0.064409 seconds
Input size: 20000	Time taken: 0.221420 seconds
Input size: 40000	Time taken: 0.779198 seconds
Input size: 80000	Time taken: 3.217924 seconds