# 快速排序算法实现

//QSort.h

// Interface of the Quick Sort
#define NUM 2000  //设置最多个数为1999个
class QSort
{
int data[NUM];
public:
int m_DataNum;
public:
void getData();
int  doPartition(int low,int high);  //返回轴的位置
void QuickSort(int low,int high);
void Output();
};

//QSort.cpp

#include "QSort.h"
#include "iostream.h"
#include "fstream.h"

void QSort::getData()
{
int data_num=0;
ifstream infile;
infile.open("qsort_data.txt");
if (!infile)
{
cout<<"' qsort_data.txt 'File not exists!"<<endl;
}
else
{
while (!infile.eof())
{
data_num++;
infile>>data[data_num];
}
m_DataNum=data_num;
}

}
int QSort::doPartition(int low,int high)
{
int pivot;
data[0]=data[low];
pivot=data[0];
while (low<high)
{
while ((low<high)&&(pivot<=data[high])) high--; //显然如果改为while ((low<high)&&(pivot>=data[high])) high--; 则是递减（下面也要改）
data[low]=data[high];
while ((low<high)&&(pivot>=data[low])) low++;  //while ((low<high)&&(pivot<=data[low])) low++;

data[high]=data[low];
}
data[low]=pivot;
return low;
}
void QSort::QuickSort(int low,int high)
{
int pivot_pos;
if (low<high)  //注意这个条件
{
pivot_pos=doPartition(low,high);
QuickSort(low,pivot_pos-1);
QuickSort(pivot_pos+1,high);
}
}
void QSort::Output()
{
for (int i=1;i<=m_DataNum;i++)
cout<<data[i]<<" ";
cout<<endl;
}
void main()
{
QSort q;
q.getData();
q.QuickSort(1,q.m_DataNum);
q.Output();
}