array1
:14 20 34 56 77
array2
:15 30 51 52 93
array1
is 14
(located in index 0 of array1
)array2
is 15 (located in index 0 of array2
)14
and 15
array1
is smaller, so 14
becomes the first
element in the merged array20
(the second element
in array1
) to 15
(the first element in array2
)array2
is smaller, so 15
becomes the second element in the larger array20
to 30
, with 20
becoming the third element in the array, and so on.mergesort.py
defines:merge_sort
to initiate the sortingsort_array
to implement the recursive merge sort algorithm—this is called by function merge_sort
merge
to merge two sorted subarrays into a single sorted subarraysubarray_string
to get a subarray’s string representation for output purposes to help visualize the sortmain
tests function merge_sort
merge_sort
¶sort_array
to initiate the recursive algorithm, passing 0
and len(data) - 1
as the low and high indices of the array to be sortedsort_array
to operate on the entire array# mergesort.py
"""Sorting an array with merge sort."""
import numpy as np
# calls recursive sort_array method to begin merge sorting
def merge_sort(data):
sort_array(data, 0, len(data) - 1)
sort_array
¶sort_array
to sort the two subarrays, then merges them.merge
with the indices for the two halves of the array, combines the two sorted arrays into one
larger sorted arraydef sort_array(data, low, high):
"""Split data, sort subarrays and merge them into sorted array."""
# test base case size of array equals 1
if (high - low) >= 1: # if not base case
middle1 = (low + high) // 2 # calculate middle of array
middle2 = middle1 + 1 # calculate next element over
# output split step
print(f'split: {subarray_string(data, low, high)}')
print(f' {subarray_string(data, low, middle1)}')
print(f' {subarray_string(data, middle2, high)}\n')
# split array in half then sort each half (recursive calls)
sort_array(data, low, middle1) # first half of array
sort_array(data, middle2, high) # second half of array
# merge two sorted arrays after split calls return
merge(data, low, middle1, middle2, high)
merge
¶while
loop iterates until the end of either subarray is reached, merging the sorted subarrayswhile
loop completes, one entire subarray has been placed in the combined array, but the other subarray still contains dataif/else
statement after the loop uses slices to fill the appropriate elements of the combined array with the remaining elements data
references# merge two sorted subarrays into one sorted subarray
def merge(data, left, middle1, middle2, right):
left_index = left # index into left subarray
right_index = middle2 # index into right subarray
combined_index = left # index into temporary working array
merged = [0] * len(data) # working array
# output two subarrays before merging
print(f'merge: {subarray_string(data, left, middle1)}')
print(f' {subarray_string(data, middle2, right)}')
# merge arrays until reaching end of either
while left_index <= middle1 and right_index <= right:
# place smaller of two current elements into result
# and move to next space in arrays
if data[left_index] <= data[right_index]:
merged[combined_index] = data[left_index]
combined_index += 1
left_index += 1
else:
merged[combined_index] = data[right_index]
combined_index += 1
right_index += 1
# if left array is empty
if left_index == middle2: # if True, copy in rest of right array
merged[combined_index:right + 1] = data[right_index:right + 1]
else: # right array is empty, copy in rest of left array
merged[combined_index:right + 1] = data[left_index:middle1 + 1]
data[left:right + 1] = merged[left:right + 1] # copy back to data
# output merged array
print(f' {subarray_string(data, left, right)}\n')
subarray_string
¶# method to output certain values in array
def subarray_string(data, low, high):
temp = ' ' * low # spaces for alignment
temp += ' '.join(str(item) for item in data[low:high + 1])
return temp
main
¶def main():
data = np.array([34, 56, 14, 20, 77, 51, 93, 30, 15, 52])
print(f'Unsorted array: {data}\n')
merge_sort(data)
print(f'\nSorted array: {data}\n')
# call main to execute the sort (we removed the if statement from the script in the book)
main()
sort_array
sort_array
with subarrays each approximately half the original array’s size, and a single call to merge
, which requires, at worst, n – 1 comparisons to fill the original array, which is O(n) sort_array
result in four more recursive sort_array calls
, each with a subarray approximately a quarter of the original array’s size, along with two calls to merge
that each require, at worst, n/2 – 1 comparisons, for a total number of comparisons of O(n)sort_array
call generating two additional sort_array
calls and a merge
call until the algorithm has split the array into one-element subarrays©1992–2020 by Pearson Education, Inc. All Rights Reserved. This content is based on Chapter 5 of the book Intro to Python for Computer Science and Data Science: Learning to Program with AI, Big Data and the Cloud.
DISCLAIMER: The authors and publisher of this book have used their best efforts in preparing the book. These efforts include the development, research, and testing of the theories and programs to determine their effectiveness. The authors and publisher make no warranty of any kind, expressed or implied, with regard to these programs or to the documentation contained in these books. The authors and publisher shall not be liable in any event for incidental or consequential damages in connection with, or arising out of, the furnishing, performance, or use of these programs.