- 快速排序
- 代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
def quick_sort(array, left, right):
if left > right:
return
t = left
i = left
j = right
while i != j:
while array[j] >= array[t] and j > i:
j -= 1
while array[i] <= array[t] and i < j:
i += 1
if i < j:
array[i], array[j] = array[j], array[i]
array[t], array[i] = array[i], array[t]
quick_sort(array, left, i - 1)
quick_sort(array, i + 1, right)
def main():
array = [6, 5, 2, 7, 3, 9, 8]
quick_sort(array, 0, len(array) - 1)
print(array)
if __name__ == "__main__":
main()