#include <stdio.h>
#include <stdlib.h>
#define MAX 10
void swap(int array[MAX], int index1, int index2) {
int temporary = array[index1];
array[index1] = array[index2];
array[index2] = temporary;
}
int partition(int array[MAX], int left, int right, int pivot)
{
int leftBound = left;
int rightBound = right - 1;
while (1)
{
while (array[leftBound] <= pivot && leftBound != rightBound)
leftBound++;
while (array[rightBound] >= pivot && rightBound != leftBound)
rightBound--;
if (rightBound <= leftBound)
break;
else {
swap(array, leftBound, rightBound);
}
}
swap(array, leftBound, right);
return leftBound;
}
void quickSort(int array[MAX], int left, int right)
{
if (right - left <= 0)
return;
else
{
int pivot = array[right];
int partitionResult = partition(array, left, right, pivot);
quickSort(array, left, partitionResult - 1);
quickSort(array, partitionResult + 1, right);
}
}
void main()
{
int array[] = {5, 7, 10, 3, 1, 6, 9, 4, 2, 8};
int length = sizeof(array) / sizeof(int);
quickSort(array, 0, length - 1);
for (int i = 0; i < MAX; i++)
{
printf("%d ", array[i]);
}
}
To embed this project on your website, copy the following code and paste it into your website's HTML: