Advertisement
Neveles

© 2020 Neveles. All rights reserved.

Apr 5th, 2020
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. //shellSort.cpp
  2. void ShellSort(int n, int mass[])
  3. {
  4.   int i, j, step;
  5.   int tmp;
  6.   for (step = n / 2; step > 0; step /= 2)
  7.   {
  8.     for (i = step; i < n; i++)
  9.     {
  10.       tmp = mass[i];
  11.       for (j = i; j >= step; j -= step)
  12.       {
  13.         if (tmp < mass[j - step])
  14.         {
  15.           mass[j] = mass[j - step];
  16.         }
  17.         else
  18.         {
  19.           break;
  20.         }
  21.       }
  22.       mass[j] = tmp;
  23.     }
  24.   }
  25. }
  26.  
  27.  
  28.  
  29. //main.cpp
  30. #include <iostream>
  31.  
  32. void ShellSort(int n, int mass[]);
  33.  
  34. int main()
  35. {
  36.   int n;
  37.   std::cout << "Input number of elements: ";
  38.   std::cin >> n;
  39.  
  40.   int *mass = new int[n];
  41.  
  42.  
  43.   std::cout << "---- Shell sort ----\nInput the array elements:\n";
  44.   for (int i = 0; i < n; i++)
  45.   {
  46.     std::cin >> mass[i];
  47.   }
  48.  
  49.   ShellSort(n, mass);
  50.  
  51.   std::cout << "Sorted array:\n";
  52.   for (int i = 0; i < n; i++)
  53.   {
  54.     std::cout << mass[i] << " ";
  55.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement