Delve into a community where programmers unite to discover code snippets, exchange skills, and enhance their programming proficiency. With abundant resources and a supportive community, you'll find everything essential for your growth and success.
defquick_sort(arr):iflen(arr)<=1:return arr
else: pivot = arr[0] left =[x for x in arr[1:]if x <= pivot] right =[x for x in arr[1:]if x > pivot]return quick_sort(left)+[pivot]+ quick_sort(right)# Example usage:input_array =[3,1,4,1,5,9,2,6,5,3,5]sorted_array = quick_sort(input_array)print("Original Array:", input_array)print("Sorted Array:", sorted_array)
In this code, the quick_sort function is a recursive implementation of the Quick Sort algorithm. The pivot element is chosen as the first element of the array, and elements are partitioned into two sub-arrays (left and right) based on their relationship to the pivot. The function is then called recursively on the left and right sub-arrays until the entire array is sorted.