From 6dc692fc50e870225b0bc41a113acf041287b9a1 Mon Sep 17 00:00:00 2001 From: Rahulcool11 Date: Wed, 4 Oct 2023 01:11:59 +0530 Subject: [PATCH] Added quickSort.py --- quickSort.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 quickSort.py diff --git a/quickSort.py b/quickSort.py new file mode 100644 index 0000000..e98977b --- /dev/null +++ b/quickSort.py @@ -0,0 +1,29 @@ +def swap(arr, a, b): + arr[a], arr[b] = arr[b], arr[a] + +def partition(arr, low, high): + pivot = arr[high] + i = low - 1 + + for j in range(low, high): + if arr[j] < pivot: + i += 1 + swap(arr, i, j) + + swap(arr, i + 1, high) + return i + 1 + +def quickSort(arr, low, high): + if low < high: + pi = partition(arr, low, high) + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 1, high) + +arr = [34, 23, 54, 19, 1, 5] +N = len(arr) + +quickSort(arr, 0, N - 1) + +print("Sorted array:") +for i in range(N): + print(arr[i], end=" ")