[soft test] data structure and algorithm basis - sorting algorithm

1. Sorting algorithm

Sorting algorithm is one of the important parts of data structure
. Sorting algorithm is used to arrange a set of data in a specific order.
Sorting can be regarded as the most basic and commonly used algorithm, and it is also the most frequently tested algorithm in written test interviews.

2. Design idea of ​​sorting algorithm

Arrange a set of data in a certain order to improve the readability and processability of the data.

3. Common sorting algorithms and their basic principles

The most basic ones are bubble sorting, selection sorting, and insertion sorting, which can be implemented quickly with code. These mainly examine your actual coding ability. Heap sorting, merge sorting, and quick sorting algorithms need to be familiar with the main idea and the details that need attention. Requires familiarity with the time and space complexities of commonly used sorting algorithms. These algorithms have their own advantages and disadvantages, and are suitable for different scenarios and data types. In practical applications, it is necessary to select a suitable sorting algorithm according to specific needs and situations.

3.1 Bubble Sort (Bubble Sort)

Sorting is achieved by continuously comparing adjacent elements and moving larger elements to the right.
The time complexity is O(n^2).

3.2 Selection Sort

Each time the smallest (or largest) element is selected from the unsorted elements and placed at the end of the sorted.
The time complexity is O(n^2).

3.3 Insertion Sort

Sorting is achieved by inserting unsorted elements one by one into a sorted sequence.
The time complexity is O(n^2).

3.4 Quick Sort (Quick Sort)

By selecting a reference element, the sequence is divided into two subsequences, the elements on the left are both smaller than the reference element, and the elements on the right are larger than the reference element, and then the left and right subsequences are quickly sorted recursively.
The time complexity is O(n log n).

3.5 Merge Sort

Divide the sequence into several subsequences, merge and sort each subsequence, and then merge the sorted subsequences into an overall ordered sequence.
The time complexity is O(n log n).

3.6 Heap Sort

This process is repeated until the sequence is in order by building a max-heap or min-heap, then swapping the top element with the last element and removing the top element.
The time complexity is O(n log n).

3.7 Shell Sort

By defining an interval sequence, group the sequence by that interval sequence, and then perform an insertion sort on each group, gradually reducing the interval size until the interval is the time complexity depends on the choice of interval sequence.

Fourth, the range of use of various sorting algorithms

(1) When the data size is small, simple direct insertion sorting or direct selection sorting can be used.
(2) When the initial state of the file is basically in order, direct insertion sorting and bubble sorting can be used.
(3) When the data size is large, the fastest sorting algorithm can be used, and quick sorting can be considered. When the records are randomly distributed, the average time of quick sort is the shortest, but in the worst case, the time complexity at this time is O(n^2), and the recursion depth is n, and the required space is O(n) .
(4) There will not be the worst case of fast sorting for sorting, and the auxiliary space required for heap sorting is less than that of quick sorting, but these two algorithms are not stable, and the sorting is required to be stable. Merge can be considered Sort.
(5) Merge sort can be used for internal sorting, or it can be used for sorting without sorting. In external sorting, multi-way merging is usually used, and by solving the merging of long strings, wrapping long initial strings, and improving the parallel capability of the host and peripherals, etc., to reduce the additional times of accessing external memory and improve the efficiency of external sorting.

Guess you like

Origin blog.csdn.net/wstever/article/details/129889336