Python bubble insert select sort simple instances
- 2020-04-02 13:54:19
- OfStack
The Python implementation of bubble, insert, selection and sorting simple examples described in this paper is more suitable for Python beginners to learn data structure and algorithm from the foundation, the example is simple and easy to understand, the specific code is as follows:
# -*- coding: cp936 -*-
#python Insertion sort
def insertSort(a):
for i in range(len(a)-1):
#print a,i
for j in range(i+1,len(a)):
if a[i]>a[j]:
temp = a[i]
a[i] = a[j]
a[j] = temp
return a
#Python Bubble sort
def bubbleSort(alist):
for passnum in range(len(alist)-1,0,-1):
#print alist,passnum
for i in range(passnum):
if alist[i]>alist[i+1]:
temp = alist[i]
alist[i] = alist[i+1]
alist[i+1] = temp
return alist
#Python Selection sort
def selectionSort(alist):
for i in range(len(alist)-1,0,-1):
maxone = 0
for j in range(1,i+1):
if alist[j]>alist[maxone]:
maxone = j
temp = alist[i]
alist[i] = alist[maxone]
alist[maxone] = temp
return alist
alist = [54,26,93,17,77,31,44,55,20]
#print bubbleSort(alist)
alist = [54,26,93,17,77,31,44,55,20]
print selectionSort(alist)
Interested friends can start to test this example, I believe that there will be new harvest.