""" file: heapSort.py version: python3 author: Sean Strout author: <<< YOUR NAME HERE >> purpose: Implementation of the heapsort algorithm, not in-place, (lst is unmodified and a new sorted one is returned) """ import arrayHeap # mkHeap (for adding/removing from heap) import testSorts # run (for individual test run) def heapSort(lst): """ heapSort(List(Orderable)) -> List(Ordered) performs a heapsort on 'lst' returning a new sorted list Postcondition: the argument lst is not modified """ return [] if __name__ == "__main__": testSorts.run('heapSort')
the first, middle and last values. """ return median([lst[0], lst[len(lst) // 2], lst[len(lst) - 1]]) def quickSort(lst): """ quickSort: List(lst) -> List(result) Where the return 'result' is a totally ordered 'lst'. It uses the median-of-3 to select the pivot e.g. quickSort([1,8,5,3,4]) == [1,3,4,5,8] """ if len(lst) == 1 or lst == []: return lst pivot = [medianOf3(lst)] lst2 = [] lst3 = [] for i in lst: if i < pivot[0]: lst2.append(i) elif i > pivot[0]: lst3.append(i) else: pivot.append(i) return quickSort(lst2) + pivot + quickSort(lst3) if __name__ == "__main__": testSorts.run('qsPivotMedian3')
else: pivot = lst[0] # here we select the first element as the pivot less, same, more = partition(pivot, lst) return quickSort(less) + same + quickSort(more) def partition(pivot, lst): """ partition: pivot (element in lst) * List(lst) -> tuple(List(less), List(same, List(more))). Where: List(Less) has values less than the pivot List(same) has pivot value/s, and List(more) has values greater than the pivot e.g. partition(5, [11,4,7,2,5,9,3]) == [4,2,3], [5], [11,7,9] """ less, same, more = list(), list(), list() for val in lst: if val < pivot: less.append(val) elif val > pivot: more.append(val) else: same.append(val) return less, same, more if __name__ == "__main__": testSorts.run('qsPivotFirst')
""" A non in-place, depth limited quickSort, using median-of-3 pivot. Once the limit drops to 0, it uses heapSort instead. """ if len(lst) == 1 or lst == []: return lst if limit <= 0: return heapSort.heapSort(lst) pivot = [median([lst[0], lst[len(lst) // 2], lst[len(lst) - 1]])] lst2 = [] lst3 = [] for i in lst: if i < pivot[0]: lst2.append(i) elif i > pivot[0]: lst3.append(i) else: pivot.append(i) return quipSortRec(lst2, limit - 1) + pivot + quipSortRec(lst3, limit - 1) def quipSort(lst): """ The main routine called to do the sort. It should call the recursive routine with the correct values in order to perform the sort """ quipSortRec(lst, log2(len(lst))) if __name__ == "__main__": testSorts.run('quipSort')
""" From a lst of unordered data, find and return the the median value from the first, middle and last values. """ return median([lst[0], lst[len(lst) // 2], lst[len(lst) - 1]]) def quickSort(lst): """ quickSort: List(lst) -> List(result) Where the return 'result' is a totally ordered 'lst'. It uses the median-of-3 to select the pivot e.g. quickSort([1,8,5,3,4]) == [1,3,4,5,8] """ if len(lst) == 1 or lst == []: return lst pivot = [medianOf3(lst)] lst2 = [] lst3 = [] for i in lst: if i < pivot[0]: lst2.append(i) elif i > pivot[0]: lst3.append(i) else: pivot.append(i) return quickSort(lst2) + pivot + quickSort(lst3) if __name__ == "__main__": testSorts.run('qsPivotMedian3')
Once the limit drops to 0, it uses heapSort instead. """ if lst == []: return lst if limit == 0: return heapSort.heapSort(lst) else: pivot = qsPivotMedian3.medianOf3( lst) # select the first element as the pivot less, same, more = qsPivotFirst.partition(pivot, lst) return quicheSortRec(less, limit - 1) + same + quicheSortRec( more, limit - 1) def quicheSort(lst): """ The main routine called to do the sort. It should call the recursive routine with the correct values in order to perform the sort """ if len(lst) == 0: return list() else: limit = int(math.log(len(lst), 2)) return quicheSortRec(lst, limit) if __name__ == "__main__": testSorts.run('quicheSort')
Once the limit drops to 0, it uses heapSort instead. """ if len(lst) == 1 or lst == []: return lst if limit <= 0: return heapSort.heapSort(lst) pivot = [median([lst[0], lst[len(lst) // 2], lst[len(lst) - 1]])] lst2 = [] lst3 = [] for i in lst: if i < pivot[0]: lst2.append(i) elif i > pivot[0]: lst3.append(i) else: pivot.append(i) return quipSortRec(lst2, limit - 1) + pivot + quipSortRec(lst3, limit - 1) def quipSort(lst): """ The main routine called to do the sort. It should call the recursive routine with the correct values in order to perform the sort """ quipSortRec(lst, log2(len(lst))) if __name__ == "__main__": testSorts.run('quipSort')
if len(lst) == 0: return list() else: pivot = lst[0] # here we select the first element as the pivot less, same, more = partition(pivot, lst) return quickSort(less) + same + quickSort(more) def partition( pivot, lst ): """ partition: pivot (element in lst) * List(lst) -> tuple(List(less), List(same, List(more))). Where: List(Less) has values less than the pivot List(same) has pivot value/s, and List(more) has values greater than the pivot e.g. partition(5, [11,4,7,2,5,9,3]) == [4,2,3], [5], [11,7,9] """ less, same, more = list(), list(), list() for val in lst: if val < pivot: less.append(val) elif val > pivot: more.append(val) else: same.append(val) return less, same, more if __name__ == "__main__": testSorts.run('qsPivotFirst')
Once the limit drops to 0, it uses heapSort instead. """ if lst == []: return lst if limit==0: return heapSort.heapSort(lst) else: pivot= qsPivotMedian3.medianOf3(lst) # select the first element as the pivot less, same, more = qsPivotFirst.partition(pivot, lst) return quicheSortRec(less,limit-1) + same + quicheSortRec(more,limit-1) def quicheSort(lst): """ The main routine called to do the sort. It should call the recursive routine with the correct values in order to perform the sort """ if len(lst)== 0: return list() else: limit = int (math.log(len(lst),2)) return quicheSortRec(lst,limit) if __name__ == "__main__": testSorts.run('quicheSort')