From 4660900fcd17b086d439edd055f3e5194090e524 Mon Sep 17 00:00:00 2001 From: pramodbharti Date: Wed, 10 Oct 2018 23:57:55 +0530 Subject: [PATCH] Added quicksort implementation in python --- data structures/quick_sort.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 data structures/quick_sort.py diff --git a/data structures/quick_sort.py b/data structures/quick_sort.py new file mode 100644 index 000000000..16da17b3e --- /dev/null +++ b/data structures/quick_sort.py @@ -0,0 +1,14 @@ +""" +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) + +print (quick_sort([5,2,8,3,9,12,43])) # This will print [2,3,5,8,9,12,43]