This is the last article in our series of official articles on algorithms. We have learned a lot about sorting, including common bubbling and fast sorting, as well as the less common simple insertion and Hill sorting. Since this is the last article today and the last one related to sorting, let's relax a bit and learn two very simple sorting algorithms.
Simple selection sort
The first is the simple selection sort, which is divided under the selection sort, but in fact, it can also be regarded as the exchange sort. Because its core code also has the realization of the exchange operation. There is not much to say about this sorting, each time you find the largest or smallest data in the traversal, and then put it in the corresponding position. Let's look at the code first, and then look at the analysis of the diagram.
function SelectSort($numbers){
$n = count($numbers);
for( $i = 0 ; $i < $n ; $i++){
$k = $i;
for( $j = $i+1 ; $j < $n ; $j++){
if($numbers[$j] < $numbers[$k]){
$k = $j;
}
}
if($k != $i){
list($numbers[$i], $numbers[$k]) = [$numbers[$k], $numbers[$i]];
}
}
echo implode(', ', $numbers), PHP_EOL;
}
SelectSort($numbers);
// 13, 27, 38, 49, 49, 65, 76, 97
The code is not complicated, you can notice that it also has exchange codes. We are using the exchange method of the little easter egg in the previous article to exchange data locations. It is slightly different from the special exchange sorting algorithms such as bubbling and fast sorting. The position of i is unchanged each time it is exchanged. What does it mean? For example, our current i is 0, which means that the smallest data in the entire sequence should be placed in this place. So the j loop starts from the position of i + 1, and then constantly compares with the data at the position of i, and constantly updates k (k is designated as i at the beginning). After finding the smallest data, directly exchange this data with the data of i, so that the smallest data is placed at the position of i. This is the core idea of simple selection and sorting.
This large section may be awkward to read. Let's take a look at the picture!
We still take the detailed process of the first trip as an example.
- k = i, then j starts traversing from the second data
- If numbers[j] are found to be less than numbers[k] data, that is, smaller data, let k = j
- After the loop traversal of j is completed, the subscript pointed to by k is the smallest data, so the values of k and i are exchanged
- Once sorted, the smallest data is placed in the forefront position
Does it feel a bit like bubbling? It is very similar indeed, bubbling is also an outer loop to put a certain maximum or minimum value in the correct position. However, it should be noted that the bubbling is the comparison of the two data before and after, and it is very likely that each comparison will be exchanged. The selection sort is based on the position of a subscript pointer to determine the data, and only one exchange is performed at the end. Therefore, it is a selective exchange, not a pure exchange all the way to the end.
Simple bucket sort
The real bucket sorting is still more complicated, but the simple bucket sorting we are learning today is really simple. It embodies a way of exchanging space for time. How does it change?
function BucketSort($numbers){
$bucketList = [];
$maxValue = max($numbers);
for($i=0;$i <= $maxValue;$i++){
$bucketList[$i] = 0;
}
foreach($numbers as $n){
$bucketList[$n]++;
}
$sortList = [];
foreach($bucketList as $k => $v){
if($v > 0){
for( ; $v > 0 ; $v--){
$sortList[] = $k;
}
}
}
echo implode(', ', $sortList), PHP_EOL;
}
If it is a sorting operation for a number type, especially if the number base is not large, such as type enumeration and the like, we can all use this bucket sorting method. First of all, we have to look at the current maximum number, then initialize an array to the subscript of this maximum number, and set everything to 0. Then traverse the original sorted array and add 1 to the value corresponding to the data to be sorted. Therefore, the values of the keys represented by the sequence to be sorted will all become 1. At the same time, if there is the same data, we use the ++ operation, and the key value corresponding to this data will continue to increase by 1. The specific process is shown in the figure below:
Believe that this picture has been explained very clearly, and there is no need for us to explain it in depth. This is the simplest way of sorting buckets. We can also replace the contents of this bucket array with two-dimensional data, so that we can sort more complex data. However, it must be noted that it must be for digital types. The bucket sorting we introduced is actually a variant of the real bucket sorting, and some people call it "counting sorting".
The more complete bucket sorting is actually to divide the data into different groups first, and each group can be regarded as a bucket. Then sort the data in the group in this bucket, and connect these groups (buckets) after the sorting is completed. Its time complexity is close to O(n). But like the simplest bucket sorting we introduced, complex bucket sorting also has many stringent requirements, so although its effect is high, it is not common.
Summarize
Today’s content is very simple. Simple selection is actually a kind of exchange sorting, but it is still classified into the selection sorting category in the broad category. The bucket sort is a kind of cardinal sort. There are actually many sorts of sorts, but apart from what we have learned, others are more complicated and uncommon. If you are interested, you can learn more about what can be further studied in our summary article in the next issue. . The excitement continues, don't miss it!
Test code:
Reference documents:
"Data Structure" Second Edition, Yan Weimin
"Data Structure" Second Edition, Chen Yue
"Aha! algorithm"
===========
Searchable on their respective media platforms [Hardcore Project Manager]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。