SHARE
TWEET

ShellSort

CzarnyBarszcz Nov 12th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     srand(time(NULL));
  11. int n;
  12. cout<<"ile liczb posortowac?"<<endl;
  13. cin>>n;
  14. int * a = new int[n];
  15. for(int k=0;k<n;k++)
  16. {
  17.     a[k]=(rand()%100)+1;
  18. }
  19. int d=n/2,p,x=2;
  20. for(int j=0;j<n;j++)
  21. {
  22.     cout<<j<<"-el :"<<a[j]<<endl;
  23. }
  24. cout<<endl;
  25. do
  26. {
  27. for(int i=d;i<n;i++)
  28. {
  29.     if(a[i]<a[i-d])
  30.         {
  31.             p=a[i];
  32.             a[i]=a[i-d];
  33.             a[i-d]=p;
  34.            while(i-(d*x)>=0)
  35.            {
  36.                if(a[i-(d*(x-1))]<a[i-(d*x)])
  37.                {
  38.                     p=a[i-(d*(x-1))];
  39.                     a[i-(d*(x-1))]=a[i-(d*x)];
  40.                     a[i-(d*x)]=p;
  41.                     x++;
  42.                }
  43.                else
  44.                {
  45.                    x=2;
  46.                    break;
  47.                }
  48.  
  49.            }
  50.         }
  51.  
  52. }
  53.  d=d/2;
  54. }while(d!=0);
  55.  
  56. for(int j=0;j<n;j++)
  57. {
  58.     cout<<j<<"-el :"<<a[j]<<endl;
  59. }
  60.     delete [] a;
  61.     getch();
  62.     return 0;
  63. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top