One of the sorting algorithms-bubble sort, select sort, insert sort

Source: Internet
Author: User

I. Sorting algorithm definition

1. Sorting calculation definition

Sorting algorithm is a kind of algorithm that can arrange a string of data according to a certain order

2. Six kinds of sorting algorithms to understand the way

Imagine when the teacher gave us in line with the height of the method used in the brain to appear in the height of the teacher to the scene in order from low to high for example

  3. Definition of stability

A sorting algorithm is stable when there are two equal key values for the record R and S, and in the original list R appears before s, in the sorted list R will also be before S.

Two. Three basic sorting algorithms

1. Bubble sort: "Move"

Move the highest to the right.

First cycle, find the tallest man on the right. The method is to compare from 22 to the right, moving the two-hour-high one to the far right, and moving to the very top.

The second cycle, in the same way, the person with the higher one moves to the right and the second to the next is the second highest.

And so on, in the rest of the people will be high in the right squeeze, squeeze to the last remaining highest on the far right.

       

 2. Select sort: "Pull"

Take the shortest and put it on the far left.

The first cycle: the first position of the people out of the first position, with the rest of the people compare, if there is shorter than him, will the short pull out and the rest of the comparison, compared to the last to the shortest of the song people pulled out, the shortest man put to the first A location

Second cycle: Take the second man out and compare it to the rest, and compare it to the last one, and put it in the second place.

And so on, each position is placed on the value that should be placed 

Bubble sort is the biggest move to the far right, and the selection sort is the smallest pull-out to the leftmost

   

3. Insert sort: "plug"

The teacher first two students lined up, and then row three height, because the front two people have been lined up, as long as the third person inserted into the appropriate position, three people on the order, and then put the fourth person into the appropriate position, the first four people lined up, and so on

The value that will be added when the loop is finished is inserted in the correct position.

    

    

    

 

One of the sorting algorithms-bubble sort, select sort, insert 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.