Advertisement
DrSmile

Siakode

Feb 17th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib> // Для rand() и srand()
  4. #include <ctime>   // Для time()
  5. using namespace std;
  6.  
  7. //1.Дан массив целых чисел
  8. //Найти максимальное число среди отрицательных
  9.  
  10. //2.Получить номера элементов степенью которых являются двойки
  11.  
  12. //3.Найти среднее арифметическое элементов, которые являются полными квадратами.
  13.  
  14. //4.Посчитать количество сравнений и обменов при использовании сортировки пузырьком,выбором,вставки.(Массивом размерностью 5к,10к,20к,и упорядоченных по возрастанию и убыванию)
  15.  
  16. //1.Дан массив целых чисел
  17. //Найти максимальное число среди отрицательных
  18. int max_negative_number(const int* arr, unsigned int size) {
  19.     int max_negative = *arr;
  20.  
  21.     for (unsigned int i = 0; size > i; i++) {
  22.         if (*(arr + i) < 0 & *(arr + i) > max_negative) {
  23.             max_negative = *(arr + i);
  24.         }
  25.     }
  26.  
  27.  
  28.  
  29.     return max_negative;
  30. }
  31. //2.Получить номера элементов степенью которых являются двойки
  32. void func(const int* arr, unsigned int size_arr) {
  33.     int index = 1;
  34.  
  35.     for (unsigned int i = 0; size_arr > i; i++) {
  36.         if (i + 1 == index) {
  37.             cout << arr[i] << endl;
  38.             index *= 2;
  39.         }
  40.     }
  41. }
  42.  
  43. // сортировка пузырьком
  44. void sort_bubble(int* arr, unsigned int size_arr, bool comp = true) {
  45.     unsigned int count_if = 0;
  46.     unsigned int count = 0;
  47.  
  48.     int tempInt;
  49.     int tempInt_left;
  50.     int tempInt_right;
  51.  
  52.     for (unsigned int k = 0; size_arr - 1 > k; k++) {
  53.         for (unsigned int i = 0; size_arr - 1 - k > i; i++) {
  54.             tempInt_left = *(arr + i);
  55.             tempInt_right = *(arr + i + 1);
  56.  
  57.             if (tempInt_left > tempInt_right) {
  58.                 *(arr + i + 1) = tempInt_left;
  59.                 *(arr + i) = tempInt_right;
  60.  
  61.                 count_if++;
  62.             }
  63.  
  64.             count++;
  65.         }
  66.     }
  67.  
  68.     cout << "sort_bubble: count_iteration = " << count << endl;
  69.     cout << "sort_bubble: count_swa[] = " << count_if << endl;
  70. }
  71. // сортировка выборкой
  72. void sort_selection(int* arr, unsigned int size_arr, bool comp = true) {
  73.     unsigned int count = 0;
  74.     unsigned int count_if = 0;
  75.     unsigned int k;
  76.     int x;
  77.  
  78.     for (unsigned int i = 0; i < size_arr; i++) {
  79.         k = i;
  80.         x = *(arr + i);
  81.  
  82.         for (unsigned int j = i + 1; j < size_arr; j++) {
  83.  
  84.             if (*(arr + j) < x) {
  85.                 k = j;
  86.                 x = *(arr + j);
  87.  
  88.                 count_if++;
  89.             }
  90.  
  91.             count++;
  92.         }
  93.  
  94.         *(arr + k) = *(arr + i);
  95.         *(arr + i) = x;
  96.     }
  97.  
  98.     cout << "sort_selection: count_iteration = " << count << endl;
  99.     cout << "sort_selection: count_swap = " << count_if << endl;
  100. }
  101. // Сортировка вставкой
  102. void sort_insertion(int* arr, unsigned int size_arr, bool comp = true) {
  103.     unsigned int count_if = 0;
  104.     unsigned int count = 0;
  105.  
  106.     for (int i = 1; i < size_arr; i++) {
  107.         for (int j = i; j > 0 & arr[j - 1] > arr[j]; j--) {
  108.             int tmp = arr[j - 1];
  109.             arr[j - 1] = arr[j];
  110.             arr[j] = tmp;
  111.  
  112.             count++;
  113.             count_if++;
  114.  
  115.         }
  116.  
  117.     }
  118.  
  119.     cout << "sort_insertion: count_iteration = " << count << endl;
  120.     cout << "sort_insertion: count_swap = " << count_if << endl;
  121. }
  122.  
  123. //3.Найти среднее арифметическое элементов, которые являются полными квадратами.
  124. void shake_arr(int* arr, unsigned int size_arr) {
  125.     for (unsigned int k = 0; size_arr > k; k++) {
  126.         *(arr + k) = rand() % 1024;
  127.     }
  128. }
  129.  
  130.  
  131.  
  132. int main() {
  133.    
  134.     int arr[] = { -8 , -4  , -2 , -1 , 16 , 32 , 64 };
  135.  
  136.     cout << max_negative_number(arr, 7) << endl;
  137.  
  138.  
  139.  
  140.  
  141.  
  142.  
  143.     int arr1[] = { 11 , 30 , 44 , 9 , 3 , 6 , 0 , 1 , 33 };
  144.  
  145.     func(arr1, 9);
  146.  
  147.  
  148.  
  149.    
  150.  
  151.  
  152.  
  153.     srand(time(NULL));
  154.     int arr_5k[5000];
  155.     int arr_10k[10000];
  156.     int arr_20k[20000];
  157.  
  158.     // 5k  
  159.     cout << "[Array 5k]" << endl;
  160.     shake_arr(arr_5k, 5000);
  161.     cout << arr_5k[0] << endl;
  162.  
  163.     sort_bubble(arr_5k, 5000);
  164.     shake_arr(arr_5k, 5000);
  165.  
  166.     sort_selection(arr_5k, 5000);
  167.     shake_arr(arr_5k, 5000);
  168.  
  169.     sort_insertion(arr_5k, 5000);
  170.     shake_arr(arr_5k, 5000);
  171.  
  172.  
  173.  
  174.     // 10k  
  175.     cout << "[Array 10k]" << endl;
  176.     shake_arr(arr_10k, 10000);
  177.  
  178.     sort_bubble(arr_10k, 10000);
  179.     shake_arr(arr_10k, 10000);
  180.  
  181.     sort_selection(arr_10k, 10000);
  182.     shake_arr(arr_10k, 10000);
  183.  
  184.     sort_insertion(arr_10k, 10000);
  185.     shake_arr(arr_10k, 10000);
  186.  
  187.  
  188.  
  189.     // 20k  
  190.     cout << "[Array 20k]" << endl;
  191.     shake_arr(arr_20k, 20000);
  192.  
  193.     sort_bubble(arr_20k, 20000);
  194.     shake_arr(arr_20k, 20000);
  195.  
  196.     sort_selection(arr_20k, 20000);
  197.     shake_arr(arr_20k, 20000);
  198.  
  199.     sort_insertion(arr_20k, 20000);
  200.     shake_arr(arr_20k, 20000);
  201.  
  202.     return 0;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement