def partition(l, p, r, prnt=True): """ Helper function for quicksort. Args: prnt: turn on printing Returns: the new partition index. """ global swaps x = l[r] if prnt: print("Checking print flag; = " + str(prnt)) print("Partition: Our pivot element x = " + str(x)) i = p - 1 for j in range(p, r): if l[j] <= x: i += 1 if prnt: print("i = " + str(i) + " and j = " + str(j)) if i != j: if prnt: print("Swapping elements " + str(l[i]) + " and " + str(l[j])) swap(l, i, j) swaps += 1 if prnt: print("Swaps = " + str(swaps)) if (i + 1) != r: if prnt: print("Swapping elements " + str(l[i + 1]) + " and " + str(l[r])) swap(l, i + 1, r) swaps += 1 if prnt: print("Swaps = " + str(swaps)) return i + 1
def hoare_partition(l, p, r): """ Alternate helper function for quicksort. """ global swaps x = l[p] i = p - 1 j = r + 1 print("Hoare: Our pivot element x = " + str(x)) while i < j: while True: j -= 1 if l[j] <= x: break while True: i += 1 if l[i] >= x: break if i < j: print("Swapping elements " + "i (" + str(i) + ") = " + str(l[i]) + " and " + "j (" + str(j) + ") = " + str(l[j])) swap(l, i, j) swaps += 1 print("Swaps = " + str(swaps)) return j
def hoare_partition(l, p, r): """ Alternate helper function for quicksort. """ global swaps x = l[p] i = p - 1 j = r + 1 print("Hoare: Our pivot element x = " + str(x)) while i < j: while True: j -= 1 if l[j] <= x: break while True: i += 1 if l[i] >= x: break if i < j: print("Swapping elements " + "i (" + str(i) + ") = " + str(l[i]) + " and " + "j (" + str(j) + ") = " + str(l[j])) swap(l, i, j) swaps += 1 print("Swaps = " + str(swaps)) return j
def heap_change_key(h, i, key, min_or_max=MAX): """ Args: h: the heap. i: the initial index for the key we are changing. key: the new value for i. min_or_max: are we dealing with a min or max heap? Returns: None (re-arranges the existing list). """ # we have to reverse the ordinary comparison here, # since we are working bottom up! comp = get_opt(not min_or_max) if comp(key, h[i]): xer = ("smaller" if min_or_max == MAX else "larger") print("New key is " + xer + " than current key.") return None h[i] = key while i > 0 and comp(h[parent(i)], h[i]): print("Swapping " + str(h[i]) + " and " + str(h[parent(i)])) swap(h, i, parent(i)) i = parent(i)
def partition(l, p, r, prnt=True): """ Helper function for quicksort. Args: prnt: turn on printing Returns: the new partition index. """ global swaps x = l[r] if prnt: print("Partition: Our pivot element x = " + str(x)) i = p - 1 for j in range(p, r): if l[j] <= x: i += 1 if i != j: if prnt: print("Swapping elements " + str(l[i]) + " and " + str(l[j])) swap(l, i, j) swaps += 1 if (i + 1) != r: if prnt: print("Swapping elements " + str(l[i + 1]) + " and " + str(l[r])) swap(l, i + 1, r) swaps += 1 return i + 1
def heapify(h, i, heapsize=None, min_or_max=MAX): """ In the text book, there is a max-heapify function. and a separate function would be needed for min-heapify. But we can easily combine them into one function, and just pass in the operator we need to differentiate a max heap from a min heap. Args: h: the list containing the heap. i: the node that might violate the heap property. heapsize: the size of the heap min_or_max: are we dealing with a max heap or min heap? Returns: None """ if heapsize is None: heapsize = len(h) comp = get_opt(min_or_max) l = left(i) r = right(i) if l < heapsize and comp(h[l], h[i]): largest = l else: largest = i if r < heapsize and comp(h[r], h[largest]): largest = r if largest != i: swap(h, i, largest) heapify(h, largest, heapsize, min_or_max)
def heap_change_key(h, i, key, min_or_max=MAX): """ Args: h: the heap. i: the initial index for the key we are changing. key: the new value for i. min_or_max: are we dealing with a min or max heap? Returns: None (re-arranges the existing list). """ # we have to reverse the ordinary comparison here, # since we are working bottom up! comp = get_opt(not min_or_max) if comp(key, h[i]): xer = ("smaller" if min_or_max == MAX else "larger") print("New key is " + xer + " than current key.") return None h[i] = key while i > 0 and comp(h[parent(i)], h[i]): print("Swapping " + str(h[i]) + " and " + str(h[parent(i)])) swap(h, i, parent(i)) i = parent(i)
def heapify(h, i, heapsize=None, min_or_max=MAX): """ In the text book, there is a max-heapify function. and a separate function would be needed for min-heapify. But we can easily combine them into one function, and just pass in the operator we need to differentiate a max heap from a min heap. Args: h: the list containing the heap. i: the node that might violate the heap property. heapsize: the size of the heap min_or_max: are we dealing with a max heap or min heap? Returns: None """ if heapsize is None: heapsize = len(h) comp = get_opt(min_or_max) l = left(i) r = right(i) if l < heapsize and comp(h[l], h[i]): largest = l else: largest = i if r < heapsize and comp(h[r], h[largest]): largest = r if largest != i: swap(h, i, largest) heapify(h, largest, heapsize, min_or_max)
def partition(l, p, r): """ Helper function for quicksort. Returns: the new partition index. """ global swaps x = l[r] print("Partition: Our pivot element x = " + str(x)) i = p - 1 for j in range(p, r): if l[j] <= x: i += 1 print("i = " + str(i) + " and j = " + str(j)) if i != j: print("Swapping elements " + str(l[i]) + " and " + str(l[j])) swap(l, i, j) swaps += 1 print("Swaps = " + str(swaps)) if (i + 1) != r: print("Swapping elements " + str(l[i + 1]) + " and " + str(l[r])) swap(l, i + 1, r) swaps += 1 print("Swaps = " + str(swaps)) return i + 1
def rand_partition(l, p, r): """ This function simply chooses a random index value between p and r, swaps that value with the one at r, and then calls partition on the new List. """ i = random.randint(p, r) print("We have randomly chosen between values " + str(p) + " and " + str(r) + ": " + str(i) + " as our pivot.") swap(l, i, r) return partition(l, p, r)
def rand_partition(l, p, r): """ This function simply chooses a random index value between p and r, swaps that value with the one at r, and then calls partition on the new List. """ i = random.randint(p, r) print("We have randomly chosen between values " + str(p) + " and " + str(r) + ": " + str(i) + " as our pivot.") swap(l, i, r) return partition(l, p, r)
def bubble_sort(l): """ Args: l: the list to sort Returns: a sorted list. Performance: Θ(n**2) """ for i in range(0, len(l) - 1): for j in range(len(l) - 1, i, -1): if l[j] < l[j - 1]: print("Swapping " + str(l[j]) + " and " + str(l[j - 1])) swap(l, j, j - 1) return l
def heapsort(h, min_or_max=MAX): """ Args: h: the list to heap sort min_or_max: are we sorting a max heap or a min heap? Returns: None Performance: O(n lg n) """ build_heap(h, min_or_max) heapsize = len(h) for i in range(len(h) - 1, 0, -1): print("heapsort heap: " + str(h)) swap(h, 0, i) heapsize -= 1 heapify(h, 0, heapsize, min_or_max)
def heapsort(h, min_or_max=MAX): """ Args: h: the list to heap sort min_or_max: are we sorting a max heap or a min heap? Returns: None Performance: O(n lg n) """ build_heap(h, min_or_max) print("After build_heap, h = " + str(h)) heapsize = len(h) for i in range(len(h) - 1, -1, -1): print("Looping in heapsort with i = " + str(i) + "; h = " + str(h)) swap(h, 0, i) heapsize -= 1 heapify(h, 0, heapsize, min_or_max)
def heapify(h, i, heapsize=None, min_or_max=MAX): """ In the text book, there is a max-heapify function. and a separate function would be needed for min-heapify. But we can easily combine them into one function, and just pass in the operator we need to differentiate a max heap from a min heap. Args: h: the list containing the heap. i: the node that might violate the heap property. heapsize: the size of the heap min_or_max: are we dealing with a max heap or min heap? Returns: None """ if heapsize is None: heapsize = len(h) comp = get_opt(min_or_max) l = left(i) r = right(i) largest = i print("\n*********\nheap in progress = " + str(h[0:heapsize])) print("heapifying with i = " + str(i) + " and " + "left = " + str(l) + "; right = " + str(r) + "; heapsize = " + str(heapsize)) if l < heapsize and comp(h[l], h[i]): print("Largest was: " + str(h[largest]) + " setting largest to: " + str(h[l])) largest = l if r < heapsize and comp(h[r], h[largest]): print("Largest was: " + str(h[largest]) + " setting largest to: " + str(h[r])) largest = r if largest != i: print("Swapping elements " + str(i) + " and " + str(largest)) swap(h, i, largest) heapify(h, largest, heapsize, min_or_max)
def heapsort(h, min_or_max=MAX): """ Args: h: the list to heap sort min_or_max: are we sorting a max heap or a min heap? Returns: None Performance: O(n lg n) """ build_heap(h, min_or_max) print("After build_heap, h = " + str(h)) heapsize = len(h) for i in range(len(h) - 1, -1, -1): print("Looping in heapsort with i = " + str(i) + "; h = " + str(h)) swap(h, 0, i) heapsize -= 1 heapify(h, 0, heapsize, min_or_max)