Skip to content

Commit

Permalink
added simulation code in quick_sort and heap_sort (#342)
Browse files Browse the repository at this point in the history
* Update quick_sort.py

* Update heap_sort.py

* Update heap_sort.py

* Update quick_sort.py

* Update heap_sort.py

* Update quick_sort.py
  • Loading branch information
hsi1032 authored and goswami-rahul committed Jun 10, 2018
1 parent 7a21ac5 commit 89c1daa
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
39 changes: 28 additions & 11 deletions algorithms/sort/heap_sort.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
22 changes: 15 additions & 7 deletions algorithms/sort/quick_sort.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 89c1daa

Please sign in to comment.