1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
| #include "stdafx.h"
#include <iostream>
#include <conio.h>
#include <ctime>
using namespace std;
//Вывод матрицы на консоль.
void output_matrix(int ** matrix, int rows, int columns)
{
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
cout << matrix[i][j] << ' ';
}
cout << endl;
}
}
//Копирование одной матрицы в другую
void copy_matrix(int ** dest_matrix, int ** source_matrix, int rows, int columns)
{
for (int i = 0; i < rows; ++i)
for (int j = 0; j < columns; ++j)
dest_matrix[i][j] = source_matrix[i][j];
}
//Быстрая сортировка
void quickSort(int * arr, int arr_size, int & compare_count, int & swap_count)
{
int lastIndex = arr_size - 1; //Последний индекс
int leftIndex = 0, rightIndex = lastIndex; //Устанавливаем индексы в начальные значения
int centerElement = arr[lastIndex / 2]; //Берем центральный элемент в качестве опорного
// процедура разделения
do {
compare_count += 2; //Будет минимум одно сравнение для каждой половины
while (arr[leftIndex] > centerElement) //Крутим цикл пока не найдем элемент меньший или равный опорному(от начала к середине)
{
++leftIndex;
++compare_count;
}
while (arr[rightIndex] < centerElement) //Крутим цикл пока не найдем элемент больший или равный опорному(от конца к середине)
{
--rightIndex;
++compare_count;
}
if (leftIndex <= rightIndex) //Если не прошли середину, то
{
//меняем элементы
int temp = arr[leftIndex];
arr[leftIndex] = arr[rightIndex];
arr[rightIndex] = temp;
++leftIndex;
--rightIndex;
++swap_count;
}
} while (leftIndex <= rightIndex); //Продолжаем цикл до тех пор, пока индексы не пересекуться (пока не дойдем до середины массива)
// рекурсивные вызовы для каждой половинки
if (rightIndex > 0) quickSort(arr, rightIndex + 1, compare_count, swap_count); //Для правой
if (lastIndex > leftIndex) quickSort(arr + leftIndex, arr_size - leftIndex, compare_count, swap_count); //Для левой
}
int main()
{
srand(time(0));
setlocale(LC_ALL, "RUS");
int rows = 0, columns = 0;
int compare_count = 0;
int swap_count = 0;
cout << "Введите кол-во строк массива. ";
cout << "Помните, значение должно быть: " << endl;
cout << "- целым" << endl << "- принадлежащим [1; 20]" << endl;
cin >> rows;
cout << "Введите кол-во столбцов массива. ";
cout << "Помните, значение должно быть: " << endl;
cout << "- целым" << endl << "- принадлежащим [1; 20]" << endl;
cin >> columns;
//Выделяем память под указатели
int ** matrix = new int*[rows];
int ** sorted_matrix = new int*[rows];
//Динамическое создание двумерного массива
for (int i = 0; i < rows; i++)
{
matrix[i] = new int[columns];
sorted_matrix[i] = new int[columns];
}
//Заполнение массива
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; ++j)
{
matrix[i][j] = rand() % 90 + 10;
//matrix[i][j] = 1;
}
}
//////////////////////////////////////////////////
///////////ВЫВОДИМ ИСХОДНУЮ МАТРИЦУ///////////////
//////////////////////////////////////////////////
cout << "Исходная матрица:\n";
output_matrix(matrix, rows, columns);
//////////////////////////////////////////////////
//////////////////БЫСТРАЯ СОРТИРОВКА//////////////
//////////////////////////////////////////////////
copy_matrix(sorted_matrix, matrix, rows, columns);
compare_count = 0;
swap_count = 0;
for (int i = 0; i < rows; ++i)
{
int tmp_comp = 0;
int tmp_swap = 0;
quickSort(sorted_matrix[i], columns, tmp_comp, tmp_swap);
compare_count += tmp_comp;
swap_count += tmp_swap;
}
cout << "\nБыстрая сортировка\n";
output_matrix(sorted_matrix, rows, columns);
cout << "Сравнений: " << compare_count << " Перестановок: " << swap_count << endl << endl << endl;
int compare_quick = compare_count;
int swap_quick = swap_count;
} |