Several sorting algorithms

Source: Internet
Author: User

1. Bubble sort: Compare before and after two adjacent elements, if greater than the exchange, so that each iteration of an array, the largest data sink to the N-1 position of the array.

Two kinds of improvements: a. If the current traversal does not occur, the interchange is already in order; b. Record the location of the last data exchange I, the data after this position is clearly already in order, then the next traversal only need to bubble the data before I.

voidBUBBLESORT3 (intA[],intN) {    intJ, K; intFlag; Flag=N;  while(Flag >0) {k=Flag; Flag=0;  for(j =1; J < K; J + +)            if(A[j-1] >A[j]) {Swap (a[j-1], a[j]); Flag=J; }    }}

Bubble sorting is less efficient and can be used when the data is small.

2. Direct insertion sorting is the insertion of data into an already sequenced array, and the direct selection of the sort is to select a minimum from the array that is not sorted later, append to the previous sort.

http://blog.csdn.net/morewindows/article/details/6665714

Several sorting 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.