Learn a little bit of programming every day

On the morning of January 16, a press conference was held by the Ministry of Education to introduce information about the General High School Curriculum Program and Curriculum Standards for Languages and Other Subjects (2017 Edition). In this reform of the "New Curriculum". Officially, artificial intelligence, Internet of Things, big data processing, algorithms, and open source hardware project design are officially classified as part of the new curriculum

That said, starting in the fall of 2018, the high school curriculum will be more up to date with artificial intelligence, big data, and all of that being the future, and algorithms being even more fundamental to computer thinking.

someday (in the future), Let me explain to you the various algorithms in computers。 In the programming, There are millions of algorithms, But the most basic and best implemented, That's what we're talking about today.: bubbling algorithm。

bubbling algorithm

Many people may wonder what kind of algorithm the bubbling algorithm actually is.

It's like a bubble that bubbles up in the water because it contains so much air that it gets pushed all the way to the surface.

The bubbling algorithm is actually a relatively uncommon permutation method in real life, but is most easily implemented in computer languages.

Use bubble sort to have the members of the Dawn organization in a row from left to right in order from shortest to tallest

The bubbling algorithm idea: compare two adjacent numbers in sequence, putting the smaller number in front and the larger number behind.

Question 1: Three numbers, sorted by the bubbling algorithm, lead to the following situation.

Round 1.

5 and 1 are compared and 5 is greater than 1. The array becomes {1,5,6};

Since 5 and 1 have swapped places, continue to compare the magnitude of 5 and the number to the right;

5 and 6 are compared, 5 is smaller than 6, the position remains the same, the array is

Second round.

5 and 6 are compared, 5 is smaller than 6, the position remains the same, the array

Why did 5 and 6 compare two rounds?

Because in bubble sort, the first sort starts with the first number; while the second sort starts with the second number and compares it all the way to the end.

You may say, that's too simple, so let's make it more complicated. Given an array, perform a bubble sort.

First sort

comparing 1 and 7, 1 is smaller than 7, so the position of 1 remains the same and the array is.

Comparing 7 and 5, 7 is larger than 5, so 7 and 5 swap places and become ;

Comparing 7 and 4, 7 is larger than 4, so 7 and 4 swap places and become ;

The first sort is over, and some might say, 5 is bigger than 4. That's not a scientific bubble sort!

Because the bubble sort is not over, we just sorted from the first place to compare, and now we have to compare the second number.

Second round of sorting

Comparing 5 and 4, 5 is larger than 4, so 5 and 4 swap places and become ;

Comparing 5 and 7, 5 is smaller than 7, so the positions of 5 and 7 remain the same and the array is ;

It can be seen that the second trip of the sort is less than the first trip of the sort by comparing the first step 1 and 5 The bubble sort expels a maximum (small) as the first number for each sort

Therefore, we are not finished with our bubble sort, the

Third round of sorting.

Comparing 5 and 7,5 is smaller than 7, so the positions of 5 and 7 remain the same and the array is

Since there are only 4 numbers in total, there is no way for 7 to continue with the numbers that follow, so the bubble sort ends here.

A bubble sort is one such algorithm that will pick a first number (largest or smallest) for each round of sorting, and then perform an array length-1 round of sorting.

Of course in the program, there is no need to do the bubble sorting operation yourself, just write the program code to have the numbers sorted automatically! All you need to understand is the principle of this algorithm.

1、621VRscan When the Red Zygons finish over 100 millionpreA round of financingnbspValve New models coming soonKnuckles controller
2、Vue 1 Build a personal admin backend from 80
3、Array slicing in the go language maverick mutable arrays
5、Monitoring alarm settings for business logs

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送