PhotoShaman

Untitled

Dec 18th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <clocale>
  4. #include <windows.h>
  5.  
  6. void output_mas(int *m, int size);
  7.  
  8. using namespace std;
  9.  
  10. void sort_vibor(int *m, int size)
  11. {
  12.     for (int i = 0; i < size - 1; i++)
  13.     {
  14.         int nMin = i;
  15.         for (int j = i + 1; j < size; j++)
  16.             if (m[j] < m[nMin]) nMin = j;
  17.         if (nMin != i)
  18.         {
  19.             int c = m[i];
  20.             m[i] = m[nMin];
  21.             m[nMin] = c;
  22.         }
  23.     }
  24. }
  25.  
  26. void quickSort(int *m, int nStart, int nEnd)
  27. {
  28.     int L, R, c, X;
  29.     if (nStart >= nEnd)
  30.         return; // готово
  31.     L = nStart;
  32.     R = nEnd;
  33.     X = m[(L + R) / 2]; // или X = A[irand(L,R)];      
  34.     while (L <= R)
  35.     {         // разделение
  36.         while (m[L] < X)
  37.             L++;
  38.         while (m[R] > X)
  39.             R--;
  40.         if (L <= R)
  41.         {
  42.             c = m[L];
  43.             m[L] = m[R];
  44.             m[R] = c;
  45.             L++; R--;
  46.         }
  47.     }
  48.     quickSort(m, nStart, R);   // рекурсивные вызовы
  49.     quickSort(m, L, nEnd);
  50. }
  51.  
  52. void main()
  53. {
  54.     setlocale(LC_ALL, "Russian");
  55.     int per = 0;
  56.     const int n1 = 100;
  57.     const int n2 = 1000;
  58.     const int n3 = 10000;
  59.     int mas1[n1], mas2[n2], mas3[n3];
  60.  
  61.     for (int i = 0; i < n1; i++)
  62.         mas1[i] = rand() % 50;
  63.     for (int i = 0; i < n2; i++)
  64.         mas2[i] = rand() % 50;
  65.     for (int i = 0; i < n3; i++)
  66.         mas3[i] = rand() % 50;
  67.     cout << "Исходный массив: " << endl;
  68.     output_mas(mas1, n1);
  69.  
  70.     sort_vibor(mas1, n1);
  71.     cout << "Сортировка выбором: " << endl;
  72.     output_mas(mas1, n1);
  73.     cout << endl;
  74.     cout << "Количество перестановок сортировкой выбора в массиве на 100 элементов=" << per;
  75.     cout << endl;
  76.     per = 0;
  77.     sort_vibor(mas2, n2);
  78.     cout << "Количество перестановок сортировкой выбора в массиве на 1000 элементов=" << per;
  79.     cout << endl;
  80.     per = 0;
  81.     sort_vibor(mas3, n3);
  82.     cout << "Количество перестановок сортировкой выбора в массиве на 10000 элементов=" << per;
  83.  
  84.  
  85.     for (int i = 0; i < n1; i++)
  86.         mas1[i] = rand() % 50;
  87.     for (int i = 0; i < n2; i++)
  88.         mas2[i] = rand() % 50;
  89.     for (int i = 0; i < n3; i++)
  90.         mas3[i] = rand() % 50;
  91.     cout << endl;
  92.     cout << "Исходный массив: " << endl;
  93.     output_mas(mas1, n1);
  94.     per = 0;
  95.     quickSort(mas1, 0, n1 - 1);
  96.  
  97.     cout << "Быстрая сортировка: " << endl;
  98.     output_mas(mas1, n1);
  99.     cout << endl;
  100.     cout << "Количество перестановок ,быстрой сортировкой в массиве на 100 элементов=" << per;
  101.     cout << endl;
  102.     per = 0;
  103.     quickSort(mas2, 0, n2 - 1);
  104.     cout << "Быстрая сортировка: " << endl;
  105.     cout << endl;
  106.     cout << "Количество перестановок ,быстрой сортировкой в массиве на 1000 элементов=" << per;
  107.     cout << endl;
  108.     per = 0;
  109.     quickSort(mas3, 0, n3 - 1);
  110.  
  111.     cout << "Быстрая сортировка: " << endl;
  112.     cout << endl;
  113.     cout << "Количество перестановок ,быстрой сортировкой в массиве на 10000 элементов=" << per;
  114.     cout << endl;
  115.  
  116.     cin.get(); cin.get();
  117. }
  118.  
  119. void output_mas(int *m, int size)
  120. {
  121.     for (int i = 0; i < size; i++)
  122.     {
  123.         if (i % 20 == 0)
  124.             cout << endl;
  125.         cout << m[i] << " ";
  126.     }
  127.  
  128.     cout << endl;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment