Kamis, 01 Maret 2012

Tugas kompleksitas & algoritmik

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

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,7,56,2,34,23,57,30,67,32,78,68,45,22,43,28,39,64,15,3,68,32,96,47,35,21,65,43,90,65,45,8,4,34,87,32,65,98,21};

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/5+1;
      for(start=1;start<=step;start++)
             shellsort::InsSort(start,step);
   }
}

void shellsort::tampil(){
for(int a=0;a<20;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();
}

Surya Kharisma Karnefo
10101152630292
IF-5

Tidak ada komentar:

Posting Komentar