Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.40 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<time.h>
  5. int arr[500000];
  6.  
  7. double swp;
  8. void selection_sort(int a[], int n)
  9. {
  10. int i,j,min_index,temp;
  11. swp=0;
  12. for(i=0; i<n-1; i++)
  13. {
  14. min_index = i;
  15. for(j=i+1; j<n; j++)
  16. {
  17. if(a[j]<a[min_index])
  18. {
  19. min_index = j;
  20. }
  21. }
  22. if(min_index!=i)
  23. {
  24. temp = a[min_index];
  25. a[min_index]=a[i];
  26. a[i] = temp;
  27. swp++;
  28. }
  29. }
  30. }
  31. void insertion_sort(int a[],int n)
  32. {
  33. int i,j,k,temp;
  34. swp=0;
  35. for(i=0; i<n; i++)
  36. {
  37. j=i;
  38. while(j>0&&a[j-1]>a[j])
  39. {
  40. temp=a[j];
  41. a[j]=a[j-1];
  42. a[j-1]=temp;
  43. j--;
  44. swp++;
  45. }
  46. }
  47.  
  48. }
  49.  
  50. void bubble_sort(int a[], int n)
  51. {
  52. int i,j,temp;
  53. swp=0;
  54. for(i=0; i<n; i++)
  55. {
  56. for(j=0; j<n-i-1; j++)
  57. {
  58. if(a[j]>a[j+1])
  59. {
  60. temp = a[j];
  61. a[j] = a[j+1];
  62. a[j+1] = temp;
  63. swp++;
  64. }
  65. }
  66. }
  67. }
  68.  
  69. int main()
  70. {
  71.  
  72. int maxx=100, i;
  73. clock_t start_time, end_time;
  74. double bubble_time;
  75. double selection_time;
  76. double insertion_time;
  77.  
  78. //printf("Enter the Upper bound: ");
  79. //scanf("%d", &maxx);
  80.  
  81. srand(time(0));
  82. for(i=0; i<1000; i++)
  83. {
  84. arr[i]=rand()%maxx+1;
  85. }
  86.  
  87. start_time = clock();
  88. bubble_sort(arr,1000);
  89. end_time = clock();
  90.  
  91. bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  92. printf("\nTime for bubble sort 1000: %lf\n",bubble_time);
  93.  
  94. start_time = clock();
  95. selection_sort(arr,1000);
  96. end_time = clock();
  97.  
  98. selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  99. printf("\nTime for selection sort 1000: %lf\n",selection_time);
  100.  
  101. start_time = clock();
  102. insertion_sort(arr,1000);
  103. end_time = clock();
  104.  
  105. insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  106. printf("\nTime for insertion sort 1000: %lf\n",insertion_time);
  107.  
  108.  
  109. /*for(i=0;i<1000;i++)
  110. {
  111. printf(" %d ",arr[i]);
  112. }
  113.  
  114. printf("\n");
  115. */
  116.  
  117.  
  118.  
  119.  
  120. for(i=0; i<5000; i++)
  121. {
  122. arr[i]=rand()%maxx+1;
  123. }
  124.  
  125. start_time = clock();
  126. bubble_sort(arr,5000);
  127. end_time = clock();
  128.  
  129. bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  130. printf("Time for bubble sort 5000: %lf\n",bubble_time);
  131. printf("swp = %lf\n",swp);
  132. start_time = clock();
  133. selection_sort(arr,5000);
  134. end_time = clock();
  135.  
  136.  
  137. printf("%d %d %d %d\n",arr[1],arr[2],arr[3],arr[4]);
  138. printf("%d\n",swp);
  139.  
  140. selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  141. printf("Time for selection sort 5000: %lf\n",selection_time);
  142. printf("swp = %lf\n",swp);
  143.  
  144.  
  145. start_time = clock();
  146. insertion_sort(arr,5000);
  147. end_time = clock();
  148.  
  149. insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  150. printf("\nTime for insertion sort 5000: %lf\n",insertion_time);
  151. printf("swp = %lf\n",swp);
  152.  
  153. for(i=0; i<10000; i++)
  154. {
  155. arr[i]=rand()%maxx+1;
  156. }
  157.  
  158. start_time = clock();
  159. bubble_sort(arr,10000);
  160. end_time = clock();
  161.  
  162. bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  163. printf("Time for bubble sort 10000: %lf\n",bubble_time);
  164. printf("swp = %lf\n",swp);
  165.  
  166. start_time = clock();
  167. selection_sort(arr,10000);
  168. end_time = clock();
  169.  
  170. selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  171. printf("Time for selection sort 10000: %lf\n",selection_time);
  172. printf("swp = %lf\n",swp);
  173.  
  174. start_time = clock();
  175. insertion_sort(arr,10000);
  176. end_time = clock();
  177.  
  178. insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  179. printf("\nTime for insertion sort 10000: %lf\n",insertion_time);
  180. printf("swp = %lf\n",swp);
  181.  
  182.  
  183. for(i=0; i<50000; i++)
  184. {
  185. arr[i]=rand()%maxx+1;
  186. }
  187.  
  188. start_time = clock();
  189. bubble_sort(arr,50000);
  190. end_time = clock();
  191.  
  192. bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  193. printf("Time for bubble sort 50000: %lf\n",bubble_time);
  194. printf("swp = %lf\n",swp);
  195. start_time = clock();
  196. selection_sort(arr,50000);
  197. end_time = clock();
  198.  
  199. selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  200. printf("Time for selection sort 50000: %lf\n",selection_time);
  201. printf("swp = %lf\n",swp);
  202.  
  203. start_time = clock();
  204. insertion_sort(arr,50000);
  205. end_time = clock();
  206.  
  207. insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  208. printf("\nTime for insertion sort 50000: %lf\n",insertion_time);
  209. printf("swp = %lf\n",swp);
  210.  
  211. for(i=0; i<100000; i++)
  212. {
  213. arr[i]=rand()%maxx+1;
  214. }
  215.  
  216. start_time = clock();
  217. bubble_sort(arr,100000);
  218. end_time = clock();
  219.  
  220. bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  221. printf("Time for bubble sort 100000: %lf\n",bubble_time);
  222. printf("swp = %lf\n",swp);
  223.  
  224. start_time = clock();
  225. selection_sort(arr,100000);
  226. end_time = clock();
  227.  
  228. selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  229. printf("Time for selection sort 100000: %lf\n",selection_time);
  230. printf("swp = %lf\n",swp);
  231.  
  232. start_time = clock();
  233. insertion_sort(arr,100000);
  234. end_time = clock();
  235.  
  236. insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  237. printf("\nTime for insertion sort 100000: %lf\n",insertion_time);
  238. printf("swp = %lf\n",swp);
  239.  
  240.  
  241. for(i=0; i<500000; i++)
  242. {
  243. arr[i]=rand()%maxx+1;
  244. }
  245.  
  246. start_time = clock();
  247. bubble_sort(arr,500000);
  248. end_time = clock();
  249.  
  250. bubble_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  251. printf("Time for bubble sort 500000: %lf\n",bubble_time);
  252. printf("swp = %lf\n",swp);
  253. start_time = clock();
  254. selection_sort(arr,500000);
  255. end_time = clock();
  256.  
  257. selection_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  258. printf("Time for selction sort 500000: %lf\n",selection_time);
  259. printf("swp = %lf\n",swp);
  260.  
  261. start_time = clock();
  262. insertion_sort(arr,500000);
  263. end_time = clock();
  264.  
  265. insertion_time = (double)(end_time-start_time)/CLOCKS_PER_SEC;
  266. printf("\nTime for insertion sort 500000: %lf\n",insertion_time);
  267. printf("swp = %lf\n",swp);
  268.  
  269.  
  270.  
  271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement