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

Python 实现排序算法-快速、冒泡、归并

2017-02-22 19:45 363 查看
#==============================================================================
# # -*- coding: utf-8 -*-
# """
# Created on Mon Feb 20 10:48:50 2017
#
# @author: zzpp220
# """
'''排序算法---

快速排序'''
__metadata__=type
#==============================================================================
class QuickSort:
##单指针方法,把首元素最作为基准,顺序遍历,小于他的弹出,然后插入数组首位
def singpoi_partition(self,arr,first,end):
##要判断first 的关系,当只有一个元素和没有元素的时候,就返回
if first<end:
key=arr[first]

for i in range(first+1,end+1):##以第一个作为key
if arr[i] < key:
arr.insert(first,arr.pop(i))
index_key=arr.index(key)
self.singpoi_partition(arr,first,index_key-1)
self.singpoi_partition(arr,index_key+1,end)

return None
#双指针方法
#     def quick_sort(array,low,high):
#        if low < high:
#            key_index = sub_sort(array,low,high)
#            quick_sort(array,low,key_index)
#            quick_sort(array,key_index+1,high)
#
#     def sub_sort(array,low,high):
#         key = array[low]
#         while low < high:
#             while low < high and array[high] >= key:
#                 high -= 1
#             while low < high and array[high] < key:
#                 array[low] = array[high]
#                 low += 1
#                 array[high] = array[low]
#         array[low] = key
#         return low

#==============================================================================

'''归并排序'''
class Recur_Order:

def recur_Order(self,arr):

if len(arr) <=1:
return arr

mid=(len(arr)-1)//2
first=arr[:mid+1]
second=arr[mid+1:]

one=self.recur_Order(first)
two=self.recur_Order(second)
return self.sub_order(one,two)

def sub_order(self,first,second):
i,j=0,0
res=[]
while first and second and i<len(first) and j<len(second):

while i<len(first) and j<len(second) and first[i] <=second[j]:
res.append(first[i])
i+=1
while i<len(first) and j<len(second) and first[i] >second[j]:
res.append(second[j])
j+=1
if first and second:
res+=first[i:] if i <len(first) else second[j:]#是等号之后的表达式
return res
#=========================insert_sort=========================
class Insert_Order:
def insert_order(self,arr):
if not arr:
return None
if len(arr)<=1:
return arr
res=[]
##先把首字母放进去
res.append(arr[0])
i,j=1,-1
while i<len(arr):
#循环比较数组中的下一个元素和res的最后元素比较,若大或相等直接加入,再比较下一份
while i<len(arr) and arr[i]>=res[-1]:
res.append(arr[i])
i+=1
#如果小于 则依次比较和res中的倒数第下一个元素计较
while i<len(arr) and j>=-len(res) and arr[i]<res[j]:
j-=1
#如果大于或者等于他,即直接插在他后面,然后i+1
if j>=-len(res) and arr[i]>=res[j]:
res.insert(j+1,arr[i])
j=-1
i+=1
break
#如果一直遍历到res的头也没发现比他更小的,就直接插在他前面
if j<=-len(res):
res.insert(j+1,arr[i])
j=-1
i+=1
return res

#'''不是说插入排序就是一个一个插入,更简单的方法如下,顺序遍历元素,挑出剩余最小的,然后和当前的互换位置,注意如果有值相同的条件,因此index要从i+开始 这一点要注意 还有就是判断i+1是否有意义,是否超出了l的边界,遍历完成后,返回的即为排序好的'''
def easier_insert_sort(self,l):

for i in range(len(l)):
min_index=l.index(min(l[i+1:]),i+1) if i < (len(l)-2) else l.index(min(l[i:]))
l[i],l[min_index]=l[min_index],l[i]

return l

def bubble(arr):
if not arr:
return None
for t in range(len(arr)):
for i in range(len(arr)-1-t):
if arr[i]<=arr[i+1]:
pass
else:
arr[i],arr[i+1]=arr[i+1],arr[i]
print arr
return arr
if __name__=='__main__':
arr=[10,4,7,-4,12,5,-23,5,34,7,23,75,2,0]
l = [4,1,9,13,34,26,10,7,4]
#==============================================================================
#     solution=QuickSort()
#     solution.singpoi_partition(arr,0,len(arr)-1)
#     print arr
#    solution=Recur_Order()
#   print solution.recur_Order(arr)
#==============================================================================
solution=Insert_Order()
#print solution.insert_order(l)#None [] [1]
#print solution.easier_insert_sort(l)
print bubble(arr)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐