quick sort的问题,前面说过了:
1,少量数据的排序效果不好,还不如直接插入;
2,对于特殊数据的处理,比如单边递增的有序序列,会出现极端差的情况;
3,数据量大的话,会让栈溢出。
所以,有了一些性能提高方案,前面说过:
1,少量数据时候用直接插入;
2,选取pivot用三者的中值;
3,去掉递归,使用栈,或者用数组来仿栈
- void qort_no_recursion( T* pTable, UINT start,UINT end) {
- stack theStack;
- theStack.push(start);
- theStack.push(end);
- while ( !theStack.empty()) {
- // get the start and end range for sort
- from = theStack.pop();
- to = theStack.pop();
- if ( from < to) {
- middle = pivot(pTable,from,to);
- // save the new range: two ranges
- if ( middle > from ) {
- theStack.push(from);
- theStack.push(middle - 1);
- }
- if ( to > middle ) {
- theStack.push(middle + 1);
- theStack.push(to);
- }
- }
- }
- }
算法很简单,就是找到一个支点之后,就把两组数据的范围值(from和to)入栈,这样栈里面的数据会逐渐增多;下一次循环只能处理一个range的数据,再次进入两个range。直到进入的逐渐减少,pop的逐渐增多,直到没有。
结束