Shorting with Shel Short

Posted by Didi Setyapramana On 6:11 PM 0 komentar


#include<conio.h>
#include<iostream.h>
#define n 10

class shellsort{
  static int A[n];
public:
  void InsSort(int start, int step);
  void ShellSort();
  void tampil();
};

int shellsort::A[n]={20,23,120,56,78,50,12,89,10,12};

void shellsort::InsSort(int start, int step)
{
  int i,j,y;
  bool ketemu;

  i=start+step;
  while(i<=n)
  {
     y=A[i];
     j=i-step;
     ketemu=false;
     while((j>=0)&&(!ketemu))
     {
        if(y<A[j])
        {
           A[j+step]=A[j];
           j=j-step;
        }
        else
           ketemu=true;
     }
     A[j+step]=y;
     i=i+step;
  }            

}

void shellsort::ShellSort()
{
   int step,start;
   step=n;
   while(step>1)
   {
      step=step/3+1;
      for(start=1;start<=step;start++)
             shellsort::InsSort(start,step);
   }
}

void shellsort::tampil(){
for(int a=0;a<10;a++)
    {
       cout<<A[a]<<" ";
    }
    cout<<endl<<endl;
}

void main()
{
    shellsort x;
    cout<<"PENGURUTAN SHELL"<<endl<<endl;
    cout<<"Sebelum diurut : "<<endl<<"A = ";
    x.tampil();
    x.ShellSort();
    cout<<"Setelah diurut : "<<endl<<"A = ";
    x.tampil();
    getch();
}

0 Response for the "Shorting with Shel Short"

Post a Comment