数据结构--排序--快排and冒泡(python)
听说大厂面试,限时两分钟写出来快排。。。
闲着没事,写了一下。。。
1 def Partition(L,low,high): 2 pivotkey = L[low] 3 while low<high: 4 while low<high and L[high]>=pivotkey: 5 high-=1 6 L[low],L[high]=L[high],L[low] 7 while low<high and L[low]<=pivotkey: 8 low+=1 9 L[low],L[high]=L[high],L[low] 10 return low 11 def Qsort(L,low,high): 12 if low < high: 13 pivot = Partition(L,low,high) 14 Qsort(L,low,pivot-1) 15 Qsort(L,pivot+1,high)
冒泡:
1 def bubbleSort(nums): 2 for i in range(len(nums)): 3 for j in range(len(nums)-1,i,-1): 4 if nums[j] < nums[j-1]: 5 temp = nums[j] 6 nums[j] = nums[j-1] 7 nums[j-1]=temp 8 return nums