Bubble Sort Verbatim

(1) Introduction of new courses

Classmates, please turn your attention to the big screen. Here are the scores of 10 contestants. How to quickly sort the final scores of the 10 contestants from high to low according to the principle that the higher the score, the higher the ranking? Can any student tell me? I saw that everyone raised their hands enthusiastically, so let's talk together. The students here said to use the ascending or descending order in the Excel table. The students here said to manually sort by two. These two methods can indeed be realized, but if you want to get the sorting results by writing a program, how do you do it? Today, the teacher and students will explore "Bubble Sort" together to find the answer.

(2) Lectures on new courses

The teacher made an animation to show the sorting process of 26, 35, 37, and 10 from small to large. The students come to watch first, and observe carefully to see what they find. 【Title of writing on blackboard】

Okay, which classmate will say. The female student in the first row, you are the first to raise your hand, and you answer. The student said that the four data were compared three times, three times in the first time, two times in the second time, and one time in the third time, and finally got the order of 10, 26, 35, 37. The description was very clear, and she was the first student who raised her hand to stand up and answer the question. Come on, everyone praises her for her excellent performance and courage. please sit down.

In fact, this kind of sorting is called bubble sorting. This kind of sorting is to push the smaller data upward one by one in a series of data. Each pass of processing is to float the smallest element to the front position like a bubble, so bubbling is a very appropriate description.

Do you understand the process of bubble sorting? So if n numbers are sorted, how many rounds of comparison are needed, and how many times in each round? Ask the students to discuss these two issues with the information technology group as a unit. If there is a problem during the discussion, you can raise your hand to signal the teacher, and the teacher will check it. Okay, five minutes for everyone, let's start now.

Well, the time is up, and the teacher sees that the voices of the students' discussions have also quietened down. I believe everyone is ready. Which group will share their exchange results? First panel rep, please.

He said that the first round needs to compare (n-1) times, and the second round compares the remaining n-1 numbers, and needs to compare n-2 times. . . By analogy, the n-1th round needs to be compared once.

Very good, please take a seat. Group 3 you want to add, please say

To sort n data, it is necessary to compare n-1 rounds. Come, please sit down.

Both students answered correctly. It seems that the discussion just now is very meaningful.

Since everyone's thinking is so clear, please continue to discuss and draw a flow chart of bubble sorting. Give everyone 5 minutes, start now.

Okay, during the inspection, the teacher found that the students drew very clearly. The teacher is now casting the flow chart of this group, let's take a look.

We see that i records the number of passes being executed, j records the subscript of the current array element during each pass of processing, and d represents the array.

He assigns 1 to the initial variable i, judges whether i is equal to n, if yes, outputs the sorted array d, if not,

Assign n to j, judge whether j is less than or equal to i, if yes, i+1, repeat the above operation.

if not,

Now give everyone 5 minutes to write the program by themselves.

When the time is up, which classmate will demonstrate.

Four numbers were randomly selected, and the sorting results were obtained from small to large. The algorithm designed by this student is completely correct.

(3) Consolidation and improvement

(4) Summary assignment

Supplement: Algorithmic complexity refers to the amount of computer resources required for a computer to run the algorithm. The amount of time resources required is called the time complexity. The amount of space resources required is called space complexity.

Guess you like

Origin blog.csdn.net/weixin_72634509/article/details/128601333