Go Bubble sort (Bubble sort)

Source: Internet
Author: User

Bubble sort

Bubble sort (Bubble sort) is the simplest of the sorting algorithms

It's usually when we get to know a new language. Practiced hand use

Today is no exception, although written in C # countless times the bubble sort, but after all changed a language, so it is necessary to come true once

Principle

1. Bubbling

Since the decision to write a record, then you must write well

We say bubble sort is simple, so why is it simple? Because it's easy to understand.

Bubbling Bubble, as it were, was like a bubble, and he slowly surfaced from the bottom of the water.

So this layer of the surface is the way we do bubbles sort of implementation

2. Principle

Suppose we have such an array:

7 2 9 22 16 93 202 0 33 29 84

It's a total number.

Let's get the numbers up and down first.

7[ 0 ] 2[ 1 ] 9[ 2 ] 22[ 3 ] 16[] 4 93[ 5 ] 202[ 6 ] 0[ 7 ] 33[ 8 ] 29[ 9 ] 84[ 10 ]

Here we first assume that we want to sort the arrays from small to large

So first:

    • The smallest number should be in the first place.

So how do you keep the smallest number in the first place?

In fact, who is the little man who came to the truth

3. Ideas

1. We compare the first digit [ 0 ] 7 to the rear number, until a 7 smaller number appears, and the number
and 7 swap Locations

Such as: the second position [ 1 ] 2 is 7 smaller than, then [ 0 ] position from 2 occupy, 7 temporary second place [ 1 ]

2. At this point the array [ 0 ] is 2 , followed by 1.

3. When [ 7 ] 0 , meet the exchange conditions again, 2 with the 0 swap position

4. The final confirmation 0 is the minimum value, then the first place confirmation is0

5. Next confirm the second digit [ 1 ] value ... Repeat the above steps

Code

// 冒泡func Bubble_Sort(slice []int/* 带排序的切片 */, order bool/* true : 正序; false : 倒序; */) {    /*        1. 本循环代表 依次与其后所有数作比较的索引        2. 由于最后一个数不用比较,所以 len(slice) - 1    */    for i := 0; i < len(slice) - 1; i++ {        // 本循环代表 外层将要与哪一位作比较        for k := i +1; k < len(slice); k++ {            if (slice[i] > slice[k] && order) /* 从大到小 */ || (slice[i] <= slice[k] && !order ) /* 从小到大 */ {                slice[i], slice[k] = slice[k], slice[i]            }        }    }}

Results

Sort results

Related Article

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.