Advertisement
Guest User

shell sorting

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <stdlib.h>
  3. #include <cstdio>
  4. #include <iostream>
  5. using namespace std;
  6. class Making
  7. {
  8. public:
  9.     void making(int *arr,int n)
  10.     {
  11.         for (int i = 0; i < n; i++)
  12.         {
  13.             arr[i] = rand() % 10;
  14.         }
  15.         for (int i = 0; i<n; i++)
  16.         {
  17.             cout << arr[i] << " ";
  18.         }
  19.     }
  20. };
  21. class Shell : Making
  22. {
  23. public:
  24.     int n;
  25.     void SortingByShell(int *arr,int n)
  26.     {
  27.         int d = n/2;//шаг (вилка) для сравнения элементов
  28.         int temp;//временная переменная для изменения позиций элементов
  29.         int k = 2;//переменная,которая задает длинну цикла
  30.         int j = 0;// переменная для учета границы массива,увеличивается соотв. d
  31. #pragma region Shell sorting
  32.         while (d != 0)
  33.         {
  34.             for (int i = 0; i < n/k-j ; i++)
  35.             {
  36.                 if (arr[i] > arr[i + d])
  37.                 {
  38.                     temp = arr[i];
  39.                     arr[i] = arr[i + d];
  40.                     arr[i + d] = temp;
  41.                 }
  42.                 if (i > d && arr[i]<arr[i-d])
  43.                 {
  44.                     temp = arr[i];
  45.                     arr[i] = arr[i - d];
  46.                     arr[i - d] = temp;
  47.                 }
  48.             }
  49.             d =(d / 2);
  50.             k=1;
  51.             j = d;
  52.         }
  53. #pragma endregion
  54.         cout << endl;
  55.         cout << "after Shell sorting:" << endl;
  56.         for(int i=0;i<n;i++)
  57.         {cout << arr[i] << " ";}
  58. #pragma region insert_sorting_(d=1)
  59.         for (int i = 0; i < n-1; i++)
  60.         {
  61.                 for (int i = 0; i < n - 1; i++)
  62.                 {
  63.                     if (arr[i] > arr[i + 1])
  64.                     {
  65.                         temp = arr[i];
  66.                         arr[i] = arr[i + 1];
  67.                         arr[i + 1] = temp;
  68.                     }
  69.                 }
  70.         }
  71. #pragma endregion  
  72.     }
  73.     void show(int *arr,int n)
  74.     {
  75.         cout << endl;
  76.         cout << "sorting achivied:" << endl;
  77.         for (int i = 0; i < n; i++)
  78.         {
  79.             cout << arr[i] << " ";
  80.         }
  81.     }
  82. };
  83.  
  84. int main()
  85. {
  86.     int n;
  87.     cout << "type size of array" << endl;
  88.     cin >> n;
  89.     int *arr = new int[n];
  90.     Making Makinging;
  91.     Makinging.making(arr,n);
  92.     Shell shelling;
  93.     shelling.SortingByShell(arr,n);
  94.     shelling.show(arr,n);
  95.     system("Pause");
  96.     return 0;
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement