Advertisement
maycod23

sorting_algos

Apr 22nd, 2022
1,091
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. // void merge(int *a, int Start, int Mid, int End)
  4. // {
  5. //     int temp[End - Start + 1];
  6. //     int i = Start;
  7. //     int j = Mid + 1;
  8. //     int k = 0;
  9. //     while (i <= Mid && j <= End)
  10. //     {
  11. //         //observation->operations
  12. //         if (a[i] < a[j])
  13. //         {
  14. //             temp[k] = a[i];
  15. //             i++;
  16. //         }
  17. //         else
  18. //         {
  19. //             temp[k] = a[j];
  20. //             j++;
  21. //         }
  22. //         k++;
  23. //     }
  24. //     //for the left out elements
  25. //     while (i <= Mid)
  26. //     {
  27. //         temp[k] = a[i];
  28. //         k++;
  29. //         i++;
  30. //     }
  31. //     while (j <= End)
  32. //     {
  33. //         temp[k] = a[j];
  34. //         k++;
  35. //         j++;
  36. //     }
  37. //     for ( i = Start; i <= End; i++)
  38. //     {
  39. //         a[i] = temp[i - Start];
  40. //     }
  41. // }
  42. // void mergesort(int *a, int start, int end)
  43. // {
  44. //     if (start < end)
  45. //     {
  46. //         int mid = (start + end) / 2;
  47. //         mergesort(a, start, mid);
  48. //         mergesort(a, mid + 1, end);
  49. //         merge(a, start, mid, end);
  50. //     }
  51. // }
  52. // int main()
  53. // {
  54. //     int n, i;
  55. //     cout << "enter the number of elements you wanna take in array" << "\n";
  56. //     cin >> n;
  57. //     int a[n];
  58. //     cout << "enter the elements of your array" << "\n";
  59. //     for (int i = 0; i <= n - 1; i++)
  60. //     {
  61. //         cin >> a[i];
  62. //     }
  63. //     cout << "your array is" << "\n";
  64. //     for (int i = 0; i <= n - 1; i++)
  65. //     {
  66. //         cout << a[i] << " ";
  67. //     }
  68. //     cout << "\n";
  69. //     mergesort(a, 0, n - 1);
  70. //     cout << "the sorted array is" << endl;
  71. //     for ( int i = 0; i <= n - 1; i++)
  72. //     {
  73. //         cout << a[i] << " ";
  74. //     }
  75. //     cout << "\n";
  76. //     return 0;
  77. // }
  78.  
  79.  
  80.  
  81. ///////////////////////////quicksort///////////////////////////
  82. //////////////////////////////////////////////////////
  83. //////////////partition///////////////////////////
  84. int partition(int *a, int start, int end)
  85. {
  86.     int pivot = a[end], pindex = start, i;
  87.     for (i = start; i < end; i++)
  88.     {
  89.         if (a[i] <= pivot)
  90.         {
  91.             swap(a[i], a[pindex]);
  92.             pindex++;
  93.         }
  94.     }
  95.     swap(a[end], a[pindex]);
  96.     return pindex;
  97. }
  98.  
  99. ///////////////////////////recursive///////////////////////////
  100. // void quicksort(int *a, int start, int end)
  101. // {
  102. //     if (start <= end)
  103. //     {
  104. //         int p = partition(a, start, end);//p ->correct index of a element
  105. //         quicksort(a, start, p - 1);
  106. //         quicksort(a, p + 1, end);
  107. //     }
  108. // }
  109.  
  110.  
  111.  
  112.  
  113. ///////////////////////////iterative quicksort///////////////////////////
  114. // void iterativeQuickSort(int *a, int start, int end)
  115. // {
  116. //     stack<pair<int, int>> stk;
  117. //     stk.push(make_pair(start, end));
  118. //     while (stk.size() != 0)
  119. //     {
  120. //         start = stk.top().first, end = stk.top().second;
  121. //         stk.pop();
  122. //         int pivot = partition(a, start, end);
  123. //         if (pivot - 1 > start) stk.push(make_pair(start, pivot - 1));
  124. //         if (pivot + 1 < end)   stk.push(make_pair(pivot + 1, end));
  125. //     }
  126. // }
  127.  
  128. // int main()
  129. // {
  130. //     int n, i;
  131. //     cout << "enter the number of elements you wanna take in array" << "\n";
  132. //     cin >> n;
  133. //     int a[n];
  134. //     cout << "enter the elements of your array" << "\n";
  135. //     for (int i = 0; i <= n - 1; i++)
  136. //     {
  137. //         cin >> a[i];
  138. //     }
  139. //     cout << "your array is" << "\n";
  140. //     for (int i = 0; i <= n - 1; i++)
  141. //     {
  142. //         cout << a[i] << " ";
  143. //     }
  144. //     cout << "\n";
  145. //     // quicksort(a, 0, n - 1);
  146. //     iterativeQuickSort(a, 0, n - 1);
  147. //     cout << "the sorted array is" << endl;
  148. //     for ( int i = 0; i <= n - 1; i++)
  149. //     {
  150. //         cout << a[i] << " ";
  151. //     }
  152. //     cout << "\n";
  153. //     return 0;
  154. // }
  155.  
  156.  
  157.  
  158.  
  159. ///////////////////////////quickselect method///////////////////////////
  160. ///////////////////////////kth smallest element///////////////////////////
  161. int main()
  162. {
  163.     int n, i, j, k;
  164.     cin >> n >> k;
  165.     k--;//k given 1 indexing ->0 indexing
  166.     //initial k->3rd smallest===saying it as like 2nd index in array (0 indexing)
  167.     int a[n];
  168.     for (i = 0; i < n; i++) cin >> a[i];
  169.     int start = 0, end = n - 1;
  170.     if (k < start || k > end)
  171.     {
  172.         cout << "-1" << endl; return 0;
  173.     }
  174.     //edge cases
  175.     while (1)//always true//infinite loop
  176.         //start<=end
  177.     {
  178.         int p = partition(a, start, end);
  179.         //0 indexing
  180.         if (p == k)
  181.         {
  182.             cout << a[p] << endl; break;
  183.         }
  184.         if (p < k)
  185.         {
  186.             start = p + 1; continue;
  187.         }
  188.         else
  189.         {
  190.             end = p - 1; continue;
  191.         }
  192.     }
  193.     return 0;
  194. }
  195.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement