/*
*filename: quicksort.c
*discription: the array data structure, to take any value as a benchhark for comparison
* (first element), larger than it is to one side, one side smaller than it.
* then, here two arrays and then repeat the process in.
*/
#include <stdio.h>
#define N 9
void QuickSort( int a[], int low, int high );
int main(void)
{
int array[N] = {2, 8, 9, 0, 3, 7 , 2, 4, 6};/*test array*/
int i = 0;
QuickSort(array, 0, N - 1);
printf("Sort completed\n");
/*output*/
while(i < N){
printf("%d ", array[i]);
i++;
}
printf("\n");
return 0;
}
void QuickSort(int *array, int low, int high)
{
int i = low, j = high; //low represent array bottom, high represent array end
int temp = array[low]; //temp for the base
while(i < j){ // exports conditions: i >= j
while(i < j && temp <= array[j])
j--; //in the right scan
if(i < j){
array[i] = array[j];
i++;
}
while(i < j && temp > array[i])
i++; //in the left scan
if(i < j){
array[j] = array[i];
j--;
}
}
array[i] = temp; //the base value in the middle
//recursively divided into two arrays in the continued sweep
if(low < i) QuickSort(array, low, i - 1);
if(i < high) QuickSort(array, j + 1, high);
}
|