Advertisement
Guest User

АИСД Лаба 7

a guest
Nov 19th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.92 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <ctime>
  4. using namespace std;
  5. void QuickSort(int first, int last);
  6. void ShellSort(int Shell[],int size);
  7. int QuickSortCounter = 0;
  8. int *mas;
  9. void main()
  10. {
  11.     srand(time(0));
  12.     int size, counter = 0;
  13.     cout << "Input array size" << endl;
  14.     while (!(cin >> size) || size > 1000000 || size < 0) {
  15.         cout << "Size must be bigger than 0 and less than 1000000 . Please try one more time." << endl;
  16.         cin.clear();
  17.         while (cin.get() != '\n') continue;
  18.     }
  19.     mas = new int[size];
  20.     int *Shell = new int[size];
  21.     int count = 0;
  22.     cout << "Random array :" << endl;
  23.     for (int i = 0; i < size; i++)
  24.     {
  25.         mas[i] = rand() % 1000000;
  26.         Shell[i] = mas[i];
  27.         cout << mas[i] << " ";
  28.     }
  29.    
  30.     int first = 0, last = size - 1;
  31.     cout << endl;
  32.  
  33.     QuickSort(first, last);
  34.     cout << endl << "Sort array :" << endl;
  35.     for (int i = 0; i < size; i++)
  36.     {
  37.         cout << mas[i] << " ";
  38.         if (i + 1 <= size && mas[i] <= mas[i + 1])
  39.             count++;
  40.     }
  41.    
  42.     cout << endl;
  43.     cout << endl << "QuickSortCounter = " << QuickSortCounter << endl;
  44.     cout << endl;
  45.     ShellSort(Shell, size);
  46.  
  47. }
  48. void QuickSort(int first, int last)
  49. {
  50.     int change, middle = (last + first) / 2, pivot = mas[middle], premier = first, back = last;
  51.     int min = mas[0],counter = 0, F = 0, L = 0;
  52.     for (int i = premier; i < back; i++) {
  53.         if (mas[i] < min)
  54.             min = mas[i];
  55.     }
  56.     do {
  57.         if (mas[first] >= pivot && mas[last] <= pivot) {
  58.             change = mas[first];
  59.             mas[first] = mas[last];
  60.             mas[last] = change;
  61.             last--;
  62.             if (mas[first] == pivot)
  63.                 first--;
  64.         }
  65.         else {
  66.             if (mas[first] >= pivot && mas[last] > pivot)
  67.             {
  68.                 last--; first--;
  69.             }
  70.         }
  71.        
  72.         first++; QuickSortCounter++;
  73.     } while (first < last);
  74.    
  75.     for (int i = premier; i < back + 1; i++)
  76.     {
  77.         if (counter == 0)
  78.         {
  79.             if (mas[i] > pivot)
  80.             {
  81.                 F = i - 1; L = i;
  82.                 counter++;
  83.             }
  84.             else {
  85.                 if (mas[i] == pivot)
  86.                 {
  87.                     F = i; L = i+1;
  88.                     counter++;
  89.                 }
  90.             }
  91.         }
  92.     }
  93.    
  94.     if (F - premier >= 1 || (F - premier == 1 && mas[F] < mas[premier]))
  95.         QuickSort(premier, F);
  96.     if (back - L >= 1 || (back - L == 1 && mas[back] < mas[L]))
  97.         QuickSort(L, back);
  98. }
  99. void ShellSort(int Shell[] ,int size)
  100. {
  101.     int ShellSortCounter = 0;
  102.     int diff = 0, change; int j = 0;
  103.    
  104.     if (size % 2 == 0)
  105.         diff = size / 2;
  106.     else
  107.         diff = (size - 1) / 2;
  108.     do {
  109.         for (int i = 0; i + diff < size; i++)
  110.         {
  111.             if (Shell[i] > Shell[i + diff])
  112.             {
  113.                 change = Shell[i];
  114.                 Shell[i] = Shell[i + diff];
  115.                 Shell[i + diff] = change;
  116.                 ShellSortCounter++;
  117.                 j = i;
  118.                 while (j - diff >= 0 && Shell[j] < Shell[j - diff])
  119.                 {
  120.                     change = Shell[j];
  121.                     Shell[j] = Shell[j - diff];
  122.                     Shell[j - diff] = change;
  123.                     j = j - diff;
  124.                     ShellSortCounter++;
  125.                 }
  126.             }
  127.         }
  128.         if (diff % 2 == 0)
  129.             diff = diff / 2;
  130.         else
  131.             diff = (diff - 1) / 2;
  132.  
  133.     } while (diff >= 1);
  134.     cout << "ShellSortCounter = " << ShellSortCounter << endl;
  135.     cout << endl;
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement