#include <iostream>
#include <cstdio>
using namespace std;
void cetak(int *array,int length) //print array elements
{
int i=0;
for(i=0;i<length;i++)
cout<<array[i] << " " ;
cout << endl;
}
//Function to determine the partisis
// partisis the array and returns the middle subscript
int partisi(int *array, int top, int bottom)
{
int x = array[top];
int i = top - 1;
int j = bottom + 1;
int temp;
do
{
do
{
j--;
}while (x >array[j]);
do
{
i++;
} while (x <array[i]);
if (i < j)
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}while (i < j);
return j; // returns middle subscript
}
void quickSort(int *num, int top, int bottom)
{
// top = subscript of beginning of array
// bottom = subscript of end of array
int middle;
if (top < bottom)
{
middle = partisi(num, top, bottom);
quickSort(num, top, middle); // sort first section
quickSort(num, middle+1, bottom); // sort second section
}
return;
}
int main()
{
int a[]={9,6,5,23,2,66,14,8,2,7,1,8}; // array to sort
cetak(a,12); // print elements
quickSort(a,0,12); //call to bubble sort
cetak(a,12); // print elements
return 0;
}
Tidak ada komentar:
Posting Komentar