fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. // Swap utility
  6. void swap(long int* a, long int* b)
  7. {
  8. int tmp = *a;
  9. *a = *b;
  10. *b = tmp;
  11. }
  12.  
  13.  
  14. // Selection sort
  15. void selectionSort(long int arr[], long int n)
  16. {
  17. long int i, j, midx;
  18.  
  19. for (i = 0; i < n - 1; i++) {
  20.  
  21. // Find the minimum element in unsorted array
  22. midx = i;
  23.  
  24. for (j = i + 1; j < n; j++)
  25. if (arr[j] < arr[midx])
  26. midx = j;
  27.  
  28. // Swap the found minimum element
  29. // with the first element
  30. swap(&arr[midx], &arr[i]);
  31. }
  32. }
  33.  
  34. // Driver code
  35. int main()
  36. {
  37. long int n = 10000;
  38. int it = 0;
  39.  
  40. // Arrays to store time duration
  41. // of sorting algorithms
  42. double tim3[10];
  43.  
  44. printf("A_size, Selection\n");
  45.  
  46. // Performs 10 iterations
  47. while (it++ < 5) {
  48. long int a[n];
  49.  
  50. // generating n random numbers
  51. // storing them in arrays a, b, c
  52. for (int i = 0; i < n; i++) {
  53. long int no = rand() % n + 1;
  54. a[i] = no;
  55. }
  56.  
  57. // using clock_t to store time
  58. clock_t start, end;
  59.  
  60. // Selection sort
  61. start = clock();
  62. selectionSort(a, n);
  63. end = clock();
  64.  
  65. tim3[it] = ((double)(end - start));
  66.  
  67. // type conversion to long int
  68. // for plotting graph with integer values
  69. printf("%li, %li \n", n, (long int)tim3[it]);
  70. ;
  71.  
  72. // increases the size of array by 10000
  73. n += 10000;
  74. }
  75.  
  76. return 0;
  77. }
Success #stdin #stdout 2s 5320KB
stdin
Standard input is empty
stdout
A_size, Selection
10000, 27258 
20000, 150186 
30000, 308567 
40000, 601516 
50000, 905908