# Super detailed explanation - top ten classic sorting algorithms - Unfinished

Today's chicken soup: you have overcome suffering, it is your wealth;
suffering has defeated you, and it is your humiliation.

# 0. Introduction to sorting algorithm

sorting algorithm is the most basic algorithm in data structure and one of the most classic algorithms.
sorting algorithms have a variety of classification methods, such as internal sorting and external sorting, comparative sorting and non comparative sorting, stable sorting and unstable sorting, etc.

## 0.1 internal sorting and external sorting

• Internal and external sorting:
(1) Internal sorting: internal sorting is the sorting of data records in memory.
(2) External sorting: because the sorting data is large, external sorting cannot accommodate all sorting records at one time. External memory needs to be accessed in the sorting process.
• Sort by internal sort and external sort:
(1) Internal sort: insert sort, Hill sort, select sort, bubble sort, merge sort, quick sort, heap sort, cardinal sort.
(2) External sort:

## 0.2 comparison sort and non comparison sort

• Comparison sort and non comparison sort:
(1) Comparison sort: determine the relative order between elements through comparison. Because its time complexity cannot exceed O(nlogn), it is also called nonlinear time comparison sort.
(2) Non comparison sort: it does not determine the relative order between elements through comparison. It can break through the time lower bound based on comparison sort and run in linear time. Therefore, it is also called linear time non comparison sort.
• Sort by comparison and non comparison:
(1) Comparison sort: bubble sort, selection sort, insertion sort, merge sort, heap sort, quick sort, etc.
(2) Non comparison sort: count sort, cardinality sort, bucket sort, etc.

## 0.3 stable sort and unstable sort

• Stable sort and unstable sort
the stability of sorting algorithm is whether the relative order of equal elements is fixed when equal elements appear in the element list. If the relative order is fixed, the sorting algorithm is stable, otherwise it is unstable.
• Classification by stable sort and unstable sort:
(1) Stable sorting: bubble sorting,
(2) Unstable sort:

# 1. Bubble sorting

## 1.1 idea of bubble sorting

The idea of bubble sorting is to compare two numbers from the first number and exchange them in reverse order. After a comparison, the largest number will be exchanged to the last, that is, the largest number will sink to the bottom, which is similar to the largest bubble at the bottom of the pool (on the contrary, the principle of sorting from large to small is the same). Then repeat the above process for the first n-1 records until the sorting is completed, similar to the bubbling process.
note: in a sort comparison, if no exchange is found between two comparisons, it indicates that the array has been ordered.

## 1.2 algorithm steps of bubble sorting

(1) Take the sorting from small to large as an example: start from the first element and the second element, compare the two adjacent elements, and if the former is larger than the latter, exchange positions with each other, otherwise keep the original state.
(2) The second element is compared with the third element. If the second element is larger than the third element, change the position of each other, otherwise it remains the same.
(3) For each pair of adjacent elements, repeat the above process of "comparison" and "exchange" until the last pair of elements. At this time, the largest number has been exchanged to the end, that is, the largest bubble has emerged.
(note that the biggest bubble has already popped up, so you don't need to participate in the next sorting process of comparison and exchange. The next task is to make the next big bubble pop up.)
(4) Similarly, start the second round of comparison and exchange sorting process for all elements (except the largest number), and find the second largest bubble.
(5) Continue to repeat the above steps for fewer and fewer elements each time until the sorting is completed (no pair of numbers need to be compared).
(6) Note: in the process of sorting, if you find that there is no element to be exchanged in pairwise comparison, it means that the whole arrangement has been orderly and there is no need to compare the remaining two elements in the last round. as can be seen from the above figure, bubble sorting belongs to stable sorting.

## 1.3 bubble sorting code

```# coding:utf-8
'''
# @Method: bubble sort
# @Author: wlhr62
'''
import os
import sys

class Solution:
def BubbleSort(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
n = len(nums)
for i in range(n):
for j in range(0, n-i-1):
if nums[j] > nums[j+1]:
# temp = nums[j]
# nums[j] = nums[j+1]
# nums[j+1] = temp
nums[j], nums[j+1] = nums[j+1], nums[j]
print("The first", i+1, "Sorting result of round:", nums)
return nums

if __name__ == "__main__":
nums = [50, 36, 66, 76, 95, 12, 25, 36]
A = Solution()
res = A.BubbleSort(nums)
print("Final sorting result:", res)

```

The running results of the code are as follows:

```Sorting results of round 1: [36, 50, 66, 76, 12, 25, 36, 95]
Sorting results of round 2: [36, 50, 66, 12, 25, 36, 76, 95]
Sorting results of round 3: [36, 50, 12, 25, 36, 66, 76, 95]
Sorting results of round 4: [36, 12, 25, 36, 50, 66, 76, 95]
Sorting results of round 5: [12, 25, 36, 36, 50, 66, 76, 95]
Sorting results of round 6: [12, 25, 36, 36, 50, 66, 76, 95]
Sorting results of Round 7: [12, 25, 36, 36, 50, 66, 76, 95]
Sorting results of round 8: [12, 25, 36, 36, 50, 66, 76, 95]
Final sorting result: [12, 25, 36, 36, 50, 66, 76, 95]
```

Existing problems:
.
don't forget the precautions mentioned above at the beginning: in a sort comparison, if no exchange is found between two comparisons, it indicates that the array is in order.

The modification code is as follows:
add a tag variable to judge whether there is exchange in the current round of sorting. If not, the sorting process is ended directly.

```# coding:utf-8
'''
# @Method: bubble sort
# @Author: wlhr62
'''
import os
import sys

class Solution:
def BubbleSort(self, nums):
"""
:type nums: List[int]
:rtype: List[int]
"""
n = len(nums)
for i in range(n):
changeNum = 0  # Add a variable to judge whether there is exchange during this round of sorting
for j in range(0, n-i-1):
if nums[j] > nums[j+1]:
nums[j], nums[j+1] = nums[j+1], nums[j]
changeNum += 1
print("The first", i+1, "Sorting result of round:", nums)
# If it is found that there is no exchange between two comparisons, it indicates that the array has been ordered
if changeNum == 0:
break
return nums

if __name__ == "__main__":
nums = [50, 36, 66, 76, 95, 12, 25, 36]
A = Solution()
res = A.BubbleSort(nums)
print("Final sorting result:", res)

```

The running results after modifying the code are as follows:

```Sorting results of round 1: [36, 50, 66, 76, 12, 25, 36, 95]
Sorting results of round 2: [36, 50, 66, 12, 25, 36, 76, 95]
Sorting results of round 3: [36, 50, 12, 25, 36, 66, 76, 95]
Sorting results of round 4: [36, 12, 25, 36, 50, 66, 76, 95]
Sorting results of round 5: [12, 25, 36, 36, 50, 66, 76, 95]
Sorting results of round 6: [12, 25, 36, 36, 50, 66, 76, 95]
Final sorting result: [12, 25, 36, 36, 50, 66, 76, 95]
```

Think: can the repetition of round 6 be omitted?

## 1.4 complexity analysis of bubble sorting

• Worst time complexity: O(n^2)
• Unless otherwise specified, the worst time complexity is generally calculated.
• In bubble sorting, the worst case is that the initial state of the list is completely in reverse order. In this case, n-1 rounds of bubbling are required, and each round of bubbling requires n-i-1 comparison and exchange operations. The average value of I is n/2 and the time complexity is n(n-1)/2.
• In the worst case, the time complexity of bubble sorting is O(n^2).
• Optimal time complexity: O(n)
• In bubble sorting, the best case is that the initial state of the list completely conforms to the expected arrangement. In this case, only one round of bubbling is required, and there is no comparison and exchange in this process. A marked variable is used to judge whether there is exchange in the current round of sorting. If not, the sorting process is ended directly.
• In the best case, the time complexity of bubble sorting is O(n).
• Average time complexity: O(n^2)
• In bubble sorting, the average time complexity is O(n^2).
• Space complexity: O(1)

## 1.5 stability analysis of bubble sorting

• Bubble sort belongs to stable sort.
• Analysis: in bubble sorting, two elements are compared each time, and the exchange will be carried out only when the size order of the elements is wrong. If there are two equal elements in the element list, they will eventually be adjacent to each other, but the comparison is not in the wrong order, so they will not be exchanged during comparison, and the relative order remains unchanged. Therefore, bubble sorting is a stable sorting algorithm.

# 10. Cardinality sorting

Posted on Mon, 01 Nov 2021 05:44:09 -0400 by rinteractive