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

Appearance settings

Improved quick_sort implementation #52

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
Nov 18, 2018
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
39 changes: 13 additions & 26 deletions 39 sorting/quick_sort.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
#quick sort implementation in Python by Neville Antony
"""
Here is the implementation of quicksort algorithm in python by Pramod Bharti
quick_sort() function takes an unsorted array and prints sorted array
"""
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr) // 2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)

def partition(arr, down, up):
i = ( down - 1 )
pivot = arr[up]

for j in range(down, up):
if arr[j] <= pivot:
i = i+1
arr[i],arr[j] = arr[j],arr[i]

arr[i+1],arr[up] = arr[up],arr[i+1]
return ( i+1 )

def quickSort(arr, down, up):
if down< up:
pi = partition(arr, down, up)
quickSort(arr, down, pi-1)
quickSort(arr, pi+1, up)

arr = [91, 72, 68, 23, 37, 55]
n = len(arr)
quickSort(arr,0,n-1)
print("The sorted array is :")
for i in range(n):
print ("%d" %arr[i]),

print (quick_sort([5,2,8,3,9,12,43])) # This will print [2,3,5,8,9,12,43]
Morty Proxy This is a proxified and sanitized view of the page, visit original site.