#Python3 implementation of QuickSort #Function to find the partition position
def partition(array, low, high):
# Choose the rightmost element as pivot
pivot = array[high]
# Pointer for greater element
i = low -1
# Traverse through all elements
# compare each element with pivot
for j in range(low, high):if array[j]<= pivot:
# If element smaller than pivot is found
# swap it with the greater element pointed by i
i = i +1
# Swapping element at i with element at j(array[i], array[j])=(array[j], array[i])
# Swap the pivot element with the greater element specified by i(array[i +1], array[high])=(array[high], array[i +1])
# Return the position from where partition is done
return i +1#Function to perform quicksort
def quick_sort(array, low, high):if low < high:
# Find pivot element such that
# element smaller than pivot are on the left
# element greater than pivot are on the right
pi =partition(array, low, high)
# Recursive call on the left of pivot
quick_sort(array, low, pi -1)
# Recursive call on the right of pivot
quick_sort(array, pi +1, high)#Driver code
array =[10,7,8,9,1,5]quick_sort(array,0,len(array)-1)print(f'Sorted array: {array}')#This code is contributed by Adnan Aliakbar
staticvoidswap(int[] arr,int i,int j){int temp = arr[i];
arr[i]= arr[j];
arr[j]= temp;}staticintpartition(int[] arr,int low,int high){int pivot = arr[high];// Index of smaller element and// indicates the right position// of pivot found so farint i =(low -1);for(int j = low; j <= high -1; j++){// If current element is smaller // than the pivotif(arr[j]< pivot){// Increment index of // smaller element
i++;swap(arr, i, j);}}swap(arr, i +1, high);return(i +1);}staticvoidquickSort(int[] arr,int low,int high){if(low < high){// pi is partitioning index, arr[p]// is now at right place int pi =partition(arr, low, high);// Separately sort elements before// partition and after partitionquickSort(arr, low, pi -1);quickSort(arr, pi +1, high);}}
#Code With Redoy: https://www.facebook.com/codewithredoy/#My python lectures: https://cutt.ly/python-full-playlist
def quick_sort(array):iflen(array)<2:return array
else:#selectpivot element
pivot = array[0]#partitioningthe 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]#callingrecursively returnquick_sort(less)+[pivot]+quick_sort(greater)
array =[9,8,7,6,5,4,3,2,1]
res =quick_sort(array)print(res)
def quicksort(arr):iflen(arr)<=1:return arr
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]returnquicksort(left)+ middle +quicksort(right)print(quicksort([3,6,8,10,1,2,1]))#Prints "[1, 1, 2, 3, 6, 8, 10]"
def partition(start, end, array):#Initializing pivot's index to start
pivot_index = start
pivot = array[pivot_index]#This loop runs till start pointer crosses#endpointer,and when it does we swap the#pivotwith element on end pointerwhile start < end:#Increment the start pointer till it finds an#elementgreater than pivotwhile start <len(array)and array[start]<= pivot:
start +=1#Decrement the end pointer till it finds an#elementless than pivotwhile array[end]> pivot:
end -=1#If start and end have not crossed each other,#swapthe numbers on start and endif(start < end):
array[start], array[end]= array[end], array[start]#Swap pivot element with element on end pointer.#This puts pivot on its correct sorted place.
array[end], array[pivot_index]= array[pivot_index], array[end]#Returning end pointer to divide the array into 2return end
#The main function that implements QuickSort
def quick_sort(start, end, array):if(start < end):#pis partitioning index, array[p]#isat right place
p =partition(start, end, array)#Sort elements before partition#andafter partitionquick_sort(start, p -1, array)quick_sort(p +1, end, array)
//piv: Pivot element intpartition(int A[],int start ,int end){int i = start +1;int piv = A[start];//make the first element as pivot element.for(int j =start +1; j <= end ; j++){/*rearrange the array by putting elements which are less than pivot
on one side and which are greater that on other. */if( A[ j ]< piv){swap(A[ i ],A [ j ]);
i +=1;}}swap( A[ start ],A[ i-1]);//put the pivot element in its proper place.return i-1;//return the position of the pivot}// recursive function Quick_sort: voidquick_sort(int A[],int start ,int end ){if( start < end ){//stores the position of pivot elementint piv_pos =partition(A,start , end );quick_sort(A,start , piv_pos -1);//sorts the left side of pivot.quick_sort( A,piv_pos +1, end);//sorts the right side of pivot.}}
// Take Left (first) Index of the array and Right (last) Index of the arrayvoidquickSort(int Data[],int l ,int r){// If the first index less or equal than the last indexif(l <= r){// Create a Key/Pivot Elementint key = Data[(l+r)/2];// Create temp Variables to loop through arrayint i = l;int j = r;while(i <= j){while(Data[i]< key)
i++;while(Data[j]> key)
j--;if(i <= j){swap(Data[i], Data[j]);
i++;
j--;}}// Recursion to the smaller partition in the array after sorted aboveif(l < j)quickSort(Data, l, j);if(r > i)quickSort(Data, i, r);}}
"""3-way Quicksort
Simpler to implement than basic quicksort
"""
from random importrandint
def qs_3_way(nums):#Sortingiflen(nums)>1:
pivot = nums[randint(0,len(nums)-1)]
smaller, greater, equals =[],[],[]for num in nums:if num < pivot:
smaller.append(num)
elif num > pivot:
greater.append(num)else:
equals.append(num)#Recursive Partitionreturnqs_3_way(smaller)+ equals +qs_3_way(greater)return nums # for easy testing
"""Basic Quicksort
"""
from random importrandint
def qs_basic(nums, low=0, high=None):if high is None:
high =len(nums)-1#Sortingif low < high:
pivot = nums[randint(low, high)]#ifinds 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 Partitionqs_basic(nums, low, j)qs_basic(nums, i, high)return nums # for easy testing
function swap(arr, leftIndex, rightIndex){// swap 2 elements in the array
var temp = arr[leftIndex];
arr[leftIndex]= arr[rightIndex];
arr[rightIndex]= temp;}
function partition(arr, left, right){
var pivot = arr[Math.floor((right + left)/2)],//middle element
i = left,//left pointer
j = right;//right pointerwhile(i <= j){// while left pointer is smaller than right pointerwhile(arr[i]< pivot){// move the left pointer to the right
i++;}while(arr[j]> pivot){// move the right pointer to the left
j--;}if(i <= j){//left pointer smaller or equal to right pointerswap(arr, i, j);//sawpping two elements
i++;
j--;}}return i;}// run as quickSort(array, 0, array.length - 1);
function quickSort(arr, left, right){
var index;if(arr.length >1){
index =partition(arr, left, right);//index returned from partitionif(left < index -1){//more elements on the left side of the pivotquickSort(arr, left, index -1);}if(index < right){//more elements on the right side of the pivotquickSort(arr, index, right);}}return arr;}
array =[29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44]
q = lambda l:q([x for x in l[1:]if x <= l[0]])+[l[0]]+q([x for x in l if x > l[0]])if l else[]print(q(array))