Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>
  4. int array[500009];
  5. long swap_count = 0;
  6.  
  7. void bubble_sort(int a[], int n)
  8. {
  9. int i,j,temp;
  10. long comp_count = 0;
  11. clock_t start_time, end_time;
  12. double time_passed;
  13.  
  14. start_time = clock();
  15. for(i=0;i<n;i++)
  16. {
  17. for(j=0;j<n-i-1;j++)
  18. {
  19. comp_count++;
  20. if(a[j]>a[j+1])
  21. {
  22. swap_count++;
  23. temp = a[j];
  24. a[j] = a[j+1];
  25. a[j+1] = temp;
  26. }
  27. }
  28. }
  29. end_time = clock();
  30. time_passed = (double)(end_time - start_time)/CLOCKS_PER_SEC;
  31.  
  32. printf("\nTime: %lf",time_passed);
  33. printf("\nSwap count: %ld",swap_count);
  34. printf("\nComp count: %ld\n",comp_count);
  35. }
  36.  
  37. void selection_sort(int a[], int n)
  38. {
  39.  
  40. int i,j,index_min,temp;
  41. long comp_count = 0;
  42. clock_t start_time, end_time;
  43. double time_passed;
  44.  
  45. start_time = clock();
  46. for(i=0; i<n-1; i++)
  47. {
  48. index_min = i;
  49. for(j=i+1; j<n; j++)
  50. {
  51. comp_count++;
  52. if(a[j] < a[index_min])
  53. {
  54. index_min = j;
  55. }
  56. }
  57. if(index_min != i)
  58. {
  59. swap_count++;
  60. temp = a[i];
  61. a[i] = a[index_min];
  62. a[index_min] = temp;
  63. }
  64. }
  65. end_time = clock();
  66. time_passed = (double)(end_time - start_time)/CLOCKS_PER_SEC;
  67. //return time_passed;
  68. printf("\nTime: %lf",time_passed);
  69. printf("\nSwap count: %ld",swap_count);
  70. printf("\nComp count: %ld\n",comp_count);
  71. }
  72.  
  73. void insertion_sort(int a[], int n)
  74. {
  75. int i, j, item;
  76. long comp_count = 0;
  77. clock_t start_time, end_time;
  78. double time_passed;
  79.  
  80. start_time = clock();
  81. for(i=1; i<n; i++)
  82. {
  83. item = a[i];
  84. j = i-1;
  85. comp_count++;
  86. while(j>=0 && a[j]>item)
  87. {
  88. swap_count++;
  89. a[j+1] = a[j];
  90. j = j-1;
  91. }
  92. a[j+1] = item;
  93. }
  94. end_time = clock();
  95. time_passed = (double)(end_time - start_time)/CLOCKS_PER_SEC;
  96. //return time_passed;
  97. printf("\nTime: %lf",time_passed);
  98. printf("\nSwap count: %ld",swap_count);
  99. printf("\nComp count: %ld\n",comp_count);
  100. }
  101.  
  102.  
  103. int main()
  104. {
  105. int num;
  106. int i, j, digit;
  107. double selection, bubble, insersion;
  108.  
  109. printf("Enter number range: ");
  110. scanf("%d",&num);
  111.  
  112. printf("Enter a number to fix digit range: ");
  113. scanf("%d",&digit);
  114.  
  115. srand(time(0));
  116.  
  117. for(i=0; i<num; i++)
  118. {
  119. array[i] = rand() % digit + 1;
  120. }
  121.  
  122. printf("\nFor bubble sort : ");
  123. bubble_sort(array, num);
  124. printf("\nFor selection sort : ");
  125. selection_sort(array, num);
  126. printf("\nFor insertion sort : ");
  127. insertion_sort(array, num);
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement