xxxxxxxxxx
def quick_sort(array):
sort(array, 0, len(array) - 1)
def sort(array, left, right):
if left >= right:
return array
pivot = array[(left + right) // 2]
index = partition(array, left, right, pivot)
sort(array, left, index - 1)
sort(array, index, right)
def partition(array, left, right, pivot):
while left <= right:
while array[left] < pivot:
left += 1
while array[right] > pivot:
right -= 1
if left <= right:
array[left], array[right] = array[right], array[left]
left += 1
right -= 1
return left
arr = [7, 2, 4, 0, 1, 5, 8, 3, 2, 0]
quick_sort(arr)
print(arr) # [0, 0, 1, 2, 2, 3, 4, 5, 7, 8]
xxxxxxxxxx
def quick_sort(array):
if len(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]
xxxxxxxxxx
[12, 19, 21, 27, 28, 29, 31, 41, 44, 44, 58, 66, 76, 78, 83, 87, 88, 97, 99]
xxxxxxxxxx
array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44]
quick_sort(array, 0, len(array) - 1)
print(array)
xxxxxxxxxx
#Code With Redoy: https://www.facebook.com/codewithredoy/
#My python lectures: https://cutt.ly/python-full-playlist
def quick_sort(array):
if len(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)
xxxxxxxxxx
def quicksort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
less = [x for x in arr[1:] if x[0] <= pivot[0]]
greater = [x for x in arr[1:] if x[0] > pivot[0]]
return quicksort(less) + [pivot] + quicksort(greater)
# Exemple d'utilisation pour trier la liste de vols par numéro de vol
vol = quicksort(vol)
xxxxxxxxxx
def partition(array, begin, end):
pivot = begin
for i in range(begin+1, end+1):
if array[i] <= array[begin]:
pivot += 1
array[i], array[pivot] = array[pivot], array[i]
array[pivot], array[begin] = array[begin], array[pivot]
return pivot
def quicksort(array, begin=0, end=None):
if end is None:
end = len(array) - 1
def _quicksort(array, begin, end):
if begin >= end:
return
pivot = partition(array, begin, end)
_quicksort(array, begin, pivot-1)
_quicksort(array, pivot+1, end)
return _quicksort(array, begin, end)
xxxxxxxxxx
def quick_sort(arr):
if len(arr) <= 1:
return arr # Base case: a list with 0 or 1 elements is already sorted
pivot = arr[len(arr) // 2] # Choose a pivot element (in this case, the middle element)
left = [x for x in arr if x < pivot] # Elements less than the pivot
middle = [x for x in arr if x == pivot] # Elements equal to the pivot
right = [x for x in arr if x > pivot] # Elements greater than the pivot
return quick_sort(left) + middle + quick_sort(right) # Recursively sort the left and right sub-arrays
my_list = [64, 34, 25, 12, 22, 11, 90]
sorted_list = quick_sort(my_list)
print("Sorted list:", sorted_list)
xxxxxxxxxx
/* 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