Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simulation code about quick_sort and heap_sort #342

Merged
merged 7 commits into from
Jun 10, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove the space around = in arguments.

def max_heap_sort(arr, simulation=False):

Same changes in all function definitions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it,
Thank you for your comment! :)

""" 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