tgoop.com/CodeProgrammer/2218
Last Update:
đź–Ą Unraveling the Magic of Sorting: A Python Guide for Novices
â–ŞBubble Sortdef bubble_sort(list):
for i in range(len(list)):
for j in range(len(list) - 1):
if list[j] > list[j + 1]:
list[j], list[j + 1] = list[j + 1], list[j] # swap
return list
â–ŞSelection Sortdef selection_sort(list):
for i in range(len(list)):
min_index = i
for j in range(i + 1, len(list)):
if list[min_index] > list[j]:
min_index = j
list[i], list[min_index] = list[min_index], list[i] # swap
return list
â–ŞInsertion Sortdef insertion_sort(list):
â–ŞQuick Sort
for i in range(1, len(list)):
key = list[i]
j = i - 1
while j >=0 and key < list[j] :
list[j+1] = list[j]
j -= 1
list[j+1] = key
return listdef partition(array, low, high):
i = (low-1)
pivot = array[high]
for j in range(low, high):
if array[j] <= pivot:
i = i+1
array[i], array[j] = array[j], array[i]
array[i+1], array[high] = array[high], array[i+1]
return (i+1)
def quick_sort(array, low, high):
if len(array) == 1:
return array
if low < high:
partition_index = partition(array, low, high)
quick_sort(array, low, partition_index-1)
quick_sort(array, partition_index+1, high)
https://www.tgoop.com/CodeProgrammer
BY Python | Machine Learning | Coding | R
Share with your friend now:
tgoop.com/CodeProgrammer/2218