Skip to content

Commit edb369f

Browse files
Create Quick sort algorithm
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.
1 parent 03a4251 commit edb369f

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Quick sort algorithm

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#Quick Sort algortitm
2+
def quick_sort(arr):
3+
if len(arr) <= 1:
4+
return arr
5+
else:
6+
pivot = arr[len(arr) // 2]
7+
left = [x for x in arr if x < pivot]
8+
middle = [x for x in arr if x == pivot]
9+
right = [x for x in arr if x > pivot]
10+
return quick_sort(left) + middle + quick_sort(right)
11+
12+
# Example usage:
13+
arr = [10, 7, 8, 9, 1, 5]
14+
sorted_arr = quick_sort(arr)
15+
print("Sorted array:", sorted_arr)

0 commit comments

Comments
 (0)