From edb369feccf67c61c8f52e402895837ff6bee15b Mon Sep 17 00:00:00 2001 From: Samriddha Pathak Date: Sun, 20 Oct 2024 09:20:33 +0545 Subject: [PATCH] Create Quick sort algorithm MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This repository contains a Python implementation of the Quick Sort algorithm. Quick Sort is an efficient, comparison-based sorting algorithm that follows the divide-and-conquer approach. It picks a pivot element, partitions the array into elements less than the pivot, equal to the pivot, and greater than the pivot, then recursively sorts the sub-arrays. Features: Pivot Selection: The algorithm uses the middle element as the pivot to partition the array. Recursive Sorting: The array is recursively divided into smaller sub-arrays, which are sorted independently. Time Complexity: Average-case time complexity is O(n log n), and worst-case time complexity is O(n²), but with randomized pivot selection, the average-case performance is optimal. --- Quick sort algorithm | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Quick sort algorithm diff --git a/Quick sort algorithm b/Quick sort algorithm new file mode 100644 index 000000000000..7a0a2fbc73ca --- /dev/null +++ b/Quick sort algorithm @@ -0,0 +1,15 @@ +#Quick Sort algortitm +def quick_sort(arr): + if len(arr) <= 1: + return arr + else: + 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) + +# Example usage: +arr = [10, 7, 8, 9, 1, 5] +sorted_arr = quick_sort(arr) +print("Sorted array:", sorted_arr)