Python Sorting Algorithm

Requirements: Sort the elements of the array in a small to large list

1. Bubble Sorting

```bubble_list=[4,213,5,6,12,123,45,66,43]
def bubble(array):
for i in range(len(array)-1):   ##Outer loop, each cycle finds the maximum value on the rightmost side, and the next time ignores the number found in the previous cycle
for j in range(len(array)-1-i):  ##Internal loop, compared with the next number, if the first number is large, then swap
if array[j]>array[j+1]:
tmp=array[j]
array[j]=array[j+1]
array[j+1]=tmp
bubble(bubble_list)
print "Final:%s" % bubble_list```
`Final:[4, 5, 6, 12, 43, 45, 66, 123, 213]`

2. Select Sort

```select_list=[25,213,5,6,12,123,45,66,43]
def select(array):
for i in range(len(array)-1):   ##Start with the first number and compare it with the latter one in turn. If the number is smaller than the first one in the array, swap it.looplen(array)-1Second.
for j in range(i+1,len(array)):
if array[i]>array[j]:
tmp=array[i]
array[i]=array[j]
array[j]=tmp
select(select_list)
print "Final:%s" % select_list```

There is a disadvantage of this sort of selection. iEvery cycle, there may be a lot of data exchange process, so how to solve this problem?
Only one variable needs to be introduced, and each time least_index determines size, it only needs to assign a subscript with a smaller value to least_index.

```select_list=[25,213,5,6,12,123,45,66,43]
def select_plus(array):
for i in range(len(array)-1):
least_index=i
for j in range(i+1,len(array)):
if array[least_index]>array[j]:
least_index=j
tmp=array[i]
array[i]=array[least_index]
array[least_index]=tmp
select_plus(select_list)
print "Final:%s" % select_list```

3. Insert Sorting

```insert_list=[25,213,5,6,12,123,45,66,43]
def insert(array):
for i in range(1,len(array)):  #Compare the second number from the array with the number on the left
position=i
current_var=array[position]  ##Keep the values in array[i]
while array[position-1]>current_var and position>0:            ##When the number on the left is greater than the number of inserts, the two numbers are exchanged until the array reaches the top of the array
array[position]=array[position-1]
array[position-1]=current_var
position-=1
insert(insert_list)
print "Final:%s" % insert_list```
`Final:[5, 6, 12, 25, 43, 45, 66, 123, 213]`

4. Quick Sorting

The data to be sorted is divided into two separate parts by one-time sorting, in which all the data in one part is smaller than all the data in the other part. Then the two parts are sorted quickly by this method, and the whole sorting process can be done recursively, so that the whole data becomes an ordered sequence.

```"""
quick_list = [69, 471, 106, 66, 149, 983, 160]

def quickSort(L, low, high):
i = low
j = high
if i >= j:
return L
key = L[i]
while i < j:
while i < j and L[j] >= key:
j = j-1
L[i] = L[j]
while i < j and L[i] <= key:
i = i+1
L[j] = L[i]
L[i] = key
quickSort(L, low, i-1)
quickSort(L, j+1, high)
return
quickSort(quick_list,0,len(quick_list)-1)
print quick_list                              ```

Posted by mbariou on Fri, 28 Feb 2020 08:45:28 -0800