Advertisement
Petro_zzz

быстрая сортировка, бинарный поиск

May 13th, 2024
868
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.73 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. #define PI 3.14926
  4.  
  5. #undef PI
  6.  
  7. #define begin {
  8. #define end }
  9. #define integer int
  10. #define print cout <<
  11.  
  12. #define POW(X) (X) * (X)
  13.  
  14. integer get_sum(integer a, integer b)
  15. begin
  16.     return a + b;
  17. end
  18.  
  19. #define WINDOWS
  20.  
  21. #ifdef WINDOWS
  22.  
  23. const double pi = 3.1415926;
  24.  
  25.  
  26.  
  27.  
  28.  
  29. void show_arr(int size, int arr[]);
  30.  
  31. void test1() {
  32.     test1();
  33.     cout << "AA ";
  34. }
  35.  
  36. template <class T>
  37. int binary_search(int left, int right, T arr[], T target) {
  38.     while (right > left) {
  39.         int midle = left + (right - left) / 2;
  40.         if (arr[midle] == target) {
  41.             return midle;
  42.         }
  43.         else if (arr[midle] > target) {
  44.             right = midle;
  45.         }
  46.         else {
  47.             left = midle + 1;
  48.         }
  49.     }
  50.     return -1;
  51. }
  52.  
  53. template <class T>
  54. int binary_search_recursion(
  55.     int left, int right, T arr[], T target) {
  56.     if (left < right) {
  57.         int midle = left + (right - left) / 2;
  58.         if (arr[midle] == target)
  59.             return midle;
  60.         else if (arr[midle] > target)
  61.             binary_search_recursion(left, midle, arr, target);
  62.         else
  63.             binary_search_recursion(midle+1, right, arr, target);
  64.     }
  65.     else
  66.         return -1;
  67. }
  68.  
  69. void test_binary() {
  70.     const int size = 10;
  71.     char arr[size]{'a','b','c','d','e','f','g','h','i','j'};
  72.     char element = 'c';
  73.    
  74.     int curr = binary_search_recursion(0, size, arr, element);
  75.    
  76.     cout << element << " at ";
  77.     cout << curr << " " << arr[curr] << endl;
  78.     cout << "left " << arr[curr - 1] << endl;
  79.     cout << "right " << arr[curr + 1] << endl;
  80.  
  81. }
  82.  
  83. void my_swap(int& a, int& b) {
  84.     int tmp = b;
  85.     b = a;
  86.     a = tmp;
  87. }
  88.  
  89. void quick_sort(int size, int arr[]) {
  90.     int left = 0;
  91.     int right = size-1;
  92.     int val_midle = arr[size / 2];
  93.     // маленькие - в левую часть
  94.     // большие в правую часть массива
  95.     int tmp;
  96.     do {
  97.         while (arr[left] < val_midle) left++;
  98.         while (arr[right] > val_midle) right--;
  99.         if (left <= right) {
  100.            
  101.             tmp = arr[left];
  102.             arr[left] = arr[right];
  103.             arr[right] = tmp;
  104.            
  105.             left++;
  106.             right--;
  107.         }
  108.         //show_arr(size, arr);
  109.     } while (left <= right);
  110.    
  111.     if (right > 0) quick_sort(right+1, arr); // в этой строке была ошибка с индексами
  112.     if (left < size) quick_sort(size-left, arr+left);
  113. }
  114.  
  115. void show_arr(int size, int arr[]) {
  116.     for (int k = 0; k < size; k++) {
  117.         cout << arr[k] << " ";
  118.     }
  119.     cout << endl;
  120. }
  121.  
  122.  
  123. void test_quick_sort() {
  124.     int arr[]{ 3,4,1,6,7,2,7,9 };
  125.     int size = sizeof(arr) / sizeof(arr[0]);
  126.    
  127.     show_arr(size, arr);
  128.     quick_sort(size, arr);
  129.     show_arr(size, arr);
  130. }
  131.  
  132.  
  133.  
  134. void main() {
  135.     //test_binary();
  136.     test_quick_sort();
  137.     /*
  138.     cout << pi*2 << endl;
  139.     print get_sum(2, 5);
  140.     print endl;
  141.  
  142.     cout << POW(9 + 1) << endl;
  143.     */
  144. }
  145.  
  146. #else
  147. void main() {
  148.     cout << "Linux not work" << endl;
  149. }
  150.  
  151. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement