Advertisement
Mlack

Untitled

Apr 27th, 2014
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <locale.h>
  4. #include <ctime>
  5. #include <conio.h>
  6.  
  7. using namespace std;
  8.  
  9. void quick_sort(int**, int, int, int);
  10.  
  11. int main() {
  12.     setlocale(LC_ALL, "RUS");
  13.     srand(time(NULL));
  14.     int n, m;
  15.     cout << "Введите кол-во строк: "; cin >> n;
  16.     cout << "Введите кол-во столбцов: "; cin >> m;
  17.     int **matrix = new int *[n];
  18.     for (int i = 0; i < n; ++i)
  19.         matrix[i] = new int[m];
  20.     for (int i = 0; i < n; ++i)
  21.     for (int j = 0; j < m; ++j)
  22.         matrix[i][j] = rand() % 50 + 1;
  23.     for (int i = 0; i < n; ++i)
  24.     for (int j = 0; j < m; ++j) {
  25.         cout << matrix[i][j] << " ";
  26.         if (j == m - 1) { cout << endl; }
  27.     }
  28.     cout << endl << endl;
  29.     //==============QUICK_SORT=====================
  30.     for (int i = 0; i < n; i++)
  31.         for (int j = 0; j < m; j += m)
  32.             quick_sort(matrix, i, 0, m-1);
  33.         //==============QUICK_SORT=====================
  34.     for (int i = 0; i < n; ++i)
  35.         for (int j = 0; j < m; ++j) {
  36.             cout << matrix[i][j] << " ";
  37.             if (j == m - 1) { cout << endl; }
  38.         }
  39.     _getch();
  40. }
  41.  
  42. void quick_sort(int **matrix, int rows, int low, int high) {
  43.     //low == i; high == m
  44.     int i = low, j = high, med_ind = (low + high) / 2, med = matrix[rows][med_ind];
  45.     do {
  46.         while (matrix[rows][i] < med) {++i;}
  47.         while (matrix[rows][j] > med) {--j;}
  48.         if (i <= j) {
  49.             if (matrix[rows][i] > matrix[rows][j]) {
  50.                 matrix[rows][i] = matrix[rows][i] ^ matrix[rows][j];
  51.                 matrix[rows][j] = matrix[rows][i] ^ matrix[rows][j];
  52.                 matrix[rows][i] = matrix[rows][i] ^ matrix[rows][j];
  53.                 cout << med << " " << i << " " << j << endl;
  54.                 for (int x = 0; x < 6; ++x) { cout << matrix[rows][x] << " "; }
  55.             }
  56.             i++; j--;
  57.         }
  58.     }
  59.     while (i > j);
  60.     cout << endl << low << " " << i << " " << j << endl;
  61.     /*if (low < j) {quick_sort(matrix, 0, low, j);}
  62.     if (i < high) {quick_sort(matrix, 0, i, high);}
  63.     */
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement