python实现2种简单的排序算法--冒泡排序和选择排序
2017-08-20 23:39
861 查看
一、冒泡排序
基本原理:将待比较的数组中的两个元素两两比较,一个循环比较完成后,最大的那个数会沉到数组的最后一个。比如要求排序后数组要按从小到达的顺序排列,有一个数组的数据个数是5个,那么最外层循环需要比较5次。
第一次比较后,结果为:
第二次比较后,结果为:
第三次比较后,结果为:
第四次比较后,结果为:
第五次比较后,结果为:
二.简单选择排序算法和优化后的选择排序算法
1.简单选择排序算法的原理是:
从数组中的第一个数据开始,每次循环都将数组划分成2个子集,第一个子集中含有第一个数,剩下的算作一个子集,拿第一个子集中的唯一的数和第二个子集中的每个数进行比较,当第一个子集中的数大于第二个子集中的数时,就两两交换。还是以上面的数组作为例子,
第一次循环后,结果为:
第二次比较后,结果为:
第三次比较后,结果为:
第四次比较后,结果为:
第五次比较后,结果为:
2 优化后的简单选择排序
我们在上面的选择排序中,对于第二个子集中的每个数和第一个子集中的数进行比较后,若是小于第一个子集的数,都要和第一个子集中的数进行交换,这样交换次数太多了,我们可不可以这样?先找到第二个子集中的最小的数,然后记录下它在数组中的位置(index),然后拿这个数和和第一个子集中的数据进行交换
还是以上面的数据为例子。
第一次循环后,结果为:
第二次比较后,结果为:
第三次比较后,结果为:
第四次比较后,结果为:
第五次比较后,结果为:
我们的代码如下:
运行后的结果如下:
基本原理:将待比较的数组中的两个元素两两比较,一个循环比较完成后,最大的那个数会沉到数组的最后一个。比如要求排序后数组要按从小到达的顺序排列,有一个数组的数据个数是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
相关文章推荐
- python 实现排序算法(三)-选择排序和冒泡排序
- 几种常用的排序算法:插入排序、冒泡排序、选择排序的算法及C++实现
- 三种排序算法python源码——冒泡排序、插入排序、选择排序
- C#实现所有经典排序算法(选择排序,冒泡排序,快速排序,插入排序,希尔排序)
- 基本算法简单实现-二分法查找、合并排序、冒泡排序、插入排序、选择排序、快速排序
- 简单的排序算法——插入排序,选择排序,交换排序(冒泡排序,快速排序)
- 排序算法(C实现)--------- 简单选择排序
- C++实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
- 十二.C语言8种排序算法及其实现 1.希尔排序 2.二分插入法 3.直接插入法 4.带哨兵的直接排序法 5.冒泡排序 6.选择排序 7.快速排序 8.堆排序
- 简单的排序算法——插入排序,选择排序,交换排序(冒泡排序,快速排序)
- Python实现冒泡,插入,选择排序简单实例
- C#实现所有经典排序算法(选择排序 冒泡排序 快速排序)
- Java实现八个常用的排序算法:插入排序、冒泡排序、选择排序、希尔排序等
- JAVA实现简单的冒泡排序与选择排序
- 选择排序,冒泡排序(python实现)
- 几种常用的排序算法的分析及java实现(希尔排序,堆排序,归并排序,快速排序,选择排序,插入排序,冒泡排序)
- JAVA之冒泡排序与简单选择排序的实现
- 各种排序算法实现——基数排序、归并排序、插入排序、冒泡排序、选择排序、快速排序、堆排序、希尔排序
- Python实现冒泡,插入,选择排序简单实例
- C#实现所有经典排序算法(选择排序 冒泡排序 快速排序)