Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.58 KB | None | 0 0
  1. #include<iostream>
  2. #include<ctime>
  3. using namespace std;
  4.  
  5. void fillArr(int* arr, int n)
  6. {
  7. for (int i = 0; i < n; i++)
  8. {
  9. arr[i] = (rand()*rand()) % 65536;
  10. }
  11. }
  12.  
  13. void outArr(int* arr, int n)
  14. {
  15. cout << "\nArray:" << endl;
  16. for (int i = 0; i < n; i++)
  17. {
  18. cout << arr[i] << endl;
  19. }
  20. }
  21.  
  22. void BubbleSort(int* arr, int n)
  23. {
  24. for (int i = 0; i < n - 1; i++)
  25. for (int j = 0; j < n - i - 1; j++)
  26. {
  27. if (arr[j] > arr[j + 1])
  28. {
  29. int temp = arr[j];
  30. arr[j] = arr[j + 1];
  31. arr[j + 1] = temp;
  32. }
  33. }
  34. }
  35.  
  36. void qSort(int* arr, int begin, int end)
  37. {
  38. int left = begin;
  39. int right = end;
  40. int pivot = arr[((left + right) / 2)];
  41. while (left <= right)
  42. {
  43. while (arr[left] < pivot) left++;
  44. while (arr[right] > pivot) right--;
  45. if (left <= right)
  46. {
  47. int temp = arr[left];
  48. arr[left] = arr[right];
  49. arr[right] = temp;
  50. left++;
  51. right--;
  52. }
  53. }
  54.  
  55. if (end > left) qSort(arr, left, end);
  56. if (begin < right)qSort(arr, begin, right);
  57. }
  58.  
  59. void ShellSort(int *arr, int n)
  60. {
  61. int i,j,k;
  62. for(i=n/2;i>0;i/=2)
  63. for(j=i;j<n;j++){
  64. int temp = arr[j];
  65. for(k=j;k>=i;k-=i){
  66. if(temp<arr[k-i])
  67. arr[k] = arr[k-i];
  68. else break;
  69. }
  70. arr[k] = temp;
  71. }
  72.  
  73. }
  74.  
  75.  
  76. int main()
  77. {
  78. int n;
  79. char qwe;
  80. cout << "We ask you to enter the size of an array:";
  81. cin >> n;
  82. int *arr1 = new int[n];
  83. int *arr2 = new int[n];
  84. int *arr3 = new int[n];
  85.  
  86. fillArr(arr1, n);
  87.  
  88. for (int i = 0; i < n; i++)
  89. {
  90. arr2[i] = arr1[i];
  91. arr3[i] = arr1[i];
  92. }
  93.  
  94. cout << "Shall we print the original aray? y/n\n";
  95. cin >> qwe;
  96. if (qwe == 'y') outArr(arr1, n);
  97.  
  98. unsigned long t = clock();
  99. BubbleSort(arr1, n);
  100. unsigned long y = clock();
  101.  
  102. cout << "Shall we print the BubbleSorted aray? y/n\n";
  103. cin >> qwe;
  104. if (qwe == 'y') outArr(arr1, n);
  105. cout << "Time: " << ((double)y / CLOCKS_PER_SEC) - ((double)t / CLOCKS_PER_SEC) << endl;
  106.  
  107. t = clock();
  108. qSort(arr2, 0, n - 1);
  109. y = clock();
  110.  
  111. cout << "Shall we print the QuickSorted aray? y/n\n";
  112. cin >> qwe;
  113. if (qwe == 'y') outArr(arr2, n);
  114. cout << "Time: " << ((double)y / CLOCKS_PER_SEC) - ((double)t / CLOCKS_PER_SEC) << endl;
  115.  
  116. t = clock();
  117. ShellSort(arr3, n);
  118. y = clock();
  119.  
  120. cout << "Shall we print the ShellSorted aray? y/n\n";
  121. cin >> qwe;
  122. if (qwe == 'y') outArr(arr3, n);
  123. cout << "Time: " << ((double)y / CLOCKS_PER_SEC) - ((double)t / CLOCKS_PER_SEC) << endl;
  124.  
  125. cout << "We thank you for using our product. Until next time." << endl;
  126.  
  127. system("pause");
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement