Quick Sorting Algorithm

Source: Internet
Author: User

Quick sorting is based onSub-governance mode.

I. Basic Ideas:

1. Find a benchmark number;

2. re-arrange the sequence, put the smaller value than the benchmark number in front, and put the larger value than the benchmark number in the back. After the split, the benchmark number is the intermediate number, which is also a split operation;

3. recursively reorder the two sequences separated by the reference number.

Quick sorting isRecursive thinkingClassic application.

This algorithm was proposed by Mr. Tony Karl in 1962.

Ii. decomposition method:

Array a [p... R] is divided into two child arrays that may be null A [p... q-1] <= A [Q] <= A [q + 1 .. R].

Iii. solution:
Perform quick sorting by recursive calling.

Iv. Merge:

Because the two sub-arrays are sorted in place, they do not need to be merged.

5. pseudocode:

Because there are many versions of the Sorting Algorithm, select the version used in introduction to algorithms, that is, use the last element of the array as the principal component for sorting.

QUICKSORT(A,p,r) If(p<r)   Then q←PARTION(A,p,r)              QUICKSORT(A,p,q-1)              QUICKSORT(A,q+1,r) PARTION(A,p,r)x←A[r]i←p-1forj←p to r-1   do if A[j]<=x      then i←i+1         exchange A[i]A[j]exchange A[i+1]A[r]return i+1

6. Source Code implementation:

# Include <stdio. h> # include <stdlib. h> # define Max 8 void Exchange (int * X, int * Y) // the pointer must be used here, otherwise, the parameter value {int temp; temp = * X; * x = * Y; * Y = temp;} of the called function cannot be changed because of function copy ;} int partion (int v [], int P, int R) {int x = V [R]; int I = p-1; Int J; For (j = P; j <= R-1; j ++) {If (V [J] <X) {I ++; Exchange (& V [I], & V [J]) ;}} Exchange (& V [I + 1], & V [R]); return I + 1;} void quicksort (INT V [], int P, int R) {int q = 0; If (P <r) {q = partion (v, P, R); quicksort (V, p, q-1 ); quicksort (v, q + 1, R) ;}} int main () {int V [Max]; for (INT I = 0; I <Max; I ++) scanf ("% d", & V [I]); quicksort (v, 0, MAX-1); For (Int J = 0; j <Max; j ++) printf ("% d \ t", V [J]); System ("pause"); Return 0 ;}


VII. Running result:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/80/wKioL1P7Ng6ii3iiAABwMgHngCY650.png "Title =" Capture. PNG "alt =" wkiol1p7ng6ii3iiaabwmghngcyw.png "/>

8. algorithm Demonstration:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/80/wKioL1P7NrzAtAE2AALviB5DjVs271.jpg "Title =" sss.png "alt =" wkiol1p7nrzatae2aalvib5djvs271.jpg "/> 650) This. length = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/7E/wKiom1P7NdOjZqWQAALYCxPV-FA293.jpg "Title =" dddd.png "width =" 450 "Height =" 158 "border =" 0 "hspace =" 0 "vspace =" 0 "style =" Width: pixel PX; Height: 158px; "alt =" wKiom1P7NdOjZqWQAALYCxPV-FA293.jpg "/>

This article from the "freehand" blog, please be sure to keep this source http://8827835.blog.51cto.com/8817835/1544910

Quick Sorting Algorithm

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.