Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. clock_t start, end;
  6. double cpu_time_used;
  7. int i, j;
  8. FILE *fp;
  9. void swap(int *xp, int *yp)
  10. {
  11. int temp = *xp;
  12. *xp = *yp;
  13. *yp = temp;
  14. }
  15.  
  16. // A function to implement bubble sort
  17. void bubbleSort(int arr[], int n)
  18. {
  19.  
  20. for (i = 0; i < n - 1; i++)
  21.  
  22. // Last i elements are already in place
  23. for (j = 0; j < n - i - 1; j++)
  24. if (arr[j] > arr[j + 1])
  25. swap(&arr[j], &arr[j + 1]);
  26. }
  27.  
  28. // A function to implement swap sort
  29. void swapSort(int arr[], int n)
  30. {
  31. for (i = 0; i < n - 1; i++)
  32. {
  33. for (j = i + 1; j < n; j++)
  34. {
  35. if (arr[i] > arr[j])
  36. {
  37. swap(&arr[i], &arr[j]);
  38. }
  39. }
  40. }
  41. }
  42.  
  43. int partition(int arr[], int l, int h)
  44. {
  45. int x = arr[h];
  46. int i = (l - 1);
  47.  
  48. for (int j = l; j <= h - 1; j++)
  49. {
  50. if (arr[j] <= x)
  51. {
  52. i++;
  53. swap(&arr[i], &arr[j]);
  54. }
  55. }
  56. swap(&arr[i + 1], &arr[h]);
  57. return (i + 1);
  58. }
  59.  
  60. /* A[] --> Array to be sorted,
  61. l --> Starting index,
  62. h --> Ending index */
  63.  
  64. void quickSortIterative(int arr[], int l, int h)
  65. {
  66. // Create an auxiliary stack
  67. int stack[h - l + 1];
  68.  
  69. // initialize top of stack
  70. int top = -1;
  71.  
  72. // push initial values of l and h to stack
  73. stack[++top] = l;
  74. stack[++top] = h;
  75.  
  76. // Keep popping from stack while is not empty
  77. while (top >= 0)
  78. {
  79. // Pop h and l
  80. h = stack[top--];
  81. l = stack[top--];
  82.  
  83. // Set pivot element at its correct position
  84. // in sorted array
  85. int p = partition(arr, l, h);
  86.  
  87. // If there are elements on left side of pivot,
  88. // then push left side to stack
  89. if (p - 1 > l)
  90. {
  91. stack[++top] = l;
  92. stack[++top] = p - 1;
  93. }
  94.  
  95. // If there are elements on right side of pivot,
  96. // then push right side to stack
  97. if (p + 1 < h)
  98. {
  99. stack[++top] = p + 1;
  100. stack[++top] = h;
  101. }
  102. }
  103. }
  104.  
  105. /* Function to print an array */
  106. void printArray(int arr[], int size)
  107. {
  108. int i;
  109.  
  110. for (i = 0; i < size; i++)
  111. {
  112. printf("%d ", arr[i]);
  113. }
  114. printf("\n");
  115. }
  116. void sort_algorithm(int n, int arr[])
  117. {
  118.  
  119. int lower = 1, upper = n, count = n;
  120. // Use current time as
  121. // seed for random generator
  122. srand(time(0));
  123. for (i = 0; i < count; i++)
  124. {
  125. int num = (rand() %
  126. (upper - lower + 1)) +
  127. lower;
  128. arr[i] = num;
  129. }
  130.  
  131. printf("Unsorted array: \n");
  132. printArray(arr, n);
  133.  
  134.  
  135. start = clock();
  136. bubbleSort(arr, n);
  137. end = clock();
  138. cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
  139. fprintf(fp, "Time for bubbleSort: %f \n", cpu_time_used);
  140. printf("Sorted array by bubbleSort: \n");
  141. printArray(arr, n);
  142. printf("Time for bubbleSort: \n");
  143. printf("%f \n", cpu_time_used);
  144.  
  145. start = clock();
  146. swapSort(arr, n);
  147. end = clock();
  148. cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
  149. fprintf(fp, "Time for swapSort: %f \n", cpu_time_used);
  150. printf("Sorted array by swapSort: \n");
  151. printArray(arr, n);
  152. printf("Time for swapSort: \n");
  153. printf("%f \n", cpu_time_used);
  154.  
  155. start = clock();
  156. quickSortIterative(arr, 0, n - 1);
  157. end = clock();
  158. cpu_time_used = ((double)(end - start)) / CLOCKS_PER_SEC;
  159. fprintf(fp, "Time for quickSort: %f \n", cpu_time_used);
  160. printf("Sorted array by quickSort: \n");
  161. printArray(arr, n);
  162. printf("Time for quickSort: \n");
  163. printf("%f \n", cpu_time_used);
  164. }
  165.  
  166. int main()
  167. {
  168. fp = fopen("Output.txt", "w");
  169. int *arr;
  170. int lenght_of_array;
  171. for (lenght_of_array = 10; lenght_of_array <= 1000; lenght_of_array*=10)
  172. {
  173. fprintf(fp, "Size of array is: %d\n", lenght_of_array);
  174. arr=calloc(lenght_of_array, sizeof(int));
  175. sort_algorithm(lenght_of_array, arr);
  176.  
  177. }
  178.  
  179. fclose(fp);
  180. free(arr);
  181. return 0;
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement