Guest User

Untitled

a guest
Feb 25th, 2018
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. // Coded by ScratchyCode
  2. // Try to compile in gcc with and without -O2 option
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. void bubbleSort(int array[], int N);
  8. void merge(int *A, int *L, int leftCount, int *R, int rightCount);
  9. void mergeSort(int *A, int n);
  10.  
  11. int main(){
  12. // array1 and array2 are equal
  13. int array1[] = {
  14. 1,51,18,56,156,84,89,1,156,16,
  15. 11,1,61,561,189,894,51,561,651,8,
  16. 48,65,165,1,81,1,91,65,18,489,
  17. 98,7897,984,56,561,651,561,56,165,1561,
  18. 651,4465,5648,97,4894984,48,564165,654489,8946,
  19. 564654,654,44654,64564634,56465456,5456465,456456,545,566,654,
  20. 7897,318,79,9879,4561,654,454,74,56,2,
  21. 456,3,465,4,654,13,654,465,879,32,
  22. 3969,28,82825,727,7781,111111,4561,4641,3465,13,
  23. 156,1654,456,414,45,8,6,4564,123,56,
  24. 456,46,156,564,4165564,65,231,123,4564,45,
  25. 56,1,3156,4,651,16,1,65465,1,654,
  26. 16,16,1,64,65,61,64,489813,13,4,
  27. 4,41,64613,7967,64479,6131,0,49,1313,654,
  28. 3569,9338,7812,489465,16468,16584,163,6513,131654,746,
  29. 313,1,7,963,15113,11563,1713,651651,165165,461231,
  30. 1717,17,78769,878,9,97,79874,897987,8974,17,
  31. 797,9,494,494,144,4894,23123,4984,465,16165,
  32. 8949,44849,4564,21,2121,4174,741,1231,2131,64,
  33. 21313,1654561,4654013,456231,17456465,1312165,32321321,1321321,132153,12316
  34. };
  35.  
  36. int array2[] = {
  37. 1,51,18,56,156,84,89,1,156,16,
  38. 11,1,61,561,189,894,51,561,651,8,
  39. 48,65,165,1,81,1,91,65,18,489,
  40. 98,7897,984,56,561,651,561,56,165,1561,
  41. 651,4465,5648,97,4894984,48,564165,654489,8946,
  42. 564654,654,44654,64564634,56465456,5456465,456456,545,566,654,
  43. 7897,318,79,9879,4561,654,454,74,56,2,
  44. 456,3,465,4,654,13,654,465,879,32,
  45. 3969,28,82825,727,7781,111111,4561,4641,3465,13,
  46. 156,1654,456,414,45,8,6,4564,123,56,
  47. 456,46,156,564,4165564,65,231,123,4564,45,
  48. 56,1,3156,4,651,16,1,65465,1,654,
  49. 16,16,1,64,65,61,64,489813,13,4,
  50. 4,41,64613,7967,64479,6131,0,49,1313,654,
  51. 3569,9338,7812,489465,16468,16584,163,6513,131654,746,
  52. 313,1,7,963,15113,11563,1713,651651,165165,461231,
  53. 1717,17,78769,878,9,97,79874,897987,8974,17,
  54. 797,9,494,494,144,4894,23123,4984,465,16165,
  55. 8949,44849,4564,21,2121,4174,741,1231,2131,64,
  56. 21313,1654561,4654013,456231,17456465,1312165,32321321,1321321,132153,12316
  57. };
  58.  
  59. int dim = 200;
  60. double start, end, totalBubbleSort, totalMergeSort;
  61.  
  62. start = clock();
  63. bubbleSort(array1,dim);
  64. end = clock();
  65. totalBubbleSort = (double)(end - start) / CLOCKS_PER_SEC;
  66.  
  67. start = clock();
  68. mergeSort(array2,dim);
  69. end = clock();
  70. totalMergeSort = (double)(end - start) / CLOCKS_PER_SEC;
  71.  
  72. printf("Dim array:\t%d\nBubble sort:\t%.10lf s\nMerge sort:\t%.10lf s\n",dim,totalBubbleSort,totalMergeSort);
  73.  
  74. return 0;
  75. }
  76.  
  77. void bubbleSort(int array[], int N){
  78. int i, j;
  79. int tmp;
  80.  
  81. for(i=0; i<N; i++){
  82. for(j=0; j<N-i-1; j++){
  83. if(array[j] > array[j+1]){
  84. tmp = array[j];
  85. array[j] = array[j+1];
  86. array[j+1] = tmp;
  87. }
  88. }
  89. }
  90.  
  91. return ;
  92. }
  93.  
  94. void merge(int *A, int *L, int leftCount, int *R, int rightCount){
  95. // lefCount = number of elements in L
  96. // rightCount = number of elements in R.
  97. // i - to mark the index of left aubarray (L)
  98. // j - to mark the index of right sub-raay (R)
  99. // k - to mark the index of merged subarray (A)
  100. int i=0, j=0, k=0;
  101.  
  102. while(i<leftCount && j< rightCount){
  103. if(L[i] < R[j])
  104. A[k++] = L[i++];
  105. else
  106. A[k++] = R[j++];
  107. }
  108.  
  109. while(i < leftCount)
  110. A[k++] = L[i++];
  111. while(j < rightCount)
  112. A[k++] = R[j++];
  113.  
  114. return ;
  115. }
  116.  
  117. void mergeSort(int *A, int n){
  118. int mid, i, *L, *R;
  119.  
  120. if(n < 2)
  121. return; // base condition. If the array has less than two element, do nothing.
  122.  
  123. mid = n/2; // find the mid index.
  124.  
  125. // create left and right subarrays
  126. // mid elements (from index 0 till mid-1) should be part of left sub-array
  127. // and (n-mid) elements (from mid to n-1) will be part of right sub-array
  128. L = (int*)malloc(mid * sizeof(int));
  129. R = (int*)malloc((n - mid) * sizeof(int));
  130.  
  131. for(i=0; i<mid; i++)
  132. L[i] = A[i]; // creating left subarray
  133. for(i=mid; i<n; i++)
  134. R[i-mid] = A[i]; // creating right subarray
  135.  
  136. mergeSort(L,mid); // sorting the left subarray
  137. mergeSort(R,n-mid); // sorting the right subarray
  138. merge(A,L,mid,R,n-mid); // Merging L and R into A as sorted list.
  139.  
  140. free(L);
  141. free(R);
  142. }
Add Comment
Please, Sign In to add comment