Data structure - Sort - quick discharge and bubbling (Python)

I heard an interview maker, limit two minutes to write out fast row. . .

Nothing to do, write a bit. . .

 

 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-=1
12         swap(nums,low,high)
13         while low < high and nums[low] <= pivot:
14             low+=1
15         swap(nums,low,high)
16     return low  #pivot的位置
17 def swap(nums,i,j):
18     nums[i],nums[j]=nums[j],nums[i]

bubble:

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

 

Guess you like

Origin www.cnblogs.com/NPC-assange/p/11448490.html