Program C++ Pengurutan (Sorting) dengan Metode Shell Sort


Script

/*Shell sort*/
#include <iostream.h>
#include <conio.h>
int main (void)
{
 int array[5];
 int length=5;
 int i,d;
 int tmp, flag;
for (i=0;i<length;i++)
 {
 cout<<"Enter a number :";
 cin>>array[i];
 }
d=length;
 flag=i;

 while(flag||(d>1))
 {
 flag=0;
 d=(d+1)/2;
 for(i=0;i<(length-d);i++)
 {
 if(array [i+d]>array[i])
 {
 tmp=array[i+d];
 array[i+d]=array[i];
 array[i]=tmp;
 flag=1;
 }
 }
 }
for(i=0;i<5;i++)
 {
 cout<<array[i]<<endl;
 }
 getche();
}

Hasil

ScreenHunter_198 Feb. 22 16.20

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s