About
Python program for implementation of Bubble Sort
def bubbleSort(arr): n = len(arr)
# optimize code, so if the array is already sorted, it doesn't need
# to go through the entire process
# Traverse through all array elements
for i in range(n-1):
# range(n) also work but outer loop will
# repeat one time more than needed.
# Last i elements are already in place
swapped = False
for j in range(0, n-i-1):
# traverse the array from 0 to n-i-1
# Swap if the element found is greater
# than the next element
if arr[j] > arr[j + 1]:
swapped = True
arr[j], arr[j + 1] = arr[j + 1], arr[j]
if not swapped:
# if we haven't needed to make a single swap, we
# can just exit the main loop.
return
Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90] bubbleSort(arr) print("Sorted array is:") for i in range(len(arr)): print("% d" % arr[i], end=" ") or def bubblesort(arr): n = len(arr) for i in range(n-1): swapped = False for j in range(0, n-i-1): if arr[j]>arr[j+1]: swapped = True arr[j], arr[j+1] = arr[j+1], arr[j] if not swapped: return n = int(input("enter the size:")) arr = list(map(int,input("enter elements:").split())) bubblesort(arr) print("sorted array:",arr)
def selectionsort(arr, size): for i in range(size): min_index = i for j in range(i + 1, size): if arr[j] < arr[min_index]: min_index = j arr[i], arr[min_index] = arr[min_index], arr[i] size = int(input("Enter size of array: ")) arr = list(map(int, input("Enter the elements: ").split())) selectionsort(arr, size) print("Sorted array:", arr)
def insertionsort(arr): for i in range(1,len(arr)): key = arr[i] j = i -1 while j>=0 and key<arr[j]: arr[j+1] = arr[j] j = j-1 arr[j+1] = key data = list(map(int,input("enter elemets:").split())) insertionsort(data) print("sorted list:",data)
Python program for implementation of Quicksort Sort
This implementation utilizes pivot as the last element in the nums list
It has a pointer to keep track of the elements smaller than the pivot
At the very end of partition() function, the pointer is swapped with the pivot
to come up with a "sorted" nums relative to the pivot
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 quickSort(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
quickSort(array, low, pi - 1)
# Recursive call on the right of pivot
quickSort(array, pi + 1, high)
data = [1, 7, 4, 1, 10, 9, -2] print("Unsorted Array") print(data) size = len(data) quickSort(data, 0, size - 1) print('Sorted Array in Ascending Order:') print(data)
def merge_sort(arr): if len(arr) > 1: mid = len(arr)//2 L = arr[:mid] R = arr[mid:] merge_sort(L) merge_sort(R) i = j = k = 0 while i < len(L) and j < len(R): if L[i] < R[j]: arr[k] = L[i] i += 1 else: arr[k] = R[j] j += 1 k += 1 while i < len(L): arr[k] = L[i] i += 1 k += 1 while j < len(R): arr[k] = R[j] j += 1 k += 1
Example usage:
arr = [64, 34, 25, 12, 22, 11, 90] merge_sort(arr) print("Sorted array is:", arr)
def heapify(arr, n, i):
largest = i
l = 2 i + 1
r = 2 i + 2
if l < n and arr[l] > arr[largest]:
largest = l
if r < n and arr[r] > arr[largest]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def heap_sort(arr): n = len(arr)
for i in range(n // 2 - 1, -1, -1):
heapify(arr, n, i)
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
Example usage:
arr = [64, 34, 25, 12, 22, 11, 90] heap_sort(arr) print("Sorted array is:", arr)
//radix
def counting_sort(arr, exp): n = len(arr) output = [0] n count = [0] 10 for i in range(n): index = arr[i] // exp count[index % 10] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = n - 1
while i >= 0:
index = arr[i] // exp
output[count[index % 10] - 1] = arr[i]
count[index % 10] -= 1
i -= 1
i = 0
for i in range(len(arr)):
arr[i] = output[i]
def radix_sort(arr): max_val = max(arr) exp = 1 while max_val // exp > 0: counting_sort(arr, exp) exp *= 10
Example usage:
arr = [170, 45, 75, 90, 802, 24, 2, 66] radix_sort(arr) print("Sorted array is:", arr)
//bucket
def insertion_sort(arr): for i in range(1, len(arr)): key = arr[i] j = i - 1 while j >= 0 and key < arr[j]: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key
def bucket_sort(arr): n = len(arr) buckets = [[] for _ in range(n)] for num in arr: index = int(num * n) buckets[index].append(num)
for i in range(n):
insertion_sort(buckets[i])
k = 0
for i in range(n):
for j in range(len(buckets[i])):
arr[k] = buckets[i][j]
k += 1
Example usage:
arr = [0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434] bucket_sort(arr) print("Sorted array is:", arr)