hustcc / hustcc/JS-Sorting-Algorithm

快速排序的实现方式有优化空间

Open
#64 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.2k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

快速排序的精髓在于“减少swap的次数”,目前的实现代码中,swap次数偏多,不够精简,在GIF动图的演示里也能看出问题。
正确的实现方式可以参考[wikipedia](https://zh.wikipedia.org/zh-sg/快速排序),以下是python实现方式的代码:
```python
def quickSort(arr, left=None, right=None):
left = 0 if not isinstance(left,(int, float)) else left
right = len(arr)-1 if not isinstance(right,(int, float)) else right
if left < right:
partitionIndex = partition(arr, left, right)
quickSort(arr, left, partitionIndex-1)
quickSort(arr, partitionIndex+1, right)
return arr

def partition(arr, left, right):
pivot = arr[right]
i, j= left, right - 1
while True:
while i<=j and arr[i]=pivot:
j-=1
if i

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.