def binarySearch(array, start, end, value): arr = quickSort.sort(array) mid = (start + end) // 2 if (start > end): return elif (arr[mid] < value): return binarySearch(arr, mid + 1, end, value) elif (arr[mid] > value): return binarySearch(arr, start, mid, value) elif (arr[mid] == value): print("value found at index:", mid) return
def bucketSort(array, maxValue): noOfBuckets = int(math.floor(math.sqrt(len(array)))) print(noOfBuckets) buckets = list() for i in range(noOfBuckets): buckets.append(list()) for i in array: appropriateBucket = int(math.ceil((i * noOfBuckets) / maxValue)) #print(i,appropriateBucket,end="\t") buckets[appropriateBucket - 1].append(i) print() #print(buckets) # sorting the individual buckets independently using quick sort O(n)=nlog n for i in buckets: quickSort.sort(i) #print(buckets) sortedList = [] for i in buckets: for j in i: sortedList.append(j) print(sortedList)
def overlappingIntervals(N): intervals = sort(N) print(intervals) stack = [] i = 0 for a, b in intervals: print(stack) print(a, b) if not stack: stack.append((a, b)) i = i + 1 c, d = stack[i - 1] # Note that these two checks happen concurrently # if you put them as separate checks they will have unintended effects # because if d is less than a it is implicit that d is also less than b if d < a: stack.append((a, b)) i = i + 1 elif d <= b: stack[i - 1] = (c, b) return stack
def overlappingIntervals(N): intervals = sort(N) print(intervals) stack = [] i = 0 for a, b in intervals: print(stack) print(a, b) if(stack == []): stack.append((a,b)) i = i + 1 c, d = stack[i-1] # Note that these two checks happen concurrently # if you put them as separate checks they will have unintended effects # because if d is less than a it is implicit that d is also less than b if(d < a): stack.append((a,b)) i = i + 1 elif(d <= b): stack[i-1] = (c,b) return(stack)
def quick(array): return quickSort.sort(array)
for i in range(len(list)): a,b,c = list[i] if current > a: count += 1 else: current = b return count def intervalLength(a,b): return b-a #intervals = [[1,2], [2,5], [2,3], [3,5], [5,7], [2,7]] #intervals = [ [1,2], [2,3], [3,4], [1,3] ] #Should be 1 intervals = [ [1,2], [1,2], [1,2] ] #Should be 2 tuples = [] for i in range(len(intervals)): start, end, length = intervals[i][0], intervals[i][1], intervalLength(intervals[i][0],intervals[i][1]) tuples.append((start, end, length)) print(tuples) tuples = quickSort.sort(tuples) print(tuples) print(findOverlapping(tuples)) """Questions to ask: Are the intervals given in any order Is the range -inf to inf Runtime? Space? If no requirements try for O(NlogN) """ #The key is only doing what the question asks you to do. Don't over think it. #Here I thought about it in terms of which to delete and created a function #that was unnessecary to solve the problem. You only need start and end. #Always remember the trival case. Always think about edge cases.
import bubbleSort import insertionSort import quickSort #Unsorted list clutter = [34, 19, 22, 51, 17, 5] print " " + "-".join(str(e) for e in clutter) #Enable only one algorithm per run! #print "bubble: " + "-".join(str(e) for e in bubbleSort.sort(clutter)) #print "insertion: " + "-".join(str(e) for e in insertionSort.sort(clutter)) print "quickSort: " + "-".join( str(e) for e in quickSort.sort(clutter, 0, len(clutter) - 1))
import time import quickSort import heapSort import margeSort import inputs import sys sys.setrecursionlimit(50000) print("50k elements random array") a = inputs.sok.copy() start = time.time() quickSort.sort(a) end = time.time() print("Quick sort: ", (end - start) * 1000, "ms") a = inputs.sok.copy() start = time.time() heapSort.sort(a) end = time.time() print("Heap Sort: ", (end - start) * 1000, "ms") a = inputs.sok.copy() start = time.time() margeSort.sort(a) end = time.time() print("Marege Sort: ", (end - start) * 1000, "ms") print("---------------------------") print("50k elements sorted array") a = inputs.sokSort.copy() start = time.time()
current = b return count def intervalLength(a: int, b: int) -> int: return b - a intervals = [[1, 2], [2, 5], [2, 3], [3, 5], [5, 7], [2, 7]] # intervals = [[1, 2], [2, 3], [3, 4], [1, 3]] # Should be 1 # intervals = [[1, 2], [1, 2], [1, 2]] # Should be 2 tuples = [] for i in range(len(intervals)): start, end, length = intervals[i][0], intervals[i][1], intervalLength( intervals[i][0], intervals[i][1]) tuples.append((start, end, length)) print(tuples) tuples = quickSort.sort(tuples) print(tuples) print(findOverlapping(tuples)) """Questions to ask: Are the intervals given in any order Is the range -inf to inf Runtime? Space? If no requirements try for O(NlogN) """ # The key is only doing what the question asks you to do. Don't over think it. # Here I thought about it in terms of which to delete and created a function # that was unnecessary to solve the problem. You only need start and end. # Always remember the trivial case. Always think about edge cases.
for i in range(k, len(N), 1): if heap[0] < negative[i]: heapq.heapreplace(heap, negative[i]) maxHeap = negate_list(heap) maxHeap.reverse() return maxHeap #Honestly the max heap thing in python is really really confusing #I should've probably just built my own code, but this is a really #simple hack. N_points = [(-2,-4),(0,-2),(-1,0),(3,-5),(-2,-2),(3,2)] k = 3 print("The n points are: ",N_points) N_dist = calc_distance(N_points) #print("(Distance, index): ",N_dist) hash_map = dict(N_dist) #dict is (key,value); D[key] returns value #print("Our hash map: ",hash_map) unzipped = list(zip(*N_dist)) #list1_2=zip(*list3) is the reverse of list3=zip(list1,list2) #print("(Distance),(indicies)",unzipped) shortest_dist = sort(unzipped[0]) #This is solution 1 quicksort() shortest_dist2 = maxheap_kSort(unzipped[0],k) #print("Just distance: ",shortest_dist) #print("Just distance: ",shortest_dist2) print(k,"closest points (using quicksort):") for i in range(k): print(i+1,": ",N_points[hash_map[shortest_dist[i]]]) print(k,"closest points (using a maxheap):") for i in range(k): print(i+1,": ",N_points[hash_map[shortest_dist2[i]]])
for i in range(k,len(N),1): if heap[0] < negative[i]: heapq.heapreplace(heap, negative[i]) maxHeap = negate_list(heap) maxHeap.reverse() return maxHeap #Honestly the max heap thing in python is really really confusing #I should've probably just built my own code, but this is a really #simple hack. N_points = [(-2,-4),(0,-2),(-1,0),(3,-5),(-2,-2),(3,2)] k = 3 print("The n points are: ",N_points) N_dist = calc_distance(N_points) #print("(Distance, index): ",N_dist) hash_map = dict(N_dist) #dict is (key,value); D[key] returns value #print("Our hash map: ",hash_map) unzipped = list(zip(*N_dist)) #list1_2=zip(*list3) is the reverse of list3=zip(list1,list2) #print("(Distance),(indicies)",unzipped) shortest_dist = sort(unzipped[0]) #This is solution 1 quicksort() shortest_dist2 = maxheap_kSort(unzipped[0],k) #print("Just distance: ",shortest_dist) #print("Just distance: ",shortest_dist2) print(k,"closest points (using quicksort):") for i in range(k): print(i+1,": ",N_points[hash_map[shortest_dist[i]]]) print(k,"closest points (using a maxheap):") for i in range(k): print(i+1,": ",N_points[hash_map[shortest_dist2[i]]])