Classic sorting algorithm-patient sortingPatience sorting
The key to this sorting is in bucket and bucket loading rules.
Bucket creation rule: if no bucket exists, create a new bucket. If the bucket creation rule is not met, create a new bucket.
Bucket logging rule: the bucket can be entered only when the number at the top of the bucket is smaller. If Multiple Buckets can be entered, the bucket can be entered from left to right.
For example, the array to be sorted [6 4 5 1 8 7 2 3]
The first step is to take the number 6. If a bucket does not exist, create a bucket according to bucket creation rule 1 and put it in. For the sake of convenience, the bucket is named bucket 1 or bucket 1.
Step 2: Obtain the number 4. Because 4 meets the bucket logging rule of Bucket 1, enter bucket 1 and place it on top of bucket 6, as shown in figure 2.
Step 3: Obtain the number 5. Because the number 5 does not conform to the bucket logging rule of Bucket 1, it is larger than the number at the top of Bucket 1. At this time, there are no other buckets, create Bucket 2 according to the bucket creation rule and place it in the bucket
Step 4: Obtain the number 1. The value 1 complies with the rules for entering bucket 1, which is smaller than the value 4 and smaller than the value 5, both of them can be entered. Check in the No. 1 bucket according to the bucket access rule 1 (it doesn't matter if you actually stay in the No. 2 bucket)
Step 5: Take the number 8 out, 8 is bigger than the head of the first bucket, and 5 is bigger than the head of the second bucket. In this case, 8 decides to stand on its own, set up bucket 3 and check in the bucket to become the first manager
Step 6: retrieve the number 7. The number 1 bucket and the number 2 bucket won't work. Finally, the number 3 bucket was taken to the server and rushed to the third bucket's door.
Step 7: Take the number 2 and get it from the head of Bucket 2.
Step 8: Take the number 3 and get it from the current head of Bucket 3 7.
All buckets have been entered ....
Then extract the numbers 1 4 6, 2 5, 3 7 8 from the first bucket order.
The rest end the battle with insertion sorting, and return home from work ~
Back to main directory [classic Sorting Algorithm] [Collection]