Seven sorting algorithms of data structure

1. Direct Insertion Sorting
1. Principle
Direct Insertion Sorting. The entire array is divided into two intervals, namely the unordered interval and the ordered interval
. Each time you select the first element of the unordered interval, select the appropriate position to insert in the ordered interval . Not much to say, let’s look at its code implementation below:
2. The code implementation will
Seven sorting algorithms of data structure
analyze the performance of this sorting. The worst time complexity is o(N), the best time complexity is o(N^2), then the average complexity The degree is o(N^2). Its space complexity is o(1), and it does not need to open up additional space. It can be seen that this sorting does not reverse the relative position of the same number. When we sort, we choose greater than or equal to the right of the number. So it is a stable sort. If a group of numbers is basically in order, then the insertion sort algorithm will be more efficient.
II. Hill sorting
1. Principle
Hill sorting is based on insertion sorting. It divides the entire array into gap groups, then sorts each group, and then synthesizes an array after sorting, and continues to divide into gap/2 Group, repeat the above steps until gap=1; then insert the entire array to get the final ordered array.
2. Code implementation
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
Let’s analyze the performance of this sorting. It is optimized on the basis of insertion sorting, so its time complexity is smaller than insertion sorting. The worst time complexity is o(N^2), the best It is o(N), the average complexity is o(N^1.3), and the space complexity is o(1), which is not a stable sort, because the relative position of the same number cannot be guaranteed during the grouping process.
3. Selection and sorting
1. Principle
Each time the largest element is selected from the unordered interval and stored at the end of the unordered interval, until all the data to be sorted are arranged.
2. Code implementation
Seven sorting algorithms of data structure
The following analysis of sorting performance, worst time complexity o (N^2), best time complexity o (N), average time complexity o (N^2), space complexity o (1), It is an unstable sort.
Four, heap sort
1. Principle
Heap sorting is also based on selection sorting, except that it selects the largest element by building a large heap, swaps the top element of the heap with the last element of the heap, and then puts it in another array, and then Adjust the stack down.
2. Code implementation
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure

Let's look at the performance of the algorithm below. The time complexity is o(N*logN), the space complexity is o(1), and the heap sort is still unstable.
5. Bubble sorting
1. Principle
Bubble sorting also divides the array into two intervals, the unordered interval [0,array.length-i-1], and the ordered interval [array.length-i-1,array.length] , Take out a number from the unordered interval in order each time, compare it with the following numbers one by one, and place the large numbers behind until all numbers are traversed. The number of bubbling is array.length, and the number of comparisons is array.length-i-1.
2. Code implementation
Seven sorting algorithms of data structure
Let’s look at the performance of the algorithm. The worst time complexity is o(N^2), the best time complexity is o(N), the average time complexity is o(N^2), and the space complexity is o(1). Bubble sort is stable sort. Because in the comparison process, the number greater than it is put behind.

6. Quick Sorting
1. Principle
Choose a value from the range to be sorted as the reference value, traverse the entire range to be sorted, and place the value smaller than the reference value on the left side of it, and the value smaller than it on the right side, and the left and right cells Process in the same way until the length of the cell is 1, or the length is 0, which means that all the cells have been sorted.
2.
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
The worst time complexity of the code to implement it is 0 (N^2), the best time complexity is o (N logN), the average time complexity is o (N logN), and the space complexity is o (N logN). Unstable, it can be seen that it is much more efficient than the previous sorts.
7. Merge and sort
1. Principle:
The divide-and-conquer method is used to merge the subsequences that have been ordered, first make each subsequence in order, then make each sequence in order, and finally merge into an ordered array.
2.
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
Seven sorting algorithms of data structure
The time complexity of the code to implement the sorting algorithm is o(N
logN), the space complexity is o(N), and the sorting is stable.

Guess you like

Origin blog.51cto.com/14632675/2542537