defquick_sort(array):iflen(array)<2:return array
else:
pivot = array[0]
less =[i for i in array[1:]if i <= pivot]
greater =[i for i in array[1:]if i > pivot]return quick_sort(less)+[pivot]+ quick_sort(greater)
arr =[7,2,4,0,1,5,8,3,2,0]print(quick_sort(arr))# [0, 0, 1, 2, 2, 3, 4, 5, 7, 8]
#Code With Redoy: https://www.facebook.com/codewithredoy/#My python lectures: https://cutt.ly/python-full-playlistdefquick_sort(array):iflen(array)<2:return array
else:#select pivot element
pivot = array[0]#partitioning the main array into less and greater
less =[i for i in array[1:]if i <= pivot]
greater =[i for i in array[1:]if i > pivot]#calling recursively return quick_sort(less)+[pivot]+ quick_sort(greater)
array =[9,8,7,6,5,4,3,2,1]
res = quick_sort(array)print(res)
/* low --> Starting index, high --> Ending index */
quickSort(arr[], low, high){if(low < high){/* pi is partitioning index, arr[p]is now
at right place */
pi = partition(arr, low, high);
quickSort(arr, low, pi -1);// Before pi
quickSort(arr, pi +1, high);// After pi
}}
Python3
"""Basic Quicksort
"""from random import randint
defqs_basic(nums, low=0, high=None):if high isNone:
high =len(nums)-1# Sortingif low < high:
pivot = nums[randint(low, high)]# i finds greater than pivot, j finds smaller than pivot
i, j = low, high
# Sort the pivotwhile i <= j:while nums[i]< pivot:
i +=1while nums[j]> pivot:
j -=1# Pivot still not sortedif i <= j:
nums[i], nums[j]= nums[j], nums[i]
i +=1
j -=1# Recursive Partition
qs_basic(nums, low, j)
qs_basic(nums, i, high)return nums # for easy testing