Data structure and algorithm learning (day2)

foreword

(1) Before studying this chapter, you need to review the content of the previous chapter, and type the code to solve the problem.

(2) After the exercises in the previous chapter, you should have realized that the capacity of the array to be applied for in the simplified version of bucket sorting is determined by the maximum value of the numbers to be sorted, so if the maximum value of the numbers to be sorted is 210000000 , then we have to apply for an array with a size of 210000001, no doubt, it is a waste of space!

(3) Moreover, i in the simplified version of bucket sorting represents a value, and book[i] represents the number of values. Since the array requires that the type of i in book[i] must be an integer, if there are decimals to be sorted, then simplify Bucket sorting is invalid!

Learning objectives for this chapter:

(1) Learn the principle of bubble sorting

(2) Can use bubble sorting to connect with real life and solve programming problems

Bubble Sort

(1) The basic idea of ​​bubble sorting is: compare two adjacent elements each time, and exchange them if their order is wrong.

(2) Code idea: If there are n numbers to be sorted from large to small, only n-1 numbers need to be returned, that is to say, n-1 operations need to be performed. And "every trip" needs to compare two numbers starting from the first digit, put the smaller number at the back, and move back one bit after the comparison is completed to continue comparing the size of the next two adjacent numbers. Repeat this step until the last number that has not been reset, and the numbers that have already been reset do not need to be compared (what do you compare the numbers that have already been reset, it is a waste of expression).

おすすめ

転載: blog.csdn.net/weixin_62261692/article/details/132645240