Interviewer: Do you know the three common sorting algorithms of bubbling, insertion, and selection? Why is insertion sort more popular?

The sorting algorithm should be something that everyone of us will come into contact with when they first learn, and it should be the first algorithm most people learn. There are many common sorting algorithms, such as monkey sorting, sleep sorting, noodle sorting, etc. Here we only learn the most common and classic sorting algorithms.

According to the time complexity of the algorithm, it can be divided into the following three categories. We learn from the classification to deepen the memory and master the algorithm.

How to analyze a "sorting algorithm"?

Execution efficiency of sorting algorithm

  1. Best case, worst case, average case time complexity
  2. Time complexity coefficients, constants, low-level
  3. Number of comparisons and exchanges (or moves)

Memory consumption of sorting algorithm

The memory consumption of an algorithm can be measured by space complexity, and sorting algorithms are no exception. However, in view of the space complexity of the sorting algorithm, we also introduced a new concept, sorted in place (Sorted in place). The in-place sorting algorithm refers specifically to the sorting algorithm whose space complexity is O(1).

Stability of sorting algorithm

It is not enough to measure the performance of a sorting algorithm only by execution efficiency and memory consumption. For the sorting algorithm, we also have an important metric, stability. The concept is that if there are elements of equal value in the sequence to be sorted, the original order of equal elements will not change after sorting.

Let me explain through an example. For example, we have a set of data 2, 9, 3, 4, 8, 3, sorted by size, it is 2, 3, 3, 4, 8, 9. There are two 3s in this set of data. After sorting by a certain sorting algorithm, if the order of the two 3s does not change, then we call this sorting algorithm a stable sorting algorithm; if the front-to-back order changes, the corresponding sorting algorithm is called unstable Sorting Algorithm.

Bubble Sort (Bubble Sort)

Bubble sort only operates on two adjacent data. Each bubbling operation will compare two adjacent elements to see if the size relationship requirements are met. If they are not satisfied, let them swap. One bubbling will move at least one element to where it should be. Repeat n times to complete the sorting of n data.

I use an example to show you the entire process of bubble sorting. We want to sort a set of data 4, 5, 6, 3, 2, 1, from small to large. The detailed process of the first bubbling operation is like this:

It can be seen that after a bubbling operation, the 6 element has been stored in the correct location. To sort all the data, we only need to perform 6 bubbling operations like this.

In fact, the bubbling process just mentioned can be optimized. When there is no data exchange for a bubbling operation, it means that the order has been reached, and there is no need to continue the subsequent bubbling operation. I have another example here. It only needs 4 bubbling operations to sort 6 elements.

The principle of the bubble sort algorithm is easier to understand. I will post the specific code below. You can combine the code to see the principle I mentioned earlier.

Insertion sort

First, we divide the data in the array into two intervals, a sorted interval and an unsorted interval. The initial sorted interval has only one element, which is the first element of the array. The core idea of ​​the insertion algorithm is to take the elements in the unsorted interval, find a suitable insertion position in the sorted interval and insert it, and ensure that the sorted interval data is always in order. Repeat this process until the elements in the unsorted interval are empty and the algorithm ends.

As shown in the figure, the data to be sorted are 4, 5, 6, 1, 3, 2, with the sorted interval on the left and the unsorted interval on the right.

Insertion sort also contains two operations, one is the comparison of elements, the other is the movement of elements. When we need to insert a piece of data a into the sorted interval, we need to compare the size of a with the elements of the sorted interval in order to find a suitable insertion position. After finding the insertion point, we also need to move the order of the elements after the insertion point one place backward, so as to make room for element a to be inserted.

The code part is not difficult, as shown below:

Note that here is to traverse the ordered data from end to beginning.

Select sort

The implementation idea of ​​selection sorting algorithm is similar to insertion sorting, and it is also divided into sorted interval and unsorted interval. However, the selection sort will find the smallest element in the unsorted interval each time and place it at the end of the sorted interval.

It is also relatively simple, just look at the code:

Expand

The time complexity of bubble sort and insertion sort are both O(n2), and both are in-place sorting algorithms. Why is insertion sort more popular than bubble sort?

Answer: From the point of view of code implementation, the data exchange of bubble sort is more complicated than the data movement of insertion sort. Bubble sort requires 3 assignment operations, while insertion sort only requires one. Let's look at this operation:


冒泡排序中数据的交换操作:
if (a[j] > a[j+1]) { // 交换
   int tmp = a[j];
   a[j] = a[j+1];
   a[j+1] = tmp;
   flag = true;
}

插入排序中数据的移动操作:
if (a[j] > value) {
  a[j+1] = a[j];  // 数据移动
} else {
  break;
}

We roughly count the execution time of an assignment statement as unit time (unit_time), and then use bubble sort and insertion sort to sort the same array with a reverse order of K. With bubble sorting, K exchange operations are required, and 3 assignment statements are required each time, so the total exchange operation time is 3*K unit time. The data movement operation in insertion sort only requires K unit time.

This is just our very theoretical analysis . For the sake of experimentation, I wrote a performance comparison test program for the above bubble sorting and insertion sorting Java code, randomly generated 10,000 arrays, each containing 200 data, and then My machine uses bubble sorting and insertion sorting algorithms to sort. The bubble sorting algorithm takes about 555ms to execute, while the insertion sorting only takes about 115ms to complete !

to sum up

To analyze and evaluate a sorting algorithm, you need to look at it from three aspects: execution efficiency, memory consumption, and stability. These three kinds of time complexity are O(n2) sorting algorithms, bubble sorting, insertion sorting, and selection sorting.

Recommended reading

Recently I interviewed Byte and BAT, and compiled an interview material "Java Interview BAT Clearance Manual", covering Java core technologies, JVM, Java concurrency, SSM, microservices, databases, data structures, etc. Obtaining method: Click "Watching", follow the official account and reply to 666 to receive, more content will be provided one after another

 

 

Guess you like

Origin blog.csdn.net/taurus_7c/article/details/105170821