基本排序的Python实现
来源:互联网 发布:剑灵人族萝莉捏脸数据 编辑:程序博客网 时间:2024/05/29 14:23
地精排序:
#! /usr/bin/env python#coding=utf-8import randomdef gnomesort(lst): i = 0 while i < len(lst): if i == 0 or lst[i-1] <= lst[i]: i += 1 else: lst[i], lst[i-1] = lst[i-1], lst[i] i -= 1def main(): lst = [random.randint(1,20) for i in range(10)] print(lst) gnomesort(lst) print(lst) if __name__ == "__main__": main()
冒泡排序:
#! /usr/bin/env python#coding=utf-8import randomdef bubble_sort_raw(seq): pass_cnt = len(seq) - 1 for i in range(pass_cnt): for j in range(len(seq)-i-1): if seq[j] > seq[j + 1]: seq[j], seq[j+1] = seq[j+1], seq[j] def bubble_sort_adv(seq): pass_cnt = len(seq) - 1 exchanged = True while pass_cnt > 0 and exchanged: exchanged = False for i in range(pass_cnt): if seq[i] > seq[i+1]: exchanged = True seq[i],seq[i+1] = seq[i+1],seq[i] pass_cnt = pass_cnt - 1 if __name__ == '__main__': seq = [random.randint(0,20) for i in range(10)] print(seq) bubble_sort_raw(seq) print(seq) seq = [random.randint(0,20) for i in range(10)] print(seq) bubble_sort_adv(seq) print(seq)
选择排序:
#! /usr/bin/env python#coding=utf-8import randomdef select_sort(lst): pass_cnt = len(lst) - 1 for i in range(pass_cnt): pos_of_min = i for j in range(i+1,pass_cnt+1): if lst[j] < lst[pos_of_min]: pos_of_min = j if pos_of_min != i: lst[i],lst[pos_of_min] = lst[pos_of_min],lst[i] def select_sort_rec(lst,i): #select sort recursive version if i == 0: return pos_of_max = i for j in range(i): if lst[j] > lst[pos_of_max]: pos_of_max = j lst[i],lst[pos_of_max] = lst[pos_of_max],lst[i] select_sort_rec(lst,i-1) if __name__ == "__main__": lst = [random.randint(0,20) for i in range(10)] print(lst) select_sort(lst) print(lst) lst = [random.randint(0,20) for i in range(10)] print(lst) select_sort_rec(lst,len(lst)-1) print(lst)
插入排序:
#! /usr/bin/env python#coding=utf-8import randomdef ins_sort(lst): for i in range(1,len(lst)): pivot = lst[i] while (i > 0 and lst[i-1] > pivot): lst[i] = lst[i-1] i = i - 1 lst[i] = pivotdef ins_sort_binarysearch(lst): for i in range(1,len(lst)): pivot = lst[i] low = 0 high = i - 1 while (low <= high): mid = (high + low) >> 1 if lst[mid] > pivot: high = mid - 1 else: low = mid + 1 while (i > low): lst[i] = lst[i-1] i = i - 1 lst[low] = pivot def ins_sort_rec(lst,i): if i == 0: return ins_sort_rec(lst,i-1) j = i while j > 0 and lst[j-1] > lst[j]: lst[j-1],lst[j] = lst[j],lst[j-1] j = j - 1 if __name__ == '__main__': lst = [random.randint(0,20) for i in range(10)] lst2 = lst[:] lst2.sort() print(lst) ins_sort(lst) print(lst2) print(lst) lst = [random.randint(0,20) for i in range(10)] lst2 = lst[:] print(lst) lst2.sort() print(lst2) ins_sort_binarysearch(lst) print(lst)
0 0
- 基本排序的Python实现
- 基本排序算法的Python实现
- 基本排序算法的Python实现
- 基本排序算法:Python实现
- 总结:python关于一些基本排序算法的实现
- Python实现基本排序算法01
- 基本排序算法的实现
- 基本排序算法的实现
- 基本的排序方法实现
- 汉诺伊塔 的Python基本实现
- Python的5个基本排序算法
- python 排序的简单实现
- Python 实现的"冒泡排序"
- 快速排序的python实现
- 冒泡排序的python实现
- 冒泡排序的Python实现
- 希尔排序的Python实现
- 归并排序的Python实现
- rails中validates_confirmation_of验证方法无效的解决办法
- Android程序崩溃 错误日志
- 随机产生50个0~6之间的整数,分别统计每个数的个数。
- JAVA EE ——structs 配置
- 白话数据结构之【最小生成树】
- 基本排序的Python实现
- android应用开发全程实录-你有多熟悉listview?
- cuda遇到的问题1
- 测试6年来的知识大纲
- C++函数后面跟一个const表示该函数是常成员函数
- [6]智能指针boost::shared_ptr
- Mybatis深入-如何使用传递不同类型的参数(以及等量替换符号的使用)
- log4net 记录MVC监控日志
- 键盘读入