Quick Sort (summary)

Source: Internet
Author: User

Finally involves a small algorithm ~ ~ Happy is unable to express ~ ~ ~ Now summarize, deepen the impression

Ha ha!! Come on!! I am a acmer.!!! Ha ha!!

-------------------------------------------------------

Sorting is a classic question, basically learning C has been sorted since, like bubbling sort.

However, if you encounter a lot of numbers, it is obvious that the speed advantage of the fast platoon is the most obvious, and it is also the most efficient! Most of the time it saves time and effort. Better yet, the quick line is written as a template, basically want to use basic can call.

--------

For a series of numbers, such as 4 1 7 2 5 8 3 6 9

These nine numbers, if the idea is to use the fast line, first select a ruler, used for digging pits. For example, now take 4 for the ruler, (of course, the middle number is also possible), and then judge one by one, (here in ascending order), encounter than 4 small number,

On the left side of the 4, the large nature goes to the right, and after a round, the sequence becomes the following:

3 1 2 4 5 7 6 9;

Why is this, you can first assume that I=1, J=9 is the beginning and end of the sequence, the first number is assigned, (because to dig a hole, so first to save the first number) and then from the number of J to compare, if encountered than

The first number of small, (such as 3) put 3 to the first number to go, and then the original 3 that position is a new pit, this time from the i++ began to judge, from left to right, looking for more than 4 large number, encountered on the original 3 position, (this is 7)

Then 7 is a new position, and then it is determined from the 8 position, until the i==j, and the end of the round.

Then the method of division and treatment was adopted.

On the left is from 1 to i-1 to the right from i+1 to 9;

Then the iteration continues, and the final order is sorted.

As the article is self-summarizing articles, there are many shortcomings please understand, very happy to work with you to learn, progress!

The code is as follows: (Refer to exercise Hangzhou OJ 1040)

#include <stdio.h>voidQuick_sort (intA[],intLintR) {    if(l<r) {intx; intI=l, J=r; x=A[l];  while(i<j) { while(i<j&&a[j]>=x) J--; if(i<j) A[i++]=A[j];  while(i<j&&a[i]<x) I++; if(i<j) A[j--]=A[i]; } A[i]=x; Quick_sort (A,l,i-1); Quick_sort (A,i+1, R); }}intMain () {intn,m,i,j,k,s,t; inta[ the]; scanf ("%d",&N);  while(n--) {scanf ("%d",&m);  for(k=1; k<=m;k++) scanf ("%d",&A[k]); Quick_sort (A,1, M);  for(i=1; i<m;i++) printf ("%d", A[i]); printf ("%d\n", A[i]); }        return 0;}

  

Quick Sort (summary)

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.