From 66433a5f0bb822917e726e7eee8142e4375d2d35 Mon Sep 17 00:00:00 2001 From: yyeltsyn Date: Mon, 29 Aug 2016 11:29:49 +0300 Subject: [PATCH] Update quick_sort.py Streamlining quick_sort function --- sorts/quick_sort.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/sorts/quick_sort.py b/sorts/quick_sort.py index f8e61ede4..257c21380 100644 --- a/sorts/quick_sort.py +++ b/sorts/quick_sort.py @@ -35,21 +35,20 @@ def quick_sort(collection): >>> quick_sort([-2, -5, -45]) [-45, -5, -2] """ + if len(collection) <= 1: + return collection less = [] equal = [] greater = [] - if len(collection) > 1: - pivot = collection[0] - for x in collection: - if x < pivot: - less.append(x) - if x == pivot: - equal.append(x) - if x > pivot: - greater.append(x) - return quick_sort(less) + equal + quick_sort(greater) - else: - return collection + pivot = collection[0] + for x in collection: + if x < pivot: + less.append(x) + elif x == pivot: + equal.append(x) + else: + greater.append(x) + return quick_sort(less) + equal + quick_sort(greater) if __name__ == '__main__':