python算法之冒泡排序和选择排序

一、冒泡排序(Bubble sort)

Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorithm that repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

Properties

  • Worst case performance O(n2)
  • Best case performance O(n)
  • Average case performance O(n2)
#冒泡排序,将一个数组进行从小到大进行排序
array_a= [80, 24, 28, 33, 19, 44, 8, 17, 22, 16, 12]#定义一个数组,define a array
array_b=[88,21,19,17,19,22,44,28,22,10,5,6]#定义一个数组 define a array
def bubble_sort_min(array_sort): #定义一个buuble_sort函数,该函数参数为数组,返回值为从小到大排序的数组。define a function,the fuction has a array parameter.
#loop_count=0
for i in range(len(array_sort)-1): #数组个数有11个,那么使用冒泡排序需要循环11-1次。n个数,循环n-1次
#print("i=",i)
#print("leni=",len(array_sort)-1)
for j in range(len(array_sort)-i-1): #每循环一次,两两比较,找到最大值放到数组中最后一个位置。第一次循环需要比较10次,第二次比较9次,第三次比较8次...。第一次循环n-1次,第二次循环n-2次,第n-1次循环1次
#print("j=",j)
#print("lenj=", len(array_sort)-i-1)
if array_sort[j]>array_sort[j+1]:#比较两个相邻数组元素的大小
array_sort[j],array_sort[j+1]=array_sort[j+1],array_sort[j]#如果前面的大就交换位置
#loop_count+=1
print("loop_count=", loop_count) #循环次数为n(n+1)/2次
return array_sort#返回排序好的数组 return a new array.
def bubble_sort_max(array_sort): #定义一个buuble_sort函数,该函数参数为数组,返回值为从大到小排序的数组。define a function,the fuction has a array parameter.
for i in range(len(array_sort)-1): #数组个数有11个,那么使用冒泡排序需要循环11-1次。n个数,循环n-1次
for j in range(len(array_sort)-i-1): #每循环一次,两两比较,找到最大值放到数组中最后一个位置。第一次循环需要比较10次,第二次比较9次,第三次比较8次...。
if array_sort[j]<array_sort[j+1]:#比较两个相邻数组元素的大小
array_sort[j],array_sort[j+1]=array_sort[j+1],array_sort[j]#如果前面的小就交换位置
return array_sort#返回排序好的数组 return a new array.
bubble_sort_min(array_a)#调用函数 call bubble_sort function
#bubble_sort(array_b)#调用函数
bubble_sort_max(array_b)
print("array_a=",array_a)#打印输出第一个排好序的数组 print array
print("array_b=",array_b)#打印输出第二个排好序的数组

二、选择排序(Selection sort)

The algorithm works by selecting the smallest unsorted item and then swapping it with the item in the next position to be filled.
The selection sort works as follows: you look through the entire array for the smallest element, once you find it you swap it
(the smallest element) with the first element of the array. Then you look for the smallest element in the remaining array
(an array without the first element) and swap it with the second element. Then you look for the smallest element in the remaining
array (an array without first and second elements) and swap it with the third element, and so on. Here is an example,
array_a=[33,32,31,30,29,28,27,26,25,24,23]
#array_a=[23,21,11,33,34,12,45,17]
small_num_index=0 #初始列表下标,默认是数组的第一个元素的下标,也就是array_a[0]
loop_count=0 #统计循环共执行了次数。
swap_count=0 #统计排序时数组元素的值实际交换的次数
for i in range(len(array_a)-1): #大循环,循环的次数应该等于数组个数n减去1次,即n-1次
print("small_num_index3=", small_num_index) #每次循环时最小值下标值,第一次循环为0,第二次循环为1,第三次循环为2....
for j in range(i,len(array_a)): #小循环,小循环的开始下标值为大循环下标,结束为数组长度-1
if array_a[j]<array_a[small_num_index]: #如果该数组元素比每次最小下标数组元素的值小则将最小下标值改为该元素下标
small_num_index=j
swap_count+=1
loop_count+=1
else:
print("the smallest num is:", array_a[small_num_index]) #每次执行完后找到最小值得数组元素
print("small_num_index1=",small_num_index)
array_a[small_num_index],array_a[i]=array_a[i],array_a[small_num_index] #将最小下标元素和最小值元素的值互换,即将最小值放在每次小循环最小下标内。
small_num_index=i+1 #将最小值下标设置为下一次循环下标最小值
print("small_num_index2=", small_num_index)
print("array_a selection_sort:",array_a)
print("swap_count=",swap_count)
print("loop_count=",loop_count) #循环次数为(n+2)(n-1)/2 次

三、两个排序的区别联系

1.冒泡排序是通过数组相邻元素两两比较,大的排后面,一次比较找到整个数组最大值放在数组最后一个位置。小循环时,通过控制小循环的循环下标range j=len(array)-i-1忽略掉数组最后已经排列好的一个元素,从后往前直到最前面。

2.选择排序是通过从前往后,从第一个数组元素和后面的元素比较,如果后面的元素小,就把该元素的值和后面的继续比较,找到最小值后,和第一个数组元素的值交换,把最小值放到数组的第一个位置。然后通过控制小循环的下标(j in range(i,len(array)))忽略掉第一个已经排好序的数组元素,从第二个元素开始继续循环找到第二小值,放到第二个元素上,以此类推,直至最后。

3.选择排序因每次循环直接找到最小值,因此需要一个变量small_num_index来记录最小值的数组元素下标。而冒泡因是两两比较,每次将大的值放到后面,所以不需要额外的变量。

4.冒泡排序执行循环次数为n(n+1)/2次,选择排序执行循环次数(n+2)(n-1)/2 次。

猜你喜欢

转载自www.cnblogs.com/xsfy/p/10788466.html
今日推荐