int n = 7;
int bil[] = {5, 3, 8, 4, 7, 2, 1};
void swap(int first, int second) {
int tampung = bil[first];
bil[first] = bil[second];
bil[second] = tampung;
}
void quickSort(int low, int high) {
int x = low, y = high;
int pivot = bil[low + (high - low) / 2];
while (x <= y) {
while (bil[x] < pivot) {
x++;
}
while (bil[y] > pivot) {
y--;
}
if (x <= y) {
swap(x, y);
x++;
y--;
}
}
if (low < y) {
quickSort(low, y);
}
if (x < high) {
quickSort(x, high);
}
}
int main() {
quickSort(0, n - 1);
int x;
for (x = 0; x < n; x++) {
printf("%d ", bil[x]);
}
return 0;
}
No comments:
Post a Comment