Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- #define PI 3.14926
- #undef PI
- #define begin {
- #define end }
- #define integer int
- #define print cout <<
- #define POW(X) (X) * (X)
- integer get_sum(integer a, integer b)
- begin
- return a + b;
- end
- #define WINDOWS
- #ifdef WINDOWS
- const double pi = 3.1415926;
- void show_arr(int size, int arr[]);
- void test1() {
- test1();
- cout << "AA ";
- }
- template <class T>
- int binary_search(int left, int right, T arr[], T target) {
- while (right > left) {
- int midle = left + (right - left) / 2;
- if (arr[midle] == target) {
- return midle;
- }
- else if (arr[midle] > target) {
- right = midle;
- }
- else {
- left = midle + 1;
- }
- }
- return -1;
- }
- template <class T>
- int binary_search_recursion(
- int left, int right, T arr[], T target) {
- if (left < right) {
- int midle = left + (right - left) / 2;
- if (arr[midle] == target)
- return midle;
- else if (arr[midle] > target)
- binary_search_recursion(left, midle, arr, target);
- else
- binary_search_recursion(midle+1, right, arr, target);
- }
- else
- return -1;
- }
- void test_binary() {
- const int size = 10;
- char arr[size]{'a','b','c','d','e','f','g','h','i','j'};
- char element = 'c';
- int curr = binary_search_recursion(0, size, arr, element);
- cout << element << " at ";
- cout << curr << " " << arr[curr] << endl;
- cout << "left " << arr[curr - 1] << endl;
- cout << "right " << arr[curr + 1] << endl;
- }
- void my_swap(int& a, int& b) {
- int tmp = b;
- b = a;
- a = tmp;
- }
- void quick_sort(int size, int arr[]) {
- int left = 0;
- int right = size-1;
- int val_midle = arr[size / 2];
- // маленькие - в левую часть
- // большие в правую часть массива
- int tmp;
- do {
- while (arr[left] < val_midle) left++;
- while (arr[right] > val_midle) right--;
- if (left <= right) {
- tmp = arr[left];
- arr[left] = arr[right];
- arr[right] = tmp;
- left++;
- right--;
- }
- //show_arr(size, arr);
- } while (left <= right);
- if (right > 0) quick_sort(right+1, arr); // в этой строке была ошибка с индексами
- if (left < size) quick_sort(size-left, arr+left);
- }
- void show_arr(int size, int arr[]) {
- for (int k = 0; k < size; k++) {
- cout << arr[k] << " ";
- }
- cout << endl;
- }
- void test_quick_sort() {
- int arr[]{ 3,4,1,6,7,2,7,9 };
- int size = sizeof(arr) / sizeof(arr[0]);
- show_arr(size, arr);
- quick_sort(size, arr);
- show_arr(size, arr);
- }
- void main() {
- //test_binary();
- test_quick_sort();
- /*
- cout << pi*2 << endl;
- print get_sum(2, 5);
- print endl;
- cout << POW(9 + 1) << endl;
- */
- }
- #else
- void main() {
- cout << "Linux not work" << endl;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement