defsort(arr):iflen(arr)<=1:returnarrpivot=arr[0]left=[xforxinarrifx<pivot]middle=[xforxinarrifx==pivot]right=[xforxinarrifx>pivot]returnsort(right)+middle+sort(left)l=[5,2,7,8,6,1,4,9,10,1,2,
importmathdefsort(arr):num_count=len(arr)n=0whileTrue:temp_arr=[]group_size=2**nforiinrange(math.ceil(num_count/group_size/2)):left=arr[group_size*(2*i):group_size*(2*i+1)]right=arr[group_size*(2*i+1)
defsort(arr):count=len(arr)foriinrange(1,count):j=iwhilej>0andarr[j-1]<arr[j]:arr[j-1],arr[j]=arr[j],arr[j-1]j-=1returnarrl=[5,2,7,8,6,1,4,9,10,1,2,3,4]print(sort(l))
defsort(arr):count=len(arr)foriinrange(count):max=arr[i]forjinrange(count-i):ifmax<arr[i+j]:max,arr[i+j]=arr[i+j],maxarr[i]=maxreturnarrl=[5,2,7,8,6,1,4,9,10,1,2,3,4]print(sort(l))
defsort(arr):count=len(arr)-1foriinrange(count):forjinrange(count-i):ifarr[j]<arr[j+1]:arr[j],arr[j+1]=arr[j+1],arr[j]returnarrl=[5,2,7,8,6,1,4,9,10,1,2,3,4]print(sort(l))
importmathdefbuild_heap(arr):#创建堆foriinrange(1,len(arr)):s=ip=math.ceil(s/2)-1whilep>=0:ifarr[s]>arr[p]:arr[s],arr[p]=arr[p],arr[s]else:breaks=pp=math.ceil(s/2)-1returnarrdefsort(arr):#创建排序列表res
Copyright © 2005-2024 51CTO.COM 版权所有 京ICP证060544号