Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ce8880

Browse files
quick sort
1 parent c9ede77 commit 3ce8880

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

‎Sorting Algorithms/Quick Sort/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# QUICK SORT
2+
3+
- Uses the logic of divide and conquer.
4+
- Pivot value divides the list into two.
5+
- Values ​​smaller than pivot value make up the left part of the list and larger values ​​make up the right part.
6+
7+
```python
8+
# recursive
9+
def quick_sort(arr):
10+
quick_sort_recursion(arr, 0, len(arr)-1)
11+
return arr
12+
13+
def quick_sort_recursion(arr,first,last):
14+
if first < last:
15+
splitpoint = partition(arr, first, last)
16+
# left right
17+
quick_sort_recursion(arr, first, splitpoint-1)
18+
quick_sort_recursion(arr, splitpoint+1, last)
19+
# end point
20+
21+
def partition(arr,first,last):
22+
# pivot value
23+
pivotvalue = arr[first]
24+
left = first + 1
25+
right = last
26+
done = False
27+
while not done:
28+
while left <= right and arr[left] <= pivotvalue:
29+
left = left + 1
30+
while arr[right] >= pivotvalue and right >= left:
31+
right = right - 1
32+
if right < left:
33+
done = True
34+
else:
35+
temp = arr[left]
36+
arr[left] = arr[right]
37+
arr[right] = temp
38+
39+
temp = arr[first]
40+
arr[first] = arr[right]
41+
arr[right] = temp
42+
43+
return right
44+
45+
arr = [3,2,13,4,6,5,743,6]
46+
print("Output: ", quick_sort(arr))
47+
```
48+
output:
49+
```
50+
[2, 3, 4, 5, 6, 6, 13, 743]
51+
```
52+
53+
54+
<img src="https://runestone.academy/runestone/books/published/pythonds/_images/partitionA.png" />
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# recursive
2+
def quick_sort(arr):
3+
quick_sort_recursion(arr, 0, len(arr)-1)
4+
return arr
5+
6+
def quick_sort_recursion(arr,first,last):
7+
if first < last:
8+
splitpoint = partition(arr, first, last)
9+
# left right
10+
quick_sort_recursion(arr, first, splitpoint-1)
11+
quick_sort_recursion(arr, splitpoint+1, last)
12+
# end point
13+
14+
def partition(arr,first,last):
15+
# pivot value
16+
pivotvalue = arr[first]
17+
left = first + 1
18+
right = last
19+
done = False
20+
while not done:
21+
while left <= right and arr[left] <= pivotvalue:
22+
left = left + 1
23+
while arr[right] >= pivotvalue and right >= left:
24+
right = right - 1
25+
if right < left:
26+
done = True
27+
else:
28+
temp = arr[left]
29+
arr[left] = arr[right]
30+
arr[right] = temp
31+
32+
temp = arr[first]
33+
arr[first] = arr[right]
34+
arr[right] = temp
35+
36+
return right
37+
38+
arr = [3,2,13,4,6,5,743,6]
39+
print("Output: ", quick_sort(arr))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /