Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cstdlib> // Для rand() и srand()
- #include <ctime> // Для time()
- using namespace std;
- //1.Дан массив целых чисел
- //Найти максимальное число среди отрицательных
- //2.Получить номера элементов степенью которых являются двойки
- //3.Найти среднее арифметическое элементов, которые являются полными квадратами.
- //4.Посчитать количество сравнений и обменов при использовании сортировки пузырьком,выбором,вставки.(Массивом размерностью 5к,10к,20к,и упорядоченных по возрастанию и убыванию)
- //1.Дан массив целых чисел
- //Найти максимальное число среди отрицательных
- int max_negative_number(const int* arr, unsigned int size) {
- int max_negative = *arr;
- for (unsigned int i = 0; size > i; i++) {
- if (*(arr + i) < 0 & *(arr + i) > max_negative) {
- max_negative = *(arr + i);
- }
- }
- return max_negative;
- }
- //2.Получить номера элементов степенью которых являются двойки
- void func(const int* arr, unsigned int size_arr) {
- int index = 1;
- for (unsigned int i = 0; size_arr > i; i++) {
- if (i + 1 == index) {
- cout << arr[i] << endl;
- index *= 2;
- }
- }
- }
- // сортировка пузырьком
- void sort_bubble(int* arr, unsigned int size_arr, bool comp = true) {
- unsigned int count_if = 0;
- unsigned int count = 0;
- int tempInt;
- int tempInt_left;
- int tempInt_right;
- for (unsigned int k = 0; size_arr - 1 > k; k++) {
- for (unsigned int i = 0; size_arr - 1 - k > i; i++) {
- tempInt_left = *(arr + i);
- tempInt_right = *(arr + i + 1);
- if (tempInt_left > tempInt_right) {
- *(arr + i + 1) = tempInt_left;
- *(arr + i) = tempInt_right;
- count_if++;
- }
- count++;
- }
- }
- cout << "sort_bubble: count_iteration = " << count << endl;
- cout << "sort_bubble: count_swa[] = " << count_if << endl;
- }
- // сортировка выборкой
- void sort_selection(int* arr, unsigned int size_arr, bool comp = true) {
- unsigned int count = 0;
- unsigned int count_if = 0;
- unsigned int k;
- int x;
- for (unsigned int i = 0; i < size_arr; i++) {
- k = i;
- x = *(arr + i);
- for (unsigned int j = i + 1; j < size_arr; j++) {
- if (*(arr + j) < x) {
- k = j;
- x = *(arr + j);
- count_if++;
- }
- count++;
- }
- *(arr + k) = *(arr + i);
- *(arr + i) = x;
- }
- cout << "sort_selection: count_iteration = " << count << endl;
- cout << "sort_selection: count_swap = " << count_if << endl;
- }
- // Сортировка вставкой
- void sort_insertion(int* arr, unsigned int size_arr, bool comp = true) {
- unsigned int count_if = 0;
- unsigned int count = 0;
- for (int i = 1; i < size_arr; i++) {
- for (int j = i; j > 0 & arr[j - 1] > arr[j]; j--) {
- int tmp = arr[j - 1];
- arr[j - 1] = arr[j];
- arr[j] = tmp;
- count++;
- count_if++;
- }
- }
- cout << "sort_insertion: count_iteration = " << count << endl;
- cout << "sort_insertion: count_swap = " << count_if << endl;
- }
- //3.Найти среднее арифметическое элементов, которые являются полными квадратами.
- void shake_arr(int* arr, unsigned int size_arr) {
- for (unsigned int k = 0; size_arr > k; k++) {
- *(arr + k) = rand() % 1024;
- }
- }
- int main() {
- int arr[] = { -8 , -4 , -2 , -1 , 16 , 32 , 64 };
- cout << max_negative_number(arr, 7) << endl;
- int arr1[] = { 11 , 30 , 44 , 9 , 3 , 6 , 0 , 1 , 33 };
- func(arr1, 9);
- srand(time(NULL));
- int arr_5k[5000];
- int arr_10k[10000];
- int arr_20k[20000];
- // 5k
- cout << "[Array 5k]" << endl;
- shake_arr(arr_5k, 5000);
- cout << arr_5k[0] << endl;
- sort_bubble(arr_5k, 5000);
- shake_arr(arr_5k, 5000);
- sort_selection(arr_5k, 5000);
- shake_arr(arr_5k, 5000);
- sort_insertion(arr_5k, 5000);
- shake_arr(arr_5k, 5000);
- // 10k
- cout << "[Array 10k]" << endl;
- shake_arr(arr_10k, 10000);
- sort_bubble(arr_10k, 10000);
- shake_arr(arr_10k, 10000);
- sort_selection(arr_10k, 10000);
- shake_arr(arr_10k, 10000);
- sort_insertion(arr_10k, 10000);
- shake_arr(arr_10k, 10000);
- // 20k
- cout << "[Array 20k]" << endl;
- shake_arr(arr_20k, 20000);
- sort_bubble(arr_20k, 20000);
- shake_arr(arr_20k, 20000);
- sort_selection(arr_20k, 20000);
- shake_arr(arr_20k, 20000);
- sort_insertion(arr_20k, 20000);
- shake_arr(arr_20k, 20000);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement