Classic sorting algorithm-bubble sorting

Practitioners in the Internet industry often encounter such a problem during the interview process. Testers and developers have the highest chance of encountering it: Please talk about several sorting algorithms that you are familiar with.

This question may not be the only question to assess your professional skills, but if you can answer it, the interviewer will definitely give you a relatively high impression score, because the algorithm directly examines a person’s thinking and logic and analyzes the data ,problem solving skill. This is also the talent quality that many high-tech companies are paying more and more attention to.

It’s a pity that many people’s performance on this question is very unsatisfactory, either they can’t answer at all, or they’ve heard it, but the explanation is very confusing. This is due to the fact that they rarely understand the algorithm or have a little knowledge of the algorithm. of.

Well, before introducing the bubbling algorithm, there must be many novices who don't even know the concept of the algorithm, so let's do some literacy first. Algorithm (Algorithm) refers to the accurate and complete description of the problem-solving scheme. It is a series of clear instructions to solve the problem. The algorithm represents a systematic method to describe the strategy mechanism of solving the problem. This is the explanation of Baidu Baike. In fact, it is easier to say that the algorithm is the method of calculation.

Going back to the above question, please list several sorting algorithms that you are familiar with.

The editor here will introduce you to several common sorting algorithms: bubble sort, quick sort, insertion sort, selection sort, hill sort, etc. Usually, you can basically get it by saying two or three sorts that you are more familiar with. The interviewer.

We will not analyze and explain each sorting algorithm here. Let's focus on the classic sorting algorithm-bubble sorting, and talk about how it is sorted.

First, let’s understand the concept of bubbling. We should all have seen bubbles in the water. The higher the bubbles, the bigger the bubbles, so we use this common phenomenon in life to describe the bubbling sort, and keep the big numbers up. , Until all the data is arranged in ascending order is the idea of ​​bubbling sorting.

Suppose we have several random numbers: 4, 3, 6, 2, 1, 5, then we need to sort these numbers in ascending order by bubble sorting. What should we do?

Idea: Compare the first number with all the other numbers. If it is larger than it, switch positions until the largest number among these numbers pops to the top, and then continue with the second round, the third round, and the fourth round. In the 5th round, until all the large arrays have popped up, all the data will be sorted from small to large.

Classic sorting algorithm-bubble sorting

The above is the schematic diagram drawn by the editor. If you still don’t understand, then we can only use our ultimate weapon. The following is the bubbling ordering diagram I found on the Internet. I hope to help you understand this. Principle of the algorithm

Classic sorting algorithm-bubble sorting

The above is the sorting algorithm. If you want to use code to implement it, how to write it? The following editor uses java code to implement the above bubble sort:

Classic sorting algorithm-bubble sorting

Then let's analyze the complexity of this algorithm again.

Let's take a look at the total number of comparisons between values ​​during the entire bubbling process.

Assuming that there are n numbers to be sorted, the first number in the first round of bubbling has to be compared with the other n-1 numbers, so n-1 comparisons are required, and the largest number is popped to the top.

So the second round of bubbling is equivalent to bubbling with the previous n-1 numbers, so the second round needs n-2 comparisons, and so on, the third round of comparison times is n-3, and the last two numbers are bubbling. It only needs to be compared once when soaking.

So the number of comparisons is actually the sum of an arithmetic sequence from 1 to n-1, so the total number of comparisons is summed according to the arithmetic sequence: [1+(n-1)]*(n-1)/2, so The final number is n*(n-1)/2 times.

The editor here gives a brief introduction to bubble sorting, including the principle of bubble sorting, and the code implementation. As for other sorting algorithms, interested students can learn about them on Baidu.

Let me share some of my favorite information, I hope it can help everyone

This information is organized around [software testing]. The main content includes: python automated testing exclusive video, Python automated detailed information, a full set of interview questions and other knowledge content. For friends of software testing, it should be the most comprehensive and complete preparation warehouse. This warehouse has accompanied me through a lot of bumpy roads, and I hope it can also help you. Pay attention to WeChat public account: Programmer Erhei, you can get it directly
 

Guess you like

Origin blog.csdn.net/m0_52668874/article/details/114949468