Skip to content

Instantly share code, notes, and snippets.

import time
import matplotlib.pyplot as plt
def insertionSort(array):
steps = 0
start = time.time()
for a in range(1,len(array)):
steps+=1
key = array[a]
b = a-1
import time
import matplotlib.pyplot as plt
import random
def merge(a,b,steps):
# creating array
c = []
# creating pointers
j=0
i=0
import math
def max_subarray(x):
# starting array to make any subarray bigger than its value
maxSubarray = [-(math.inf)]
# looping through all the array to find the maxsubarray
# bruteforce approach
for j in range(len(x)):
for i in range(j+1, len(x)+1):
def maxHeapify(A,i):
# it takes the node with index i and puts it in the right position
# in the heap to make it a maxHeap
l = Left(i)
r = Right(i)
# since we already know that element in index i will be: smaller
# than the right/left child or higher than both
# we are storing the index of the higher child in 'largest'
# which will be the substitution that we need to make, in case that we need to do any substitution
if l < (len(A)) and A[l] > A[i]:
@luccabb
luccabb / 3WayMergeSort.py
Created April 5, 2020 19:00
3 Way Merge Sort
import time
import matplotlib.pyplot as plt
import random
def merge3(a,b,c):
# Starting array that will be returned
final = []
# Starting pointers
i=0 # pointer for array 'a'
j=0 # pointer for array 'b'
@luccabb
luccabb / InsertionMergeSort.py
Last active April 5, 2020 19:07
Comparison between merge sort, insertion sort and 3 way merge sort. Augmented sort is the combination between insertion and merge sort to create the best time complexity
import time
import matplotlib.pyplot as plt
import random
def merge3(a,b,c):
# Starting array that will be returned
final = []
# Starting pointers
i=0 # pointer for array 'a'
j=0 # pointer for array 'b'
@luccabb
luccabb / kWayMergeSort.py
Last active April 5, 2020 19:14
Doing a merge sort breakable in 'k' possible ways to find what is the best 'k' to reduce time complexity
def Right(i):
# right child index
return 2*i + 2
def Left(i):
# left child index
return 2*i +1
def Parent(i):
# parent index
import sys
def Right(i):
# right child index
return 2*i + 2
def Left(i):
# left child index
return 2*i +1
import random
import matplotlib.pyplot as plt
def hatCheck(n):
# using index as the id for people
# creating list for hats from 0 to 'n' matching
# the index and then shuffling it
hat = [a for a in range(0,n)]
random.shuffle(hat)
correct =0
import random
import matplotlib.pyplot as plt
import math
def askingNewAssistant():
# getting new assistant ability
return random.random()
def hireAssistant(n):
# hire assistant for n applications