Hi

It seems that your algorithm for quick sort is wrong. The following is the algorithm for quick sort. I hope this helps.

Best regards

Chong

Code:

+++++++++++++++++++++++++++++++++++++++++
// quicksort.cpp
//
// This program shows how to use Quicksort to sort arrays.
// Note that pivot in the program is nothing more than a guide!! It
// is used just for a comparison!!
//
#include <stdio.h>
#include <string.h>
#include <conio.h> //for getche()
#include <windows.h>
#include <winbase.h>
#define swap(x,y) { int t; t=x; x=y; y=t;}
#define order(x,y) if (x > y) swap(x,y)
#define O2(x,y) order(x,y)
#define O3(x,y,z) O2(x,y);O2(x,z);O2(y,z)
typedef enum {yes, no} yes_no;
static yes_no find_pivot(int *left, int *right, int *pivot_ptr);
static int *partition(int *left, int *right, int pivot);
void quicksort(int *left, int *right);
main( )
{
int *p,pivot;
int age[5] = {7,2,5,8,9};
int len = sizeof(age)/sizeof(int);
quicksort(age, age+len-1);
for (int k=0; k<len; k++) {
printf("[Age] %d\n", age[k]);
} // for
getche();
} // main
void quicksort(int *left, int *right)
{
int *p;
int pivot;
if (find_pivot(left,right,&pivot) == yes) {
p = partition(left,right,pivot);
quicksort(left, p-1);
quicksort(p, right);
} // if
} // quicksort
static yes_no find_pivot(int *left, int *right, int *pivot_ptr)
{
int a,b,c,*p;
a = *left;
b = *( left + (right-left)/2 );
c = *right;
O3(a,b,c);
if (a < b) {
*pivot_ptr = b;
return (yes);
} // if
if (b < c) {
*pivot_ptr = c;
return(yes);
} // if
for ( p = left+1; p <= right; p++) {
if (*p != *left) {
*pivot_ptr = (*p < *left) ? *left: *p;
return(yes);
} // if
} // for
return(no);
} // find_pivot
static int *partition(int *left, int *right, int pivot)
{
while (left <= right) {
while (*left< pivot) ++left;
while (*right >= pivot) --right;
if (left < right) {
swap(*left, *right);
++left;
--right;
} // if
} // while
return(left);
} // partition