Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- int partition(int a[], int start, int end)
- {
- int pivot = a[end];
- int pIndex = start;
- for (int i = start; i < end; i++)
- {
- if (a[i] <= pivot)
- {
- swap(a[i], a[pIndex]);
- pIndex++;
- }
- }
- swap(a[pIndex], a[end]);
- return pIndex;
- }
- void quicksort(int a[], int start, int end)
- {
- if (start >= end) {
- return;
- }
- int pivot = partition(a, start, end);
- quicksort(a, start, pivot - 1);
- quicksort(a, pivot + 1, end);
- }
- void heapify(int arr[], int n, int i)
- {
- int largest = i;
- int l = 2 * i + 1;
- int r = 2 * i + 2;
- if (l < n && arr[l] > arr[largest])
- largest = l;
- if (r < n && arr[r] > arr[largest])
- largest = r;
- if (largest != i)
- {
- swap(arr[i], arr[largest]);
- heapify(arr, n, largest);
- }
- }
- void heapSort(int arr[], int n)
- {
- for (int i = n / 2 - 1; i >= 0; i--)
- heapify(arr, n, i);
- for (int i = n - 1; i >= 0; i--)
- {
- swap(arr[0], arr[i]);
- heapify(arr, i, 0);
- }
- }
- int main() {
- int n;
- cin >> n;
- int** a = new int* [n];
- for (int i = 0; i < n; i++) {
- a[i] = new int[n];
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cin >> a[i][j];
- }
- }
- int* t = new int[n];
- int c = 1;
- int i = 0;
- int j = 1;
- while (c <= n) {
- while (i < n - 1 && j < n) {
- t[i] = a[i][j];
- i++; j++;
- }
- heapSort(t, i); //сортируем
- //quicksort(t, 0, i-1); // сортируем
- i = 0;
- j = c;
- while (i < n - 1 && j < n) { // вставка отсортированного обратно
- a[i][j] = t[i];
- i++; j++;
- }
- for (int k = 0; k < i; k++) { // обнуление временного массива
- t[k] = 0;
- }
- i = 0;
- c++;
- j = c;
- }
- int u = 3;
- int k = 4;
- int m = 3;
- int g = 0;
- while (u != -1) {
- while (m != -1) {
- t[g] = a[k][m];
- k--; m--;
- g++;
- }
- heapSort(t, g); //сортируем
- //quicksort(t, 0, g-1); // сортируем
- k = 4;
- m = u;
- g = 0;
- while (m != -1) {
- a[k][m] = t[g];
- k--; m--;
- g++;
- }
- for (int l = 0; l < g; l++) {
- t[l] = 0;
- }
- g = 0;
- k = 4;
- u--;
- m = u;
- }
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- cout << a[i][j] << " ";
- }
- cout << endl;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement