Skip to content

Commit e832ece

Browse files
authored
Add Python
1 parent 1eb3418 commit e832ece

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

6.quickSort.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,34 @@ function swap(arr, i, j) {
6262
arr[i] = arr[j];
6363
arr[j] = temp;
6464
}
65-
```
65+
```
66+
67+
68+
## 4. Python 代码实现
69+
70+
```python
71+
def quickSort(arr, left=None, right=None):
72+
left = 0 if not isinstance(left,(int, float)) else left
73+
right = len(arr)-1 if not isinstance(right,(int, float)) else right
74+
print(left,right)
75+
if left < right:
76+
partitionIndex = partition(arr, left, right)
77+
quickSort(arr, left, partitionIndex-1)
78+
quickSort(arr, partitionIndex+1, right)
79+
return arr
80+
81+
def partition(arr, left, right):
82+
pivot = left
83+
index = pivot+1
84+
i = index
85+
while i <= right:
86+
if arr[i] < arr[pivot]:
87+
swap(arr, i, index)
88+
index+=1
89+
i+=1
90+
swap(arr,pivot,index-1)
91+
return index-1
92+
93+
def swap(arr, i, j):
94+
arr[i], arr[j] = arr[j], arr[i]
95+
```

0 commit comments

Comments
 (0)