Fast sequencing as a sort algorithm in the fighter, has always been the sort algorithm used most of the test is the most of an algorithm
It has been proved that the order of n elements, the optimal algorithm also needs Nlogn time complexity, and the time complexity of fast sorting is Nlogn,
So fast sorting is one of the best algorithms in the sorting algorithm, so let's go on to explore the magic fast sorting algorithm.
The core idea of fast sorting: Each element k is moved to the correct position each time, and the elements less than k are moved to the left of K, the elements greater than k are moved to the right of K
Number of columns to be sorted x[], length n
Algorithm one: Initialize K=x[m] (M is the first element to be sorted), for each element X[i] (i>m), if x[i]>=k,i++; if X[i]<k,swap (X[i],x[++m])
The code is as follows:
voidQsort1 (intLinth) { if(l>=h)return; intm=l; for(inti=l;i<=h;i++) { if(x[i]<X[l]) { inttemp=x[i];x[i]=x[++m];x[m]=X[i]; } } inttemp=x[l];x[l]=x[m];x[m]=X[l]; Qsort1 (L,m-1); Qsort1 (M+1, h);}
The algorithm pair of random integer groups is very effective, but for n the same elements of time, its performance will drop sharply, because each time to spend O (n) to remove an element, the total need to approach O (N2) Time complexity
So the two-way closer algorithm two was born,
voidQsort2 (intLinth) { if(l>=h)return; intt=x[l],i=l,j=h; while(i<j) { while(i<j&&x[j]>t) j--; X[i]=X[j]; while(i<j&&x[i]<t) i++; X[J]=X[i]; } X[i]=T; Qsort2 (L,i-1); Qsort2 (i+1, h);}
The algorithm is optimized for the same elements, each time the position of the current element can be positioned in close to the middle position, which makes the total time close to O (NLOGN) time complexity
The improved algorithm II improves the efficiency of the equivalent sequence, but it is close to the time complexity of O (N2) for the sorted ascending order, because we divide the first element each time
The three ideas of the improved algorithm are as follows: Swap (L,randint (l,h)), Randint (L,H) guarantees that an integer of [l,h] range is generated
int randint (int l,int h) { srand (unsigned (Time (NULL))); int Temp=rand ()% (u+1); return temp>l?temp:h-temp;}
After that, where can the quick sort be improved?
we can find that the fast sort calls a lot of time to sort small sub-arrays, and if you sort these very small sub-data with simple algorithms such as insertion sorting, the program's sub-speed will be faster
if (h-l<cutoff) return
How to determine the value of cutoff is a difficult point, this can be obtained through experiments, such as the n fixed to 100000, and then to cutoff in [1,100] each possible value is run again, compare the run time
The final algorithm four codes are as follows:
intRandint (intLinth) {Srand (Unsigned (Time (NULL))); intTemp=rand ()% (u+1); returntemp>l?temp:h-temp;}voidQSORT4 (intLintHintcutoff) { if(L-h<cutoff)return; intR=Randint (L,H); intt=x[r];x[r]=x[l];x[l]=T; T=X[l]; inti=l,j=h; while(i<j) { while(i<j&&x[j]>t) j--; X[i]=X[j]; while(i<j&&x[i]<t) i++; X[J]=X[i]; } X[i]=T; QSORT4 (L,i-1); QSORT4 (i+1, h);}voidIsort (inth) { for(inti =0; I < H; ++i) {intTemp=x[i];intJ; for(j=i;j>0&&x[j-1]>temp;j--) {X[j]=x[j-1];count++; } X[j]=temp; }}QSORT4 (L,h,cutoff); Isort (h);
, &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &N Bsp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp &NBSP; , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp , &NB Sp &NBSP , &NB Sp
Besides, quick sort.