First, it is explained that quick sorting is an improvement for Bubble sorting. Why? Let's take a look at the Bubble sorting, which divides the sequence into two parts: the first half is unordered, the second half is in ascending order, and the second half is greater than the first half.
In this way, we can get some similarities between quick sorting and Bubble Sorting:
- They all need to go through N-byte sorting
- Each sort goes through the O (n) comparison.
- The elements in the second half are larger than those in the first half.
The difference is that the exchange operation of the bubble sort occurs between adjacent elements, that is, one sort can undergo multiple exchange operations; the exchange operation of quick sorting takes place between two elements with a long interval. One Sort operation takes less time.
The recursive and non-Recursive Implementation Code of quick sorting is given below:
?
#include<iostream>#include<vector>#include<stack>#include<cstdlib>#include<algorithm>usingnamespace std; /** Divide the array into two parts. the left part of the Axis worker is smaller than the right part of the Axis **/template<typename Comparable> int partition(vector<Comparable> &vec,intlow,int high){ Comparable pivot=vec[low]; // Select any element as the axis. Select the first element here. while(low while(low high--; vec[low]=vec[high]; while(low low++; vec[high]=vec[low]; } // At this time, low = high vec[low]=pivot; returnlow; } /** Use recursive quick sorting **/template<typenameComparable> voidquicksort1(vector<Comparable> &vec,intlow,int high){ if(low intmid=partition(vec,low,high); quicksort1(vec,low,mid-1); quicksort1(vec,mid+1,high); }} /** Use non-recursive quick sorting of stacks **/template<typenameComparable> voidquicksort2(vector<Comparable> &vec,intlow,int high){ stack<int> st; if(low intmid=partition(vec,low,high); if(low<mid-1){ st.push(low); st.push(mid-1); } if(mid+1 st.push(mid+1); st.push(high); } // In fact, it is to use the stack to save the subscript of the first and end elements of each sub-string to be sorted. This range is taken out in the next while loop and the partition operation is performed on this sub-series. while(!st.empty()){ intq=st.top(); st.pop(); intp=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(){ intlen=1000000; vector<int> vec; for(inti=0;i<len;i++) vec.push_back(rand()); clock_tt1=clock(); quicksort1(vec,0,len-1); clock_tt2=clock(); cout<<"recurcive "<<1.0*(t2-t1)/CLOCKS_PER_SEC<<endl; // Re-disrupt the order 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; return0; } |
[Email protected]: ~ G ++ quicksort. cpp? Oqsorisun @ zcypc: ./Qs
Recurcive 0.38.
None recurcive 0.47
We can see that non-recursive algorithms are slower than recursive algorithms. The following explains why.
The stack used by the recursive algorithm is automatically generated by the program. The stack contains parameters used for function calling and local variables in the function. If many local variables or other functions are called inside the function, the stack will be very large. Each recursive call requires a large stack, and the efficiency will naturally decrease.
For non-recursive algorithms, each cycle uses its pre-created stack, so no matter the complexity of the program, it will not affect the program efficiency.
For the above fast sorting, because the local variable has only one mid and the stack is small, the efficiency is not lower than that of non-Recursive Implementation.