Fast sequencing of basic algorithms

Source: Internet
Author: User

The insertion sort is the most basic sort method, with a time complexity of O (Nlogn). High efficiency, and in the written interview is often asked, to write more practice to be able to readily write the goal of the fast row.

The quick sort is a sort of division exchange proposed by C.r.a.hoare in 1962. It adopts a strategy of division, which is usually referred to as the division method (Divide-and-conquermethod).

The basic idea of this method is:

1. First, a number is taken from the series as the base number. (Here we select the first element of the array each time)

2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.

3. Repeat the second step for the left and right intervals until there is only one number for each interval.

Post an easy-to-understand link: http://blog.csdn.net/morewindows/article/details/6684558

The simple implementation of the insertion in the C language is sorted as follows:

1 /*C language implementation of fast sorting algorithm*/2 /*S is the array to be sorted, and L is the starting position to sort the array ,*/3 4 voidQuick_sort (intS[],intLintR)5 {6     if(l<r) {7         inti = l, j = r, x =S[l];8          while(i<j)9         {Ten              while(i<j&&s[j]>=x) { Onej--; A             } -             if(i<j) { -s[i++] =S[j]; the             } -              while(i<j&&s[i]<=x) { -i++; -             } +             if(i<j) { -s[j--] =S[i]; +             } A         } atS[i] =x; -Quick_sort (S, l, I-1); -Quick_sort (S, i+1, R); -     } -}

A simple test case with the following code:

#include <stdio.h>#defineMAXSIZE 10/*C language implementation of fast sorting algorithm*//*S is the array to be sorted, and L is the starting position to sort the array ,*/voidQuick_sort (intS[],intLintR) {    if(l<r) {inti = l, j = r, x =S[l];  while(i<j) { while(i<j&&s[j]>=x) {J--; }            if(i<j) {S[i++] =S[j]; }             while(i<j&&s[i]<=x) {i++; }            if(i<j) {S[j--] =S[i]; }} S[i]=x; Quick_sort (S, l, I-1); Quick_sort (S, I+1, R); }}intMain () {intA[maxsize]; inti; printf ("Please input the num:\n");  for(i=0; i<maxsize; i++) {scanf ("%d",&A[i]); } printf ("before the sort:\n");  for(i=0; i<maxsize; i++) {printf ("%d", A[i]); } printf ("\ n"); Quick_sort (A,0, MAXSIZE); printf ("After the sort:\n");  for(i=0; i<maxsize; i++) {printf ("%d", A[i]); } printf ("\ n");}
View Code

Fast sequencing of basic algorithms

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.