您的位置:首页 > 编程语言 > Python开发

python实现2种简单的排序算法--冒泡排序和选择排序

2017-08-20 23:39 861 查看
一、冒泡排序

基本原理:将待比较的数组中的两个元素两两比较,一个循环比较完成后,最大的那个数会沉到数组的最后一个。比如要求排序后数组要按从小到达的顺序排列,有一个数组的数据个数是5个,那么最外层循环需要比较5次。

array = [3, 8 , 9, 5, 1]


第一次比较后,结果为:

[3, 8, 5, 1, 9]


第二次比较后,结果为:

[3, 5, 1, 8, 9]


第三次比较后,结果为:

[3, 1, 5, 8, 9]


第四次比较后,结果为:

[1, 3, 5, 8, 9]


第五次比较后,结果为:

[1, 3, 5, 8, 9]


二.简单选择排序算法和优化后的选择排序算法

1.简单选择排序算法的原理是:

从数组中的第一个数据开始,每次循环都将数组划分成2个子集,第一个子集中含有第一个数,剩下的算作一个子集,拿第一个子集中的唯一的数和第二个子集中的每个数进行比较,当第一个子集中的数大于第二个子集中的数时,就两两交换。还是以上面的数组作为例子,
array = [3, 8 , 9, 5, 1]


第一次循环后,结果为:

array = [1, 8 , 9, 5, 3]


第二次比较后,结果为:

[1, 3, 9, 8, 5]


第三次比较后,结果为:

[1, 3, 5, 9, 8]


第四次比较后,结果为:

[1, 3, 5, 8, 9]


第五次比较后,结果为:

[1, 3, 5, 8, 9]


2 优化后的简单选择排序

我们在上面的选择排序中,对于第二个子集中的每个数和第一个子集中的数进行比较后,若是小于第一个子集的数,都要和第一个子集中的数进行交换,这样交换次数太多了,我们可不可以这样?先找到第二个子集中的最小的数,然后记录下它在数组中的位置(index),然后拿这个数和和第一个子集中的数据进行交换

还是以上面的数据为例子。
array = [3, 8 , 9, 5, 1]


第一次循环后,结果为:

array = [1, 8 , 9, 5, 3]


第二次比较后,结果为:

[1, 3, 9, 5, 8]


第三次比较后,结果为:

[1, 3, 5, 9, 8]


第四次比较后,结果为:

[1, 3, 5, 8, 9]


第五次比较后,结果为:

[1, 3, 5, 8, 9]


我们的代码如下:

def bubble_sort(array):
start_bubble = time.time()
for i in range(len(array)):
for j in range(len(array)-i-1):
if array[j] > array[j+1]:
temp = array[j]
array[j] = array[j+1]
array[j+1] = temp
print "the in circle's sort is done, the result is:",array
end_bubble = time.time()
print 'bubble time is:', end_bubble - start_bubble

def selection_sort(array):
start_sort = time.time()
for i in range(len(array)):
for j in range(i, len(array)):
if array[i] > array[j]:
temp = array[i]
array[i] = array[j]
array[j] = temp
print "the selection sort in circle is done.the result is:", array
end_sort = time.time()
print 'sort time is:', end_sort - start_sort

def modified_selection_sort(array):
start_modi_sort = time.time()
for i in range(len(array)):
min = i
for j in range(i, len(array)):
if array[min] > array[j]:
min = j
temp = array[i]
array[i] = array[min]
array[min] = temp
print "the modi sort in circle is done.the result is:", array
end_modi_sort = time.time()
print 'modi sort time is:', end_modi_sort - start_modi_sort


运行后的结果如下:

###########
the in circle's sort is done, the result is: [3, 8, 5, 1, 9]
the in circle's sort is done, the result is: [3, 5, 1, 8, 9]
the in circle's sort is done, the result is: [3, 1, 5, 8, 9]
the in circle's sort is done, the result is: [1, 3, 5, 8, 9]
the in circle's sort is done, the result is: [1, 3, 5, 8, 9]
bubble time is: 4.10079956055e-05
None
the selection sort in circle is done.the result is: [1, 8, 9, 5, 3]
the selection sort in circle is done.the result is: [1, 3, 9, 8, 5]
the selection sort in circle is done.the result is: [1, 3, 5, 9, 8]
the selection sort in circle is done.the result is: [1, 3, 5, 8, 9]
the selection sort in circle is done.the result is: [1, 3, 5, 8, 9]
sort time is: 2.50339508057e-05
None
the modi sort in circle is done.the result is: [1, 8, 9, 5, 3]
the modi sort in circle is done.the result is: [1, 3, 9, 5, 8]
the modi sort in circle is done.the result is: [1, 3, 5, 9, 8]
the modi sort in circle is done.the result is: [1, 3, 5, 8, 9]
the modi sort in circle is done.the result is: [1, 3, 5, 8, 9]
modi sort time is: 5.79357147217e-05
None
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐