整数排序 II - quick_sort and merge_sort
https://www.lintcode.com/problem/464/?utm_source=sc-cheatsheet-cyc
输入:[3,2,1,4,5],输出:[1,2,3,4,5]。输入:[2,3,1],输出:[1,2,3]。```python
from typing import (
List,
)
class Solution:
"""
@param a: an integer array
@return: nothing
"""
def sort_integers2(self, a: List[int]):
# write your code here
self.quick_partition(a, 0, len(a)-1) #err: len(a)
def quick_partition(self, a: List[int], start:int, end:int):
if start >= end: # start > end也可以
return
pivot = a[(start + end) // 2]
left, right = start, end
while left <= right:
while left <= right and a[left] < pivot: #err: a[left] <= pivot 有大小才有必要换,等于没必要
left += 1
while left <= right and a[right] > pivot: #error: a[right] >= pivot
right -= 1
if left <= right:
a[left], a[right] = a[right], a[left]
left += 1 #err: missing 不要忘了这个
right -= 1 #err: missing
self.quick_partition(a, start, right)
self.quick_partition(a, left, end)
```Last updated