// 参考网址:
// http://blog.csdn.net/zuiaituantuan/article/details/5978009 快速排序的优化与详细分析
// http://developer.51cto.com/art/201403/430986.htm 最浅显易懂的讲解
// 实现总结:
// 快速排序的每一轮处理其实就是将这一轮的基数归位,直到所有的树都归位为止,排序就结束了。
// 每一次是将大于和小于基数的两个数对换,直到两个指针ij相遇为止。然后将相遇为止的值和基数对换。
// 如此,就将数列规划为大于基数的在其右边,小于基数的在其左边了。
//
// 1、优化方案:
// 1.1、
// 1.2、
/* 注意:《以由小到大排序为例》
* 1、算法分为两步:其一,选择一个基准元素,一般选取左边第一个元素。其二,分别从基准的下一个元素和最右边的
* 那个元素开始找,《一定要从最右边开始找》,分别在右边找小于基准的元素和在左边找大于基准的元素,《在
* 左边找的是大于基准的元素,在右边找的是小于基准的元素,这里要注意边界问题,是<=和>=,此边界问题很重要》,
* 如果左右两边的指针没有靠拢,就将其交换,这样就把大的放在了右边,小的放在了左边,《交换后,两个指针要保持
* 原来的位置不变》。当两个指针相遇,判断相遇位置的元素是否小于基准元素,小则交换。不管有没有交换,都要记
* 录下这个基准当前的位置,至此,将原来的序列,分割为大于和小于基准元素的两个序列。其三,是递归以上两步,
* 知道最右边的指针和最左边的指针的位置相同。
*/
// 生成1万个整数排序,快速排序要1千多ms,堆排序要1万多ms。确实快速排序是排序算法中最快的。
// 生成1千万个整数排序,快速排序要347000ms,堆排序要242157ms。虽然快速排序是排序算法中最快的,但是随着数据规模的
// 扩大,两者之间的差距已经缩小。
// 生成1千个整数排序,快速排序要0ms,堆排序要16ms。快速排序依然是排序算法中最快的,特别是在小规模数据排序中,其优势
// 相当明显。
#ifndef QUICKSORT_H
#define QUICKSORT_H
#include "Common.h"
int partition(int arr[], int low, int high);
void si_sort( int arr[], int low, int high );
void printArr( int arr[], int arrnum );
int main_Q();
int main_fopen();
template<typename T>
class QuickSort
{
private:
vector<T> v;
public:
QuickSort(vector<T> &v);
void quickSortAlgorithm(int left, int right);
void printVector();
};
void QuickSortCppTest();
#endif
以上是头文件;
以下是CPP文件:
#include "QuickSort.h"
int partition(int arr[], int low, int high)
{
arr[0] = arr[low];
while( low < high )
{
while( (low < high) && (arr[high] > arr[0] ) )
high--;
while( (low < high ) && (arr[low] < arr[0] ) )
low++;
}
arr[low] = arr[0];
cout<<"pivot index is "<< low <<endl;
return low;
}
void si_sort( int arr[], int low, int high )
{
int pivotindex;
if( low < high )
{
pivotindex = partition(arr,low,high);
si_sort(arr,low,pivotindex-1);
si_sort(arr,pivotindex+1, high);
}
}
void printArr( int arr[], int arrnum )
{
int i = 1;
cout<< "sorted sequence: "<<endl;
for( ; i <= arrnum; i++ )
cout<<arr[i]<<" ";
cout<<endl;
}
int main_Q()
{
int index = 1,arrnum;
char s[100], *sp;
int num[10] = {0};
cout<<"Please input(ex:2 45 34 12 34 5 6 )"<<endl;
fgets(s,sizeof(s),stdin);
sp=strtok(s, " " );
while(sp!=NULL)
{
num[index++] = atoi(sp);
sp = strtok(NULL," ");
}
arrnum = index -1;
cout<<"You have input the numbers "<<arrnum<<endl;
si_sort(num,1,arrnum);
printArr(num,arrnum);
system("pause");
return 0;
}
int main_fopen()
{
FILE *stream;
char string[] = "This is a test\n";
char msg[20];
stream = fopen("D:\\desktop\\DUMMY\.txt","w+");
fwrite(string, strlen(string), 1, stream);
fseek(stream, 0, SEEK_SET);
fgets( msg, strlen(string)+1, stream);
printf("%s", msg);
fclose(stream);
system("pause");
return 0;
}
template<typename T>
QuickSort<T>::QuickSort(vector<T> &v)
{
(*this).v = v;
}
template<typename T>
void QuickSort<T>::quickSortAlgorithm(int left, int right)
{
if( left >= right )
return;
T temp = (*this).v[left];
int key = left;
int i = left+1;
int j = right;
while( i < j )
{
while( (*this).v[j] >= temp && j > i ) j--;
while( (*this).v[i] <= temp && j >i ) i++;
if( i != j )
{
::swap<T>( (*this).v[i], (*this).v[j] );
}
}
if( (*this).v[left] > (*this).v[i] )
{
::swap<T>( (*this).v[left], (*this).v[i] );
key = i;
}
quickSortAlgorithm(left, key-1);
quickSortAlgorithm(key+1,right);
return;
}
template<typename T>
void QuickSort<T>::printVector()
{
for( int count = 0; count < (*this).v.size(); count++ )
cout<<(*this).v[count]<<" ";
cout<<endl;
return;
}
void QuickSortCppTest()
{
clock_t tBegin,tEnd;
vector<int> v(1000,8);
for( int i=0; i < v.size(); i++ )
v[i]=rand()%19999;
QuickSort<int> VTest(v);
//VTest.printVector();
tBegin = clock();
VTest.quickSortAlgorithm(0,v.size()-1);
tEnd = clock();
cout<<"The run time of Quick is : "<<tEnd-tBegin<<" ms"<<endl;
//VTest.printVector();
return;
}
代码经过测试,粘贴复制即可运行。