Quick Sort C language implementation

Source: Internet
Author: User

Quick Sort

First find a number as the Datum point, the number is greater than the datum point to the right, the number less than or equal to the datum point on the left, and then use recursion, on the left and right side of the datum point to do the same operation, when the number of recursive entry only one can stop recursion.

How do I put the number that is less than or equal to the datum point to the left and the number above the datum point to the right?

For convenience, select the first number of the array as a datum point.

Use two sentinels, starting from both sides of the array, and finding the non-qualifying points to stop immediately. When two Sentinels stop, the two points corresponding to the Sentinel are exchanged.

When two sentinels meet, the corresponding number of datum points and Sentinels is exchanged.

It can be seen that the quick sort is at worst the time complexity is O (N2), but it is much faster than bubbling sort, because the bubble sort is just swapping the points on both sides of the adjacent bit fast sort but the jumping exchange.

1#include <stdio.h>2#include <stdlib.h>3 inta[8]={1,3,3, the, at,214,5,3};4 voidQuicksortintStartintOver )5 {6     intkey=A[start],change;7     intx=start,y=Over ;8     if(x>=y)return;9      while(1)Ten     { One         if(a[y]>key) Ay--; -         Else if(a[x]<=key) -X + +; the         Else -         { -Change=A[x]; -a[x]=A[y]; +a[y]=Change ; -         } +         if(x==y) Break; A     } ata[start]=A[x]; -a[x]=key; -Quicksort (start,x-1); -Quicksort (x+1, over); - } - intMain () in { -     inti; toQuicksort0,7); +      for(i=0;i<8; i++) -printf"%d", A[i]); the     return 0; *}

Quick Sort 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.