Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#73 from alaouimehdi1995/master
Browse files Browse the repository at this point in the history
More concise, optimized and readable code in sorts/quick_sort.py file
  • Loading branch information
AnupKumarPanwar authored Apr 6, 2017
2 parents d1ac134 + 7ae9759 commit 408c5d0
Showing 1 changed file with 11 additions and 30 deletions.
41 changes: 11 additions & 30 deletions sorts/quick_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,9 @@
python quick_sort.py
"""
from __future__ import print_function
from random import shuffle


def sort(collection):
shuffle(collection)
return quick_sort(collection)


def quick_sort(collection):
def quick_sort(ARRAY):
"""Pure implementation of quick sort algorithm in Python
:param collection: some mutable ordered collection with heterogeneous
Expand All @@ -35,27 +29,14 @@ def quick_sort(collection):
>>> quick_sort([-2, -5, -45])
[-45, -5, -2]
"""
total_elements = len(collection)

if total_elements <= 1:
return collection
less = []
equal = []
greater = []
pivot = collection[0]

equal.append(pivot)

for i in range(1, total_elements):
element = collection[i]

if element < pivot:
less.append(element)
elif element == pivot:
equal.append(element)
else:
greater.append(element)
return quick_sort(less) + equal + quick_sort(greater)
ARRAY_LENGTH = len(ARRAY)
if( ARRAY_LENGTH <= 1):
return ARRAY
else:
PIVOT = ARRAY[0]
GREATER = [ element for element in ARRAY[1:] if element > PIVOT ]
LESSER = [ element for element in ARRAY[1:] if element <= PIVOT ]
return quick_sort(LESSER) + [PIVOT] + quick_sort(GREATER)


if __name__ == '__main__':
Expand All @@ -69,5 +50,5 @@ def quick_sort(collection):
input_function = input

user_input = input_function('Enter numbers separated by a comma:\n')
unsorted = [int(item) for item in user_input.split(',')]
print(sort(unsorted))
unsorted = [ int(item) for item in user_input.split(',') ]
print( quick_sort(unsorted) )

0 comments on commit 408c5d0

Please sign in to comment.