This article mainly introduces several examples of common sorting algorithms implemented by Python, such as direct insertion sorting, direct selection sorting, bubble sorting, and quick sorting, for more information about Baidu interview preparation, please refer to the preparation for Baidu interview. although it was still swallowed up, I still put the "trophy" in those days, algorithms have always been a weakness of their own, and we need to work harder in the future.
Below we use Python to implement several common sorting methods, such as fast sorting, selection sorting, and two-way parallel sorting.
The code is as follows:
# Encoding = UTF-8
Import random
From copy import copy
Def directInsertSort (seq ):
"Insert sort directly """
Size = len (seq)
For I in range (1, size ):
Tmp, j = seq [I], I
While j> 0 and tmp <seq [j-1]:
Seq [j], j = seq [j-1], J-1
Seq [j] = tmp
Return seq
Def directSelectSort (seq ):
"Select sort directly """
Size = len (seq)
For I in range (0, size-1 ):
K = I; j = I + 1
While j <size:
If seq [j] <seq [k]:
K = j
J + = 1
Seq [I], seq [k] = seq [k], seq [I]
Return seq
Def bubbleSort (seq ):
"Bubble sorting """
Size = len (seq)
For I in range (1, size ):
For j in range (0, size-I ):
If seq [j + 1] <seq [j]:
Seq [j + 1], seq [j] = seq [j], seq [j + 1]
Return seq
Def _ pide (seq, low, high ):
"Quick sort division function """
Tmp = seq [low]
While low! = High:
While low If low
Seq [low] = seq [high]
Low + = 1
While low If low
Seq [high] = seq [low]
High-= 1
Seq [low] = tmp
Return low
Def _ quickSort (seq, low, high ):
"Secondary function of quick sorting """
If low> = high: return
Mid = _ pide (seq, low, high)
_ QuickSort (seq, low, mid-1)
_ QuickSort (seq, mid + 1, high)
Def quickSort (seq ):
"Quick sort package function """
Size = len (seq)
_ QuickSort (seq, 0, size-1)
Return seq
Def merge (seq, left, mid, right ):
Tmp = []
I, j = left, mid
While I <mid and j <= right:
If seq [I] <seq [j]:
Tmp. append (seq [I])
I + = 1
Else:
Tmp. append (seq [j])
J + = 1
If I <mid: tmp. extend (seq [I:])
If j <= right: tmp. extend (seq [j:])
Seq [left: right + 1] = tmp [0: right-left + 1]
Def _ mergeSort (seq, left, right ):
If left = right:
Return
Else:
Mid = (left + right)/2
_ MergeSort (seq, left, mid)
_ MergeSort (seq, mid + 1, right)
Merge (seq, left, mid + 1, right)
# Binary Union sorting
Def mergeSort (seq ):
Size = len (seq)
_ MergeSort (seq, 0, size-1)
Return seq
If _ name _ = '_ main __':
S = [random. randint (0,100) for I in range (0, 20)]
Print s
Print "\ n"
Print directSelectSort (copy (s ))
Print directInsertSort (copy (s ))
Print bubbleSort (copy (s ))
Print quickSort (copy (s ))
Print mergeSort (copy (s ))
The running result is as follows:
The code is as follows:
E: \ python_project \ practice> sorting. py
[10, 47, 56, 76, 64, 84, 26, 8, 47, 51, 88, 81, 32, 95, 91, 29, 28, 69, 61, 45]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]
[8, 10, 26, 28, 29, 32, 45, 47, 47, 51, 56, 61, 64, 69, 76, 81, 84, 88, 91, 95]