This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bubbleSort(arr): | |
n = len(arr) | |
# Traverse through all array elements | |
for i in range(n): | |
# Last i elements are already in place | |
for j in range(0, n-i-1): | |
# traverse the array from 0 to n-i-1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This function takes last element as pivot, places | |
# the pivot element at its correct position in sorted | |
# array, and places all smaller (smaller than pivot) | |
# to left of pivot and all greater elements to right | |
# of pivot | |
def partition(arr,low,high): | |
i = ( low-1 ) # index of smaller element | |
pivot = arr[high] # pivot | |
for j in range(low , high): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# To heapify subtree rooted at index i. | |
# n is size of heap | |
def heapify(arr, n, i): | |
largest = i # Initialize largest as root | |
l = 2 * i + 1 # left = 2*i + 1 | |
r = 2 * i + 2 # right = 2*i + 2 | |
# See if left child of root exists and is | |
# greater than root | |
if l < n and arr[i] < arr[l]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def merge_sort(arr): | |
if len(arr) >1: | |
mid = len(arr)//2 #Finding the mid of the array | |
L = arr[:mid] # Dividing the array elements | |
R = arr[mid:] # into 2 halves | |
mergeSort(L) # Sorting the first half | |
mergeSort(R) # Sorting the second half | |
i = j = k = 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def selection_sort(arr: []): | |
# Traverse through all array elements | |
for i in range(len(arr)): | |
# Find the minimum element in remaining | |
# unsorted array | |
min_idx = i | |
for j in range(i+1, len(arr)): | |
if arr[min_idx] > arr[j]: | |
min_idx = j |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def insertion_sort(arr: []): | |
# Traverse through 1 to len(arr) | |
for i in range(1, len(arr)): | |
key = arr[i] | |
# Move elements of arr[0..i-1], that are | |
# greater than key, to one position ahead | |
# of their current position |