Fast sorting algorithm

Source: Internet
Author: User
Tags sorts

In front of the blog has written the English version of the Quick Find implementation examples, in the review, with a new understanding.

Thought

The key to the fast sorting algorithm is to first select a number in the array, then divide the number in the array into two parts, move to the left of the array smaller than the selected number, and move to the right of the array with a larger number than the selection.

"Example description"

Fast sort array data[] = {2,4,9,3,6,7,1,5}, set the first number as the base value, compare the other elements to it, the array length is 8, set two pointers i,j respectively point to the first subscript of the array to be sorted, to be compared to move:
First set of sorts:
Base value basis = data[0] = 2 The array is divided into the order of adjustment, so that less than 2 is on its left, greater than 2 on its right, the concrete realization of the idea, first to Data[j] operation, when data[j]> basis, J minus 1, moving forward one, Otherwise the value of data[j] will be filled data[i],data[j] will become the next object to be filled, I at this time move backward one lattice, and start the operation of Data[i], if the data[i]< basis,i plus 1, move backward one, otherwise it will data[i] The value fills at this time the data[j],data[i] becomes the object to be filled, J moves forward, repeating the previous steps, until I = j, stating that all is relatively finished, filling the position of this time data[i], so that can be divided by the baseline value of the left is less than basis, The right is greater than the basis case. The process is as follows:

This group is sorted with basis = Data[i],i = 0,j = 7, sorted after the resulting array {1,2,9,3,6,7,4,5}
Second set of sorts: i = 0, j = 0, basis = data[i] = 1, sorted after the resulting array {1,2,9,3,6,7,4,5}
Third group sort: i = 2, j=7, basis = data[i] = 9, sorted after the resulting array {1,2,5,3,6,7,4,9}

Fourth group sort: i=2,j=6,basis = data [2] = 5, sorted after the resulting array {1,2,4,3,5,7,6,9}

Fifth group sort: i = 2,j = 3,,basis = data[2] = 4, sorted after the resulting array {1,2,3,4,5,7,6,9}

Fifth group sort: i = 5,j = 6,basis = data[5] = 7, sorted after the resulting array {1,2,3,4,5,6,7,9}

"Test Code"

#include <stdio.h>voidquicksort (int Data[],int lo,int hi]{if(Lo>=hi) return;    int I=lo, J=hi; int basis = data[lo];while (I < J) {while ( Data[j] > Basis) &&(i<J))J-- ;        if(I&LT;J)//This piece must have this judgment, otherwise it will be wrong { Data[i++] = data[j];} while ( Data[i] < basis) &&(i<J))i++;if(I&LT;J) { Data[J--] = data[i]; }    } Data[i] = basis;Quicksort data, lo, i-1);Quicksort Data, i+1, HI);}intMain () {int i; Int data[] = {2,4,9,3,6,7,1,5};Quicksort Data , 0, 7);for (i=0;i<8; i++) printf ("%d", Data[i]);Return0;}

Output

Fast 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.