前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >常见排序算法-Python实现

常见排序算法-Python实现

作者头像
marsggbo
发布2018-01-23 17:20:16
7740
发布2018-01-23 17:20:16
举报

常见排序算法-Python实现

python

排序

算法

1.二分法

python    32行

代码语言:js
复制
#coding=utf-8 
def binary_search(input_array, value): 
 """Your code goes here.""" 
    length = len(input_array) 
    left = 0 
    right = length-1 
 if length == 1: 
 return 0 if value == input_value[0] else -1 
 else: 
        mid = (left+right)/2 
 while(right-left>1): 
 if input_array[mid] == value: 
 return mid 
 elif input_array[mid] > value: 
                right = mid 
                mid = (left+right)/2 
 else: 
                left = mid 
                mid = (left+right)/2 
 if input_array[left] == value: 
 return left 
 elif input_array[right] == value: 
 return right 
 else: 
 return -1 
 
test_list = [1,3,9,11,15,19,29] 
test_val1 = 25 
test_val2 = 15 
print (binary_search(test_list, test_val1)) 
print (binary_search(test_list, test_val2)) 

2.冒泡法

python    60行

代码语言:js
复制
#coding=utf-8 
 
# way=1递增排序 way=0递减排序 
def bubble_sort(array,way=1): 
	length = len(array) 
 
 if not length: 
		print("Error!The length of array must be greater than 0.\n") 
 return 'Wrong array' 
 
 if way == 1: 
 while length > 0: 
 for i in range(length-1): 
 if array[i] > array[i+1]: 
					array[i],array[i+1] = array[i+1],array[i] 
			length -= 1 
 return array 
 
 elif way == 0: 
 while length > 0: 
 for i in range(length-1): 
 if array[i] < array[i+1]: 
					array[i],array[i+1] = array[i+1],array[i] 
			length -= 1 
 return array 
 
# 加入排序判断标志,可提高运行效率 
# way=1递增排序 way=0递减排序 
def better_bubble_sort(array,way=1): 
	is_sorted = True # 判断记录上次遍历是否进行过交换,若没有则表示不用再遍历了 
	length = len(array) 
 
 if not length: 
		print("Error!The length of array must be greater than 0.\n") 
 return 'Wrong array' 
 
 if way == 1: 
 while length > 0 and is_sorted: 
 for i in range(length-1): 
				is_sorted = False 
 if array[i] > array[i+1]: 
					array[i],array[i+1] = array[i+1],array[i] 
					is_sorted = True 
			length -= 1 
 return array 
 
 elif way == 0: 
 while length > 0 and is_sorted: 
 for i in range(length-1): 
				is_sorted = False 
 if array[i] < array[i+1]: 
					array[i],array[i+1] = array[i+1],array[i] 
					is_sorted = True 
			length -= 1 
 return array 
 
 
test =  [21, 4, 1, 3, 9, 20, 25, 6, 21, 14]  
print(better_bubble_sort(test,1)) 

3.插入排序

python    19行

代码语言:js
复制
#coding=utf-8 
 
def insert_sort(array): 
	length = len(array) 
	flag = array[0] 
 for x in range(1,length): 
 # 之前的 
 if array[x] < array[x-1]: 
			flag = array[x] 
			y = x 
 while y != 0 and array[y-1] > flag : 
				array[y] = array[y-1] 
				y -= 1 
			array[y] = flag 
 return array 
 
test = [21, 4, 1, 3, 9, 20, 25, 20, 3] 
print(insert_sort(test)) 

4.归并排序

python    31行

代码语言:js
复制
#coding=utf-8 
 
def merge_sort(array): 
 if len(array) <= 1: 
 return array 
 
	split_index = len(array)/2 
	left = merge_sort(array[:split_index]) 
	right = merge_sort(array[split_index:]) 
 return merge(left,right) 
 
def merge(left,right): 
	i = 0 
	j = 0 
	result = [] 
 while i < len(left) and j < len(right): 
 if left[i] <= right[j]: 
			result.append(left[i]) 
			i += 1 
 else: 
			result.append(right[j]) 
			j += 1 
 
	result += (left[i:]) 
	result += (right[j:]) 
 return result 
 
a = [1,2] 
test = [21, 4, 1, 3, 9, 20, 25]  
print(merge_sort(test)) 

5.选择排序

python    16行

代码语言:js
复制
#coding=utf-8 
 
def select_sort(array): 
	length = len(array) 
 # mini = array[0] 
 for i in range(length): 
		mini = array[i] 
 for j in range(i,length): 
 if array[j] < mini: 
				mini = array[j] 
				array[i],array[j] = array[j],array[i] 
 return array 
 
test = [21, 4, 1, 3, 9, 20, 25, 20, 3]  
print(select_sort(test)) 

6.快速排序

python    26行

代码语言:js
复制
#coding=utf-8 
 
# 递归 
def quick_sort(lists, left, right): 
 # 快速排序 
 if left >= right: 
 return lists 
    key = lists[left] 
    low = left 
    high = right 
 while left < right: 
 while left < right and lists[right] >= key: 
            right -= 1 
        lists[left] = lists[right] 
 while left < right and lists[left] <= key: 
            left += 1 
        lists[right] = lists[left] 
    lists[right] = key 
    quick_sort(lists, low, left - 1) 
    quick_sort(lists, left + 1, high) 
 return lists 
 
 
test = [21, 4, 1, 3, 9, 20, 25, 6, 21, 14] 
print (quick_sort(test,0,len(test)-1)) 

written by MARSGGBO 2017-2-14

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-03-26 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常见排序算法-Python实现
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档