#include <iostream>
using namespace std;
const int ukuran_input = 5;
void cetak(int *A)
{
for ( int i = 0; i < ukuran_input; i++ )
cout << A[i] << " ";
cout << endl;
}
int partisi(int* A, int i, int j)
{
int pivot = A[j];
while ( i < j )
{
while ( A[i] < pivot )
i++;
while ( A[j] > pivot )
j--;
if ( A[i] == A[j] )
i++;
else if ( i < j )
{
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
}
return j;
}
void quicksort(int* A, int i, int j)
{
if ( i < j )
{
int k = partisi(A, i, j);
quicksort(A, i, k-1);
quicksort(A, k+1, j);
}
}
int main()
{
int A [ukuran_input] = {3, 2, 1, 4, 8};
// cout << "Input: ";
cetak(A);
quicksort(A, 0, 5);
cout<<endl;
cout << "==> ";
cetak(A);
system("PAUSE");
return EXIT_SUCCESS;
}
No comments:
Post a Comment