Fast sorting, C language implementation

Source: Internet
Author: User

Sorting method is more famous, the specific algorithm to see:

This blog is easy to say: http://blog.csdn.net/morewindows/article/details/6684558

This is the basis for fast sorting, implemented in code as follows:

voidDiviedsort (int* Arr_p,intStartintend) {    intLeft,right,i=0; intPivot,result; BOOLFlag; Flag=TRUE; Pivot=Arr_p[start]; Right=end; Left=start; #ifDEBUGprintf ("pivot:%d\r\n", pivot); printf ("L:");  for(i=0; i<end+1; i++){        if(i==0) printf ("_ "); Elseprintf ("%d", Arr_p[i]); } printf ("\ r \ n"); #endif     while(Left<right &&flag) {         while(arr_p[right]>=pivot) { Right--; if(Right<=left) {//number of less than pivot is not found on the right to exit the total loopArr_p[left] = pivot;//save pivot to the left pit.Flag =FALSE;  Break; }        }        if(flag) {//find a number less than pivot to move to leftArr_p[left] =Arr_p[right]; Arr_p[right]= +; Left++; if(left>=Right ) {Flag=FALSE; Arr_p[right]= pivot;//when the left shift equals right, the description is just over;            }        }Else{            #ifDEBUGprintf ("Lf:");  for(i=0; i<end+1; i++){                if(arr_p[i]== +) printf ("_ "); Elseprintf ("%d", Arr_p[i]); } printf ("\ r \ n"); #endif             Break; }        #ifDEBUGprintf ("R:");  for(i=0; i<end+1; i++){            if(arr_p[i]== +) printf ("_ "); Elseprintf ("%d", Arr_p[i]); } printf ("\ r \ n"); #endif         while(arr_p[left]<=pivot) { Left++; if(Left>=right) {//left no number found greater than pivot exit Total loopArr_p[right] = pivot;//Save pivot to the right pit .Flag =FALSE;  Break; }        }        if(flag) {//find a number greater than pivot to move to rightArr_p[right] =Arr_p[left]; Arr_p[left]= +; Right--; if(right<=Left ) {Flag=FALSE; Arr_p[left]= pivot;//When right is shifted to left, the description ends exactly .            }        }Else{            #ifDEBUGprintf ("Rf:");  for(i=0; i<end+1; i++){                if(arr_p[i]== +) printf ("_ "); Elseprintf ("%d", Arr_p[i]); } printf ("\ r \ n"); #endif             Break; }        #ifDEBUGprintf ("L:");  for(i=0; i<end+1; i++){            if(arr_p[i]== +) printf ("_ "); Elseprintf ("%d", Arr_p[i]); } printf ("\ r \ n"); #endif    }}
View Code

The operating result is:

This example is quite special. A round of results, but in fact, both sides of the pivot are recursively call this algorithm, to get from small to large order.

The conditions for recursive judgment here are:

Left<right

The left subscript is greater than or equal to the right subscript when the number has been arranged from small to large.

The following is the complete code for quick sorting.

//Quick Sort//Left : The starting bit of the array//right: The end of the array, note that it is not the maximum capacity of the arrayvoidQuickSort (int* Arr,intLeftintRight ) {    intPivotnum =0; intStart,end; if(left<Right ) {Start= Left,end =Right ; Pivotnum=Arr[start];  while(start<end) {            //find a number less than pivotnum from the right             while(Start<end && arr[end]>=pivotnum) {End--;//the right cursor shifts to the left            }            if(start<end) {Arr[start++] =Arr[end]; }            //find a number greater than pivotnum from the left             while(Start<end && arr[start]<=pivotnum) {Start++;//the left cursor shifts to the right            }            if(start<end) {Arr[end--] =Arr[start]; }} Arr[start]= Pivotnum;//Here we are going to save the middle number to the last cursor stop after we get the order according to the Pivotnum comparison .QuickSort (arr,left,start-1); QuickSort (Arr,start+1, right); }}

Verification Code:

intMainintargcChar*argv[]) {    intI=0; intarr[ One] = {6,2,3,9,1,2,7, A,4,1,0}; QuickSort (arr,0,Ten);  for(i=0;i< One; i++) {printf ("%d", Arr[i]); } printf ("\ r \ n"); System ("Pause"); return 0;}

Operation Result:

Finally, the selection of Pivotnum intermediate values can be randomly selected or the median can be gone.

Efficiency issues for quick sequencing:

The time complexity is O (n^2) If the median value is minimum or maximum each time it is determined;

The best case is that the median is exactly the median, so the time complexity is O (NLGN);

Summary: In terms of average efficiency, fast sequencing is based on the key comparison of the fastest, the average efficiency is O (NLGN);

Fast sorting, C language implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.