vkichukova

Untitled

Jan 29th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int* mergeArrays(int a[], int m, int b[], int n)
  6. {
  7. int *c = new int[m+n];
  8. int smaller;
  9. m > n ? smaller = m : smaller = n;
  10. int i = 0, j = 0, k = 0;
  11.  
  12. while(i < smaller && j < smaller)
  13. {
  14. if(a[i] < b[i])
  15. {
  16. c[k++] = a[i++];
  17. }
  18. else
  19. {
  20. c[k++] = b[j++];
  21. }
  22. }
  23.  
  24. while(i < m)
  25. {
  26. c[k++] = a[i++];
  27. }
  28. while(j < n)
  29. {
  30. c[k++] = b[j++];
  31. }
  32. /*for(int i = 0; i < k; i++)
  33. cout << c[i] << " ";*/
  34. return c;
  35. }
  36.  
  37. void quickSort(int arr[], int left, int right) {
  38.  
  39. int i = left, j = right;
  40. int tmp;
  41. int pivot = arr[(left + right) / 2];
  42.  
  43. /* partition */
  44. while (i <= j)
  45. {
  46.  
  47. while (arr[i] < pivot)
  48. i++;
  49.  
  50. while (arr[j] > pivot)
  51. j--;
  52.  
  53. if (i <= j)
  54. {
  55. tmp = arr[i];
  56. arr[i] = arr[j];
  57. arr[j] = tmp;
  58. i++;
  59. j--;
  60. }
  61. }
  62.  
  63. /* recursion */
  64. if (left < j)
  65. quickSort(arr, left, j);
  66. if (i < right)
  67. quickSort(arr, i, right);
  68. }
  69.  
  70. int main()
  71. {
  72. int a[4] = {4,3,2,1};
  73. int b[5] = {9,8,6,5,7};
  74.  
  75. quickSort(a, 0, 3);
  76. quickSort(b, 0, 4);
  77. int *c = mergeArrays(a, 4, b, 5);
  78.  
  79. cout << endl;
  80. for(int i = 0; i < 9; i++)
  81. cout << c[i] << " ";
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment