From 183df6889b6599dfd2c6c159128786805ce77bdf Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Wed, 5 Apr 2017 06:58:16 +0100 Subject: [PATCH 1/3] Code more concise and more readable --- sorts/quick_sort.py | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 26b92fd3b..c12689b9f 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -10,15 +10,9 @@ For manual testing run: 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 @@ -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__': @@ -70,4 +51,4 @@ if __name__ == '__main__': user_input = input_function('Enter numbers separated by a comma:\n') unsorted = [int(item) for item in user_input.split(',')] - print(sort(unsorted)) + print(quick_sort(unsorted)) From 8d06eb2c63e5a085e530a6d41014a6dfe4f3b09b Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Thu, 6 Apr 2017 03:44:47 +0100 Subject: [PATCH 2/3] Added some space before and after operators --- sorts/quick_sort.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index c12689b9f..5153a249a 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -30,13 +30,13 @@ def quick_sort(ARRAY): [-45, -5, -2] """ ARRAY_LENGTH=len(ARRAY) - if(ARRAY_LENGTH<=1): + 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) + 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__': From 7ae975922016417f507dabd136f960fe775d3840 Mon Sep 17 00:00:00 2001 From: Mehdi ALAOUI Date: Thu, 6 Apr 2017 03:55:28 +0100 Subject: [PATCH 3/3] Added some other spaces --- sorts/quick_sort.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 5153a249a..8974e1bd8 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -29,13 +29,13 @@ def quick_sort(ARRAY): >>> quick_sort([-2, -5, -45]) [-45, -5, -2] """ - ARRAY_LENGTH=len(ARRAY) + 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] + 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) @@ -50,5 +50,5 @@ if __name__ == '__main__': input_function = input user_input = input_function('Enter numbers separated by a comma:\n') - unsorted = [int(item) for item in user_input.split(',')] - print(quick_sort(unsorted)) + unsorted = [ int(item) for item in user_input.split(',') ] + print( quick_sort(unsorted) )