// QuickSort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "stdio.h"
void Swap(int * pValue1, int * pValue2)
{
 int temp = *pValue1;
 *pValue1 = *pValue2;
 *pValue2 = temp;
}
//
//Parameters: a is array, sPoint is start point, ePoint is end point
//Function: make the array to two parts, one is small part, the other is big one
//          all points in small part is smaller than each one in the big part
//          ok, and the third part is the eValue and after this function, it is
//          the value of the middle point.
//Return: and the i+1 is the middle point, which is the return value
//
int Partion(int * a, int sPoint, int ePoint)
{
 int eValue = a[ePoint];
 int i = sPoint -1;
 int j = 0;
 for(j = sPoint; j < ePoint; j++)
 {
  if(a[j] <= eValue)
  {
   i = i + 1;
   Swap(&a[i], &a[j]);
  }
 }
 Swap(&a[i+1], &a[ePoint]);
 return i+1;
}
//
//when the start point smaller than the end point to make array to two part
//and quicksort the two part of the array
//
void QuickSort(int * a, int sPoint, int ePoint)
{
 if(sPoint < ePoint)
 {
  int mPoint = Partion(a, sPoint, ePoint);
  QuickSort(a, sPoint, mPoint-1);
  QuickSort(a, mPoint+1, ePoint);
 }
 return;
}

int main(int argc, char* argv[])
{
 int i = 0;
 int array[10] = {3, 7, 4, 9, 1, 2, 6, 5, 8, 6};
 QuickSort(array, 0, 9);
 
 printf("array sort is \n");
 for(i = 0; i < 10; i++)
 {
  printf("%d ",array[i]);
 }
 printf("\n");
 return 0;
}