Sort algorithm (Bubble sort, select sort, insert sort)

Source: Internet
Author: User

First, bubble sort:

1, Algorithmic thinking:

For the data to be sorted, compare the two adjacent numbers from top to bottom and adjust them, moving the largest number downward and the smaller number upward. That is: Each trip compares adjacent two data elements in turn, placing the smaller numbers on the left, looping the same operation until all the data elements to be sorted are finished.

2. Example Analysis:

For example: We have to stand in a row of 10 people of varying height, asking them to line up in height from low to high, set 10 person numbered 0---9, the adjacent two persons in turn compare, if the left side is higher than the right person, then exchanges two person's position, otherwise does not exchange, until finally two person, namely completes a trip sort at this time. After a trip, the tallest person is already on the far right.

The result of a sort of order: (move to the last position after ranking the highest person)

So many times the sort, and finally finished the whole sort.

3, Algorithm analysis: (have small to large sort)

1>, each trip in the process, is to compare two consecutive numbers, if a[i]>a[i+1], then exchange two number, otherwise do not change;

2>, every trip is a heavy cycle, and because to go through the process, that is, there is a cycle outside, so there is a double cycle.

4, Algorithmic code: (Java version) [Java]   View plain copy print?/**   *  bubble sort   algorithm    *  @author  xcbeyond   *   */      public class  bubblesort {          public static void main (String [] args)  {           // TODO  auto-generated method stub           int a[]  ={13,15,37,89,60,39,12,109,56,72} ;  

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.