python算法之排序

#冒泡排序
#基本思想:两两相邻记录的关键字,如果反序则交换,直到没有反序的记录为止
#要点:1.两两注意是相邻的两个元素的意思
# 2.如果有n个元素需要比较n-1次,每一轮减少1次比较
# 3.既然叫冒泡排序,那就是从下往上两两比较,所以看上去就跟泡泡往上冒一样。
def bubblesort(numList):
leng = len(numList)-1
for i in range(leng): #i=0
for j in range(leng-i):#j=0,1,2,3,4,5,6
if numList[leng-j] < numList[leng-j-1]:#leng-j=6,leng-j-1=5
midnum = numList[leng-j]
numList[leng-j] = numList[leng-j-1]
numList[leng-j-1] = midnum
print(numList)
return numList
print(bubblesort([1,2,-1,8,6,7,-2]))
# [-2, 1, 2, -1, 8, 6, 7] 索引0~索引6,从后往前,相邻元素两两相比,如果错位,进行元素互换
# [-2, -1, 1, 2, 6, 8, 7] 索引0~索引5,从后往前,相邻元素两两相比,如果错位,进行元素互换
# [-2, -1, 1, 2, 6, 7, 8] 索引0~索引4,从后往前,相邻元素两两相比,如果错位,进行元素互换
# [-2, -1, 1, 2, 6, 7, 8] 索引0~索引3,从后往前,相邻元素两两相比,如果错位,进行元素互换
# [-2, -1, 1, 2, 6, 7, 8] 索引0~索引2,从后往前,相邻元素两两相比,如果错位,进行元素互换
# [-2, -1, 1, 2, 6, 7, 8] 索引0~索引1,从后往前,相邻元素两两相比,如果错位,进行元素互换

#插入排序
#基本思想:将一个记录插入到已经排好序的有序表中,从而得到一个新的、记录数增加1的有序表。
def insertsort(numList):
leng = len(numList)
for i in range(1,leng):
if numList[i] < numList[i-1]:
midnum = numList[i]
for j in range(i):
if numList[i-j-1]>midnum:
numList[i-j] = numList[i-j-1]
numList[i-j-1] = midnum
print(numList)
return numList
print(insertsort([1,2,-1,8,6,7,-2]))
# [1, 2, -1, 8, 6, 7, -2] 从原始数组中,从索引1开始,与索引0元素相比,如果遇到错位的,把这个索引的值插到正确的位置
# [-1, 1, 2, 8, 6, 7, -2] 继上次更改数据,从索引2开始,与索引1元素相比,如果遇到错位的,把这个索引的值插到正确的位置
# [-1, 1, 2, 8, 6, 7, -2] 继上次更改数据,从索引3开始,与索引2元素相比,如果遇到错位的,把这个索引的值插到正确的位置
# [-1, 1, 2, 6, 8, 7, -2] 继上次更改数据,从索引4开始,与索引3元素相比,如果遇到错位的,把这个索引的值插到正确的位置
# [-1, 1, 2, 6, 7, 8, -2] 继上次更改数据,从索引5开始,与索引4元素相比,如果遇到错位的,把这个索引的值插到正确的位置
# [-2, -1, 1, 2, 6, 7, 8] 继上次更改数据,从索引6开始,与索引5元素相比,如果遇到错位的,把这个索引的值插到正确的位置

#选择排序
#基本思想:通过n-i次关键字间的比较,从n-i+1个记录中选出关键字最小的记录,并和第i(1<=i<=n)个记录交换。
def selectsort(numList):
leng = len(numList)
for i in range(leng-1):
temp_min = numList[i+1]
j_index = i+1
for j in range(i+1,leng):
if numList[j] < temp_min:
temp_min = numList[j]
j_index = j
if numList[i] > temp_min:
temp = numList[i]
numList[i] = temp_min
numList[j_index] = temp
print(numList)
return numList
print(selectsort([1,2,-1,8,6,7,-2]))
# [-2, 2, -1, 8, 6, 7, 1] 从原始数组中,记索引0的值为s,找出索引1~索引6最小值t,如果s>t,则交换两个值
# [-2, -1, 2, 8, 6, 7, 1] 继上次更改数据,记索引1的值为s,找出索引2~索引6最小值t,如果s>t,则交换两个值
# [-2, -1, 1, 8, 6, 7, 2] 继上次更改数据,记索引2的值为s,找出索引3~索引6最小值t,如果s>t,则交换两个值
# [-2, -1, 1, 2, 6, 7, 8] 继上次更改数据,记索引3的值为s,找出索引4~索引6最小值t,如果s>t,则交换两个值
# [-2, -1, 1, 2, 6, 7, 8] 继上次更改数据,记索引4的值为s,找出索引5~索引6最小值t,如果s>t,则交换两个值
# [-2, -1, 1, 2, 6, 7, 8] 继上次更改数据,记索引5的值为s,找出索引6~索引6最小值t,如果s>t,则交换两个值

#快速排序:
#基本思想:递归思想,在每次调用时都将第一个元素作为基准,把所有小于这个数的数放左边,大于这个数的数放右边
def swap(numList, low, high):
temp = numList[low]
numList[low] = numList[high]
numList[high] = temp
def Partition(numList, low, high):
point = numList[low]
while low < high:
while low= point:
high -= 1
swap(numList, low, high)
while low low += 1
swap(numList, low, high)
print(numList)
return low
def quicksort(numList, low, high):
print(low)
if low < high:
point = Partition(numList, low, high)
quicksort(numList, low, point-1)
quicksort(numList, point+1, high)
return numList
numList = [66,13,51,76,81,26,57,69,23]
print(quicksort(numList, 0, len(numList)-1))
# [23, 13, 51, 57, 26, 66, 81, 69, 76] [23, 13, 51, 57, 26] [66] [81, 69, 76]
# [13, 23, 51, 57, 26, 66, 81, 69, 76] [13] [23] [51, 57, 26] [66] [81, 69, 76]
# [13, 23, 26, 51, 57, 66, 81, 69, 76] [13] [23] [26] [51] [57] [66] [81, 69, 76]
# [13, 23, 26, 51, 57, 66, 76, 69, 81] [13] [23] [26] [51] [57] [66] [76, 69] [81]
# [13, 23, 26, 51, 57, 66, 69, 76, 81] [13] [23] [26] [51] [57] [66] [69] [76] [81] low=high=6,终止

你可能感兴趣的:(python算法之排序)