From 839a84182c13751d35e83029c95085af0738dd11 Mon Sep 17 00:00:00 2001 From: alvin562 Date: Tue, 3 Jan 2017 00:08:42 -0800 Subject: [PATCH] updated version --- sorts/quick_sort.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index 257c21380..26b92fd3b 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -35,19 +35,26 @@ def quick_sort(collection): >>> quick_sort([-2, -5, -45]) [-45, -5, -2] """ - if len(collection) <= 1: + total_elements = len(collection) + + if total_elements <= 1: return collection less = [] equal = [] greater = [] pivot = collection[0] - for x in collection: - if x < pivot: - less.append(x) - elif x == pivot: - equal.append(x) + + 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(x) + greater.append(element) return quick_sort(less) + equal + quick_sort(greater)