听说大厂面试,限时两分钟写出来快排。。。
闲着没事,写了一下。。。
1 def quickSort(nums,low,high): 2 if low < high: 3 pivot = partition(nums,low,high) 4 quickSort(nums,low,pivot-1) 5 quickSort(nums,pivot+1,high) 6 return nums 7 def partition(nums,low,high): 8 pivot = nums[low] 9 while low < high:10 while low < high and nums[high] >= pivot:11 high-=112 swap(nums,low,high)13 while low < high and nums[low] <= pivot:14 low+=115 swap(nums,low,high)16 return low #pivot的位置17 def swap(nums,i,j):18 nums[i],nums[j]=nums[j],nums[i]
冒泡:
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]=temp8 return nums