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


Script

/* quick sort*/
# include <iostream.h>
# include <iomanip.h>
# include <conio.h>

//void quickSort(int[],int);
void q_sort(int[],int,int);

int main ()
{
 int NumList[8]={5,34,32,25,75,42,22,2};
 // int temp;
 cout<<" Data Sebelum diurutkan: \n";
 for(int d=0;d<8;d++)
 {
 cout<<setw(3)<<NumList[d];
 }
 cout<<"\n\n";
 q_sort(NumList,0,8);
 cout<<" Data setelah diurutkan: \n";
 for(int iii=0;iii<8;iii++)
 cout<<setw(3)<<NumList[iii]<<endl<<endl;
 // quickSort(NumList,8);
 getch();
}
/*
void quickSort(int numbers[],int array_size)
{
 q_sort(numbers,0,array_size-1);
}
 */
void q_sort(int numbers[],int left,int right)
{
 int pivot,l_hold,r_hold;
 l_hold=left;
 r_hold=right;
 pivot=numbers[left];
 while(left<right)
 {
 while((numbers[right]>=pivot)&&(left<right))
 right--;
 if(left!= right)
 {
 numbers[left]=numbers[right];
 left++;
 }
 while((numbers[left]<=pivot)&&(left<right))
 {
 left++;
 }
 if (left!=right)
 {
 numbers[right]=numbers[left];
 right--;
 }
 }
 numbers[left]=pivot;
 pivot=left;
 left=l_hold;
 right=r_hold;
 if(left<pivot)
 q_sort(numbers,left,pivot-1);
 if(right>pivot)
 q_sort(numbers,pivot+1,right);
 }

Hasil

ScreenHunter_200 Feb. 22 16.25

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