Algorithm Learning ~ Divide and conquer method ~ Quick Sort

Source: Internet
Author: User

Every day in the sort. is simply to give this chaotic world a right order. But then. Sorting also has a fast and slow point,

Bubble Sort method, the idea is very simple is to go up daleitai, this time complexity is very bad, O (n^2) o is a constant, O (n^2) is the maximum limit (value) N (n-1)/2 = (n^2-n)/2 The maximum value is still n^2 two cases are the same.

Select the sorting method, and each trip selects the smallest (or largest) element from the data element to be sorted, placing it in the first (last) Order of the ordered sequence, until all the data elements are sorted out. Select Sort is an unstable sort party time complexity of O (n), and the worst case is still O (n^2).

Sorting is better, and this sort of Logn-level method,

First on the code, the digits can first look at,. Generally the first time will not be very clear. Here's an explanation:

1 //Quick Sort2#include <stdio.h>3 intMain ()4{voidQuick_sort (intS[],intLintR);5     inta[10001],i,x;6     while(~SCANF ("%d",&x))7{ for(i=0; i<x;i++)8scanf"%d",&a[i]);9Quick_sort (A,0, X-1);//The first parameter is the array address, then the interval that needs to be sorted, and I is the lengthTen     for(i=0; i<x;i++) One     { Aprintf"%d", A[i]);//re-order output -     } -printf"\ n"); the    } - } - voidQuick_sort (intS[],intLintR//returns the position of the adjusted base number - { +     if(L < R)//as long as the starting point is less than the end -     { +         //Swap (S[l], s[(l + R)/2]);//swap this number and first number in the middle to see note 1 A         inti = l, j = r, x = S[l];//given the initial value, I is the starting point J is the end point//S[l] is the current value at          while(i<j) -         { -              while(i<j&&s[j]>=x)//from right to left find the first number less than X, starting point -j--;//find the first one that is less than the value of the loop J is the one that is currently less than -             if(i<j) -S[I++]=S[J];//put S[j] into s[i], S[j] formed a new pit, i++ in              while(i<j&&s[i]<x)//find the first number greater than or equal to x from left to right -i++; to             if(i<j) +S[j--]=s[i];// -         } theS[i] = x;//when exiting, I equals J. Fill in the hole with X.  *Quick_sort (s,l,i-1);//recursive calls to the first and second half segments.  $Quick_sort (s,i+1, R);Panax Notoginseng      } -}

Algorithm Learning ~ Divide and conquer method ~ Quick Sort

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.