Skip to content

Add quicksort #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
add quicksort
  • Loading branch information
vincenzobaz committed Jul 28, 2016
commit 5509981c5c3b17055bba940cbbea9de86e0f1187
29 changes: 29 additions & 0 deletions QuickSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

def quicksort(A, p, r):
if p < r:
q = partition(A, p, r)
quicksort(A, p, q - 1)
quicksort(A, q + 1, r)


def partition(A, p, r):
x = A[r]
i = p - 1
for j in range(p, r):
if A[j] <= x:
i += 1
tmp = A[i]
A[i] = A[j]
A[j] = tmp
tmp = A[i+1]
A[i+1] = A[r]
A[r] = tmp
return i + 1


if __name__ == "__main__":
A = [8, 4, 5, 7, 1, 2, 3, 6]
# partition(A, 0, 7)
print(A)
quicksort(A, 0, 7)
print(A)