From 780beed22e389660d404eb2bb217bd3376748b88 Mon Sep 17 00:00:00 2001 From: Anshu Sharma <60863169+Anshusharma06@users.noreply.github.com> Date: Mon, 12 Oct 2020 08:10:13 +0530 Subject: [PATCH] Create quick_sort.py --- LeetCode/quick_sort.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 LeetCode/quick_sort.py diff --git a/LeetCode/quick_sort.py b/LeetCode/quick_sort.py new file mode 100644 index 0000000..1be4fc4 --- /dev/null +++ b/LeetCode/quick_sort.py @@ -0,0 +1,33 @@ +def QuickSort(arr): + + elements = len(arr) + + #Base case + if elements < 2: + return arr + + current_position = 0 #Position of the partitioning element + + for i in range(1, elements): #Partitioning loop + if arr[i] <= arr[0]: + current_position += 1 + temp = arr[i] + arr[i] = arr[current_position] + arr[current_position] = temp + + temp = arr[0] + arr[0] = arr[current_position] + arr[current_position] = temp #Brings pivot to it's appropriate position + + left = QuickSort(arr[0:current_position]) #Sorts the elements to the left of pivot + right = QuickSort(arr[current_position+1:elements]) #sorts the elements to the right of pivot + + arr = left + [arr[current_position]] + right #Merging everything together + + return arr + + + +array_to_be_sorted = [4,2,7,3,1,6] +print("Original Array: ",array_to_be_sorted) +print("Sorted Array: ",QuickSort(array_to_be_sorted))