I have been free for a while, so I took some time to summarize the seven commonly used sorting algorithms in Java. I hope I can review them later! 1. Insertion sort algorithm The basic idea of insertion sort is that in the process of traversing the array, it is assumed that the elements before serial number i, that is, [0..i-1], have been sorted. This trip needs to find the correct position k of the element x corresponding to i, and in the process of finding this position k, move the compared elements back one position one by one to "make room" for the element x, and finally assign the element value corresponding to k to x. In general, the time complexity and space complexity of insertion sort are O(n2) and O(1), respectively.
2. Selection sort algorithm The basic idea of selection sort is to traverse the array, with i representing the number that needs to be sorted, and then find the minimum value in the remaining [i…n-1], and then exchange the minimum value found with the value pointed to by i. Because there is a sub-process of selecting the highest value in each process of determining the element, people figuratively call it selection sort. The time complexity and space complexity of selection sort are O(n2) and O(1) respectively.
3. Bubble sort algorithm Bubble sort is to put larger numbers at the bottom and smaller numbers at the top.
4. Quick sort algorithm Through a sorting process, the records to be sorted are divided into two independent parts. The keywords of one part of the records are smaller than those of the other part. Then, the two parts of the records can be sorted separately to achieve the purpose of ordering the entire sequence. The essence is to find a base position (pivot, watershed, which is smaller than the left and larger than the right. It can be random and named base. First, start from the rightmost side of the sequence to find a smaller one than base. If it is smaller, change the position, so that the base moves to the position on the right just now (smaller than the base when compared) (recorded as a temporary high position), so that the right side of the base is larger than the base. Then, start from the leftmost side of the sequence to find a larger one than base. If it is larger, change the position, so that the base moves to the position on the left just now (larger than the base when compared) (recorded as a temporary low position), so that the left side of the base is smaller than the base. Repeat the above two steps until low == high, which makes the pivot and watershed truly found. Return to this position, and recurse on the sequences on the left and right of the watershed respectively.
5. Merge sort algorithm Merge sort is implemented recursively, which belongs to the "divide and conquer" method. The target array is divided into two from the middle, and then the two arrays are sorted separately. After the sorting is completed, the two sorted arrays are "merged" together. The most important thing about merge sort is the "merging" process. The merging process requires additional space that is the same length as the two arrays to be merged.
6. Shell sort algorithm Shell sort was born because insertion sort encounters the problem of moving too many elements when processing large-scale arrays. The idea of Shell sort is to "divide and conquer" a large array into several small arrays, divided by gap. For example, the array [1, 2, 3, 4, 5, 6, 7, 8], if divided by gap = 2, can be divided into two arrays [1, 3, 5, 7] and [2, 4, 6, 8] (correspondingly, if gap = 3, the divided arrays are: [1, 4, 7], [2, 5, 8], [3, 6]) and then insert sort the divided arrays separately. After each sub-array is sorted, reduce the gap value and repeat the previous steps until gap = 1, that is, insert sort the entire array. At this time, the array is basically sorted, so the elements that need to be moved will be very small, which solves the problem of a large number of moves when inserting sort is processing large arrays. Shell sort is an improved version of insertion sort. It is very helpful to improve efficiency when the amount of data is large. When the amount of data is small, it is recommended to use insertion sort directly.
7. Heap sort algorithm The essence is to first construct a big top heap, parent is larger than children, the root node is the *** node, swap the *** node (root) with the tail node (the *** node, which is smaller), leaving the *** tail node, now ***, and the rest, starting from the *** element to the position before the tail node, construct a big top heap recursively.
|
<<: Starting a business? Actually, I just don’t want to work.
>>: Mobile, Ecosystems, and the Death of the PC
It’s the end of August, the tail end of summer, a...
At a procurement forum, the procurement director ...
Without discussing how to define “success”, can y...
Why does the original material cost hundreds of t...
Preface In e-commerce apps, the focus is on produ...
Let me first briefly introduce Bilibili, or B Sta...
Do you see the IV drip in the picture? That's...
Wei Ke We live at the bottom of the Earth's a...
First of all, some "fat beef rolls" are...
It was not until December that we realized that t...
In the physical examination report, many people w...
Adult life is often a major rollover scene But I ...
Yesterday afternoon, Douyin officially held a mar...
Preface I remember that when I was a sophomore, I...
According to the National Health Commission websi...