Advertisement
NickAndNick

Сортировка нечётных строк матрицы.

Dec 15th, 2012
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. bool __cdecl random_matrix(int **, const size_t, const size_t, const int _left = 1, int _rigth = 1000);
  9. void __cdecl show_matrix(int **, const size_t, const size_t);
  10. void __cdecl delete_matrix(int **, size_t);
  11. int ** __cdecl create_matrix(int **, const size_t, const size_t);
  12. void __cdecl quick_sort(int *, const size_t, const size_t);
  13. void __cdecl uswap(int &, int &);
  14.  
  15. int main() {
  16.     srand(unsigned(time(NULL)));
  17.     wcout.imbue(locale("rus_rus.866"));
  18.  
  19.     wcout << L"Введите строки: ";
  20.     size_t rows;
  21.     cin >> rows;
  22.     wcout << L"Введите столбцы: ";
  23.     size_t cols;
  24.     cin >> cols;
  25.  
  26.     int ** matrix = NULL;
  27.     matrix = create_matrix(matrix, rows, cols);
  28.  
  29.     if (random_matrix(matrix, rows, cols)) {
  30.         wcout << L"Исходная матрица:\n";
  31.         show_matrix(matrix, rows, cols);
  32.  
  33.         for (size_t row = 1; row < rows; row += 2)
  34.             quick_sort(matrix[row], 0, cols - 1);
  35.  
  36.         wcout << L"Изменённая матрица:\n";
  37.         show_matrix(matrix, rows, cols);
  38.     } else wcout << L"\aНе удалось заполнить матрицу!\n";
  39.  
  40.     delete_matrix(matrix, rows);
  41.     matrix = NULL;
  42.  
  43.     cin.get(); cin.get();
  44.     return 0;
  45. }
  46.  
  47. inline bool random_matrix(int ** _matrix, const size_t _rows, const size_t _cols, const int _left, int _rigth) {
  48.     if (_left >= _rigth) return false;
  49.  
  50.     if (_left < 0 && _rigth >= 0) _rigth += abs(_left);
  51.     else if (_left < 0 && _rigth < 0) _rigth = abs(_left) - abs(_rigth);
  52.     else if (_left >= 0 && _rigth > 0) _rigth -= _left;
  53.  
  54.     for (size_t row = 0; row < _rows; row++)
  55.         for (size_t col = 0; col < _cols; col++)
  56.             _matrix[row][col] = rand() % _rigth + _left;
  57.  
  58.     return true;
  59. }
  60.  
  61. void show_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  62.     if (_cols && _rows)
  63.         for (size_t row = 0; row < _rows; row++, cout << endl)
  64.             for (size_t col = 0; col < _cols; col++)
  65.                 cout << setw(5) << _matrix[row][col];
  66.     else wcout << L"Нет данных для отображения!\n";
  67. }
  68.  
  69. void delete_matrix(int ** _matrix, size_t _rows) {
  70.     for (size_t row = 0; row < _rows; row++)
  71.         delete[] _matrix[row];
  72.     delete[] _matrix;
  73. }
  74.  
  75. int ** create_matrix(int ** _matrix, const size_t _rows, const size_t _cols) {
  76.     _matrix = new int * [_rows];
  77.  
  78.     for (size_t row = 0; row < _rows; row++)
  79.         _matrix[row] = new int [_cols];
  80.  
  81.     return _matrix;
  82. }
  83.  
  84. void quick_sort(int * _array, const size_t _beg, const size_t _end) {
  85.     int n = (int)_beg, m = (int)_end;
  86.     int mid = _array[(_beg + _end) >> 1];
  87.  
  88.     do {
  89.         while (_array[n] < mid && n < (int)_end) ++n;
  90.         while (_array[m] > mid && m > (int)_beg) --m;
  91.  
  92.         if (n <= m) {
  93.             if (n < m) uswap(_array[n], _array[m]);
  94.             ++n;
  95.             --m;
  96.         }
  97.     } while (n <= m);
  98.  
  99.     if ((int)_beg < m) quick_sort(_array, _beg, m);
  100.     if (n < (int)_end) quick_sort(_array, n, _end);
  101. }
  102.  
  103. void uswap(int & _a, int & _b) {
  104.     int temp = _a;
  105.     _a = _b;
  106.     _b = temp;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement