Penerapan Divide and conquer dalam Quick Sort
#include <stdio.h>
int arr[8] = {5,2,7,6,1,8,9,3};
int partition(int low,int high)
{
int i;
int j;
int pivot;
int temp;
pivot = arr[low];
i = low;
j = high;
while(i<j)
{
while(arr[i]<pivot)
{
i++;
}
while(arr[j]>pivot)
{
j--;
}
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
return j;
}
void quicksort(int low,int high) //Divide
{
int j;
if(low<high)
{
j = partition(low,high);
quicksort(low,j);
quicksort(j+1,high);
}
}
int main()
{
quicksort(0,7);
int i;
for(i = 0;i<8;i++)
{
printf("%d",arr[i]);
}
}
Komentar
Posting Komentar