快速排序(递归和非递归)


原文:http://www.oschina.net/code/snippet_54100_8734
http://www.cnblogs.com/zhangchaoyang/articles/2234815.html
#include <iostream>
using namespace std;
int partition( int *a, int l, int h){
  int x = a[l];
  int i = l;
  int j = h+1;
  int temp;
  while (i<j){
   while (a[++i]<x&&i<h);
   while (a[--j]>x);
   if (i<j){
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
   }
  }
  a[l] = a[j];
  a[j] = x;
  return j;
}
void qsort ( int *a, int l, int h){
  if (l>=h)
   return ;
  int q = partition(a, l, h);
  qsort (a, l, q-1);
  qsort (a, q+1, h);
}
int main(){
  int a[9] = {9,8,7,6,5,4,3,2,1};
  qsort (a,0,8);
  for ( int i=0; i<9; i++)
   cout<<a[i]<<endl;
  return 0;
}

非递归算法 跳至 [1] [2]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//数据规模很大时,递归的算法很容易导致栈溢出,改为非递归,模拟栈操作,最大长度为n,每次压栈时先压长度较大的,此时栈深度为logn。
#include <iostream>
using namespace std;
int partition( int *a, int l, int h){
  int x = a[l];
  int i = l;
  int j = h+1;
  int temp;
  while (i<j){
   while (a[++i]<x&&i<h);
   while (a[--j]>x);
   if (i<j){
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
   }
  }
  a[l] = a[j];
  a[j] = x;
  return j;
}
void qsort ( int *a, int l, int h)
{
  if (l>=h)
   return ;
  int *s = new int [h-l+1];
  int p = 0;
  s[p++] = l;
  s[p++] = h;
  int low,high,q;
  while (p>0){
   high = s[--p];
   low = s[--p];
   if (low>=high)
    break ;
   q = partition(a, low, high);
   if (q-low > high-q){
    s[p++] = low;
    s[p++] = q-1;
    if (high > q){
     s[p++] = q+1;
     s[p++] = high;
    }
   } else {
    s[p++] = q+1;
    s[p++] = high;
    if (q > low){
     s[p++] = low;
     s[p++] = q-1;
    }
   }
  }
  delete []s;
 
}
int main(){
  int a[9] = {9,8,7,6,5,4,3,2,1};
  //int a[9] = {1,2,3,4,5,6,7,8,9};
  qsort (a,0,8);
  for ( int i=0; i<9; i++)
   cout<<a[i]<<endl;
  return 0;
}
[2]. [代码] 非递归算法  跳至  [1]  [2]
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//数据规模很大时,递归的算法很容易导致栈溢出,改为非递归,模拟栈操作,最大长度为n,
//每次压栈时先压长度较大的,此时栈深度为logn。
#include <iostream>
using namespace std;
int partition( int *a, int l, int h){
  int x = a[l];
  int i = l;
  int j = h+1;
  int temp;
  while (i<j){
   while (a[++i]<x&&i<h);
   while (a[--j]>x);
   if (i<j){
    temp = a[i];
    a[i] = a[j];
    a[j] = temp;
   }
  }
  a[l] = a[j];
  a[j] = x;
  return j;
}
void qsort ( int *a, int l, int h){
  if (l>=h)
   return ;
  int *s = new int [h-l+1];
  int p = 0;
  s[p++] = l;
  s[p++] = h;
  int low,high,q;
  while (p>0){
   high = s[--p];
   low = s[--p];
   if (low>=high)
    break ;
   q = partition(a, low, high);
   if (q-low > high-q){
    s[p++] = low;
    s[p++] = q-1;
    if (high > q){
     s[p++] = q+1;
     s[p++] = high;
    }
   } else {
    s[p++] = q+1;
    s[p++] = high;
    if (q > low){
     s[p++] = low;
     s[p++] = q-1;
    }
   }
  }
  delete []s;
 
}
int main(){
  int a[9] = {9,8,7,6,5,4,3,2,1};
  //int a[9] = {1,2,3,4,5,6,7,8,9};
  qsort (a,0,8);
  for ( int i=0; i<9; i++)
   cout<<a[i]<<endl;
  return 0;
}



首先说明一下快速排序是对冒泡排序的改进。为什么这么说呢?想一下冒泡排序,它把序列分成了两部分,前半部分无序,后半部分升序排列,并且后半部分的数都大于前半部的数。

由此可得到快速排序和冒泡排序的一些共同点:

  1. 都要经历n趟排序
  2. 每趟排序要经历O(n)次比较
  3. 都是后半部分元素比前半部大

而不同之处就在于冒泡排序的交换操作发生相邻的元素之间,即一趟排序可以要经过多次交换操作;快速排序的交换操作发生在间隔比较远的两个元素之间,一趟排序要经过交换操作次数会少一些。

下面给出快速排序的递归和非递归实现代码:

#include<iostream>
#include<vector>
#include<stack>
#include<cstdlib>
#include<algorithm>
using  namespace  std;
 
/**把数组分为两部分,轴pivot左边的部分都小于轴右边的部分**/
template  < typename  Comparable>
int  partition(vector<Comparable> &vec, int  low, int  high){
     Comparable pivot=vec[low];  //任选元素作为轴,这里选首元素
     while (low<high){
         while (low<high && vec[high]>=pivot)
             high--;
         vec[low]=vec[high];
         while (low<high && vec[low]<=pivot)
             low++;
         vec[high]=vec[low];
     }
     //此时low==high
     vec[low]=pivot;
     return  low;
}
 
/**使用递归快速排序**/
template < typename  Comparable>
void  quicksort1(vector<Comparable> &vec, int  low, int  high){
     if (low<high){
         int  mid=partition(vec,low,high);
         quicksort1(vec,low,mid-1);
         quicksort1(vec,mid+1,high);
     }
}
 
/**使用栈的非递归快速排序**/
template < typename  Comparable>
void  quicksort2(vector<Comparable> &vec, int  low, int  high){
     stack< int > st;
     if (low<high){
         int  mid=partition(vec,low,high);
         if (low<mid-1){
             st.push(low);
             st.push(mid-1);
         }
         if (mid+1<high){
             st.push(mid+1);
             st.push(high);
         }
         //其实就是用栈保存每一个待排序子串的首尾元素下标,下一次while循环时取出这个范围,对这段子序列进行partition操作
         while (!st.empty()){
             int  q=st.top();
             st.pop();
             int  p=st.top();
             st.pop();
             mid=partition(vec,p,q);
             if (p<mid-1){
                 st.push(p);
                 st.push(mid-1);
             }
             if (mid+1<q){
                 st.push(mid+1);
                 st.push(q);
             }      
         }
     }
}
 
int  main(){
     int  len=1000000;
     vector< int > vec;
     for ( int  i=0;i<len;i++)
         vec.push_back( rand ());
     clock_t  t1= clock ();
     quicksort1(vec,0,len-1);
     clock_t  t2= clock ();
     cout<< "recurcive  " <<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl;
     
     //重新打乱顺序
     random_shuffle(vec.begin(),vec.end());
         
     t1= clock ();
     quicksort2(vec,0,len-1);
     t2= clock ();
     cout<< "none recurcive  " <<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl;
     
     return  0;
}

orisun@zcypc:~ g++quicksort.cppoqsorisun@zcypc:   ./qs
recurcive 0.38
none recurcive 0.47

可以看到非递归的算法比递归实现还要慢。下面解释为什么会这样。

递归算法使用的栈由程序自动产生,栈中包含:函数调用时的参数和函数中的局部变量。如果局部变量很多或者函数内部又调用了其他函数,则栈会很大。每次递归调用都要操作很大的栈,效率自然会下降。

而对于非递归算法,每次循环使用自己预先创建的栈,因此不管程序复杂度如何,都不会影响程序效率。

对于上面的快速排序,由于局部变量只有一个mid,栈很小,所以效率并不比非递归实现的低。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值