diff --git a/algorithms/sort/heap_sort.py b/algorithms/sort/heap_sort.py index 6c91cee90..ec4c7fa2b 100644 --- a/algorithms/sort/heap_sort.py +++ b/algorithms/sort/heap_sort.py @@ -1,17 +1,21 @@ -def max_heap_sort(arr): +def max_heap_sort(arr, simulation=False): """ Heap Sort that uses a max heap to sort an array in ascending order Complexity: O(n log(n)) """ + iteration = 0 + if simulation: + print("iteration",iteration,":",*arr) + for i in range(len(arr) - 1, 0, -1): - max_heapify(arr, i) + iteration = max_heapify(arr, i, simulation, iteration) - temp = arr[0] - arr[0] = arr[i] - arr[i] = temp + if simulation: + iteration = iteration + 1 + print("iteration",iteration,":",*arr) return arr -def max_heapify(arr, end): +def max_heapify(arr, end, simulation, iteration): """ Max heapify helper for max_heap_sort """ last_parent = (end - 1) // 2 @@ -31,21 +35,30 @@ def max_heapify(arr, end): if arr[child] > arr[current_parent]: arr[current_parent], arr[child] = arr[child], arr[current_parent] current_parent = child + if simulation: + iteration = iteration + 1 + print("iteration",iteration,":",*arr) # If no swap occured, no need to keep iterating else: break + arr[0], arr[end] = arr[end], arr[0] + return iteration - -def min_heap_sort(arr): +def min_heap_sort(arr, simulation=False): """ Heap Sort that uses a min heap to sort an array in ascending order Complexity: O(n log(n)) """ + iteration = 0 + if simulation: + print("iteration",iteration,":",*arr) + for i in range(0, len(arr) - 1): - min_heapify(arr, i) + iteration = min_heapify(arr, i, simulation, iteration) + return arr -def min_heapify(arr, start): +def min_heapify(arr, start, simulation, iteration): """ Min heapify helper for min_heap_sort """ # Offset last_parent by the start (last_parent calculated as if start index was 0) @@ -64,12 +77,16 @@ def min_heapify(arr, start): if child + 1 <= end - start and arr[child + start] > arr[ child + 1 + start]: child = child + 1 - + # Swap if child is less than parent if arr[child + start] < arr[current_parent + start]: arr[current_parent + start], arr[child + start] = \ arr[child + start], arr[current_parent + start] current_parent = child + if simulation: + iteration = iteration + 1 + print("iteration",iteration,":",*arr) # If no swap occured, no need to keep iterating else: break + return iteration diff --git a/algorithms/sort/quick_sort.py b/algorithms/sort/quick_sort.py index a262bb4c1..10794e7a5 100644 --- a/algorithms/sort/quick_sort.py +++ b/algorithms/sort/quick_sort.py @@ -1,18 +1,26 @@ -def quick_sort(arr): +def quick_sort(arr, simulation=False): """ Quick sort Complexity: best O(n log(n)) avg O(n log(n)), worst O(N^2) """ - return quick_sort_recur(arr, 0, len(arr) - 1) - + + iteration = 0 + if simulation: + print("iteration",iteration,":",*arr) + arr, _ = quick_sort_recur(arr, 0, len(arr) - 1, iteration, simulation) + return arr -def quick_sort_recur(arr, first, last): +def quick_sort_recur(arr, first, last, iteration, simulation): if first < last: pos = partition(arr, first, last) # Start our two recursive calls - quick_sort_recur(arr, first, pos - 1) - quick_sort_recur(arr, pos + 1, last) - return arr + if simulation: + iteration = iteration + 1 + print("iteration",iteration,":",*arr) + + _, iteration = quick_sort_recur(arr, first, pos - 1, iteration, simulation) + _, iteration = quick_sort_recur(arr, pos + 1, last, iteration, simulation) + return arr, iteration def partition(arr, first, last): wall = first