def merge(L, start_index, sublist_size): """ Merge two sublists of a list L Parameters: L - the list start_index - the index of the first element to be merged sublist_size - the size of the chunks to be merged Left chunk: L[start_index] to L[start_index + sublist_size - 1] Right chunk: L[start_index + sublist_size] to L[start_index + 2 * sublist_size - 1] """ index_left = start_index left_stop_index = start_index + sublist_size index_right = start_index + sublist_size right_stop_index = min(start_index + 2 * sublist_size, len(L)) print('Merging sublists:', L[index_left:left_stop_index], 'and', L[index_right:right_stop_index]); L_tmp = [] while (index_left < left_stop_index and index_right < right_stop_index): if L[index_left] < L[index_right]: L_tmp.append(L[index_left]) index_left += 1 else: L_tmp.append(L[index_right]) index_right += 1 if index_left < left_stop_index: L_tmp.extend(L[index_left : left_stop_index]) if index_right < right_stop_index: L_tmp.extend(L[index_right : right_stop_index]) L[start_index : right_stop_index] = L_tmp print('Merged sublist:', L_tmp, '\n')
merge(['Healdsburg', 'Windsor', 'Rohnert Park', 'Santa Rosa'], 0, 2)
This code will do the following to the 4-element list that we passed to it:
def merge_sort(L): """ Sort a list L using the merge sort algorithm. (Starter code doesn't fully sort the list.) """ chunksize = 1 # Start by dividing the list into N sub-lists of 1 element each print("\n*** Sorting sublists of size", chunksize) # Divide the list into pairs of chunks left_start_index = 0 # Start of left chunk in each pair # While we still have chunks to merge while left_start_index + chunksize < len(L): merge(L, left_start_index, chunksize) # Move to next pair of chunks left_start_index += 2 * chunksize print('List is now', L)
Name of input file: cities-small.txt
The original list of cities is:
0: Santa Rosa
1: Petaluma
2: Rohnert Park
3: Windsor
4: Healdsburg
*** Sorting sublists of size 1
Merging sublists: ['Santa Rosa'] and ['Petaluma']
Merged sublist: ['Petaluma', 'Santa Rosa']
Merging sublists: ['Rohnert Park'] and ['Windsor']
Merged sublist: ['Rohnert Park', 'Windsor']
List is now ['Petaluma', 'Santa Rosa', 'Rohnert Park', 'Windsor', 'Healdsburg']
*** Sorting sublists of size 2
Merging sublists: ['Petaluma', 'Santa Rosa'] and ['Rohnert Park', 'Windsor']
Merged sublist: ['Petaluma', 'Rohnert Park', 'Santa Rosa', 'Windsor']
List is now ['Petaluma', 'Rohnert Park', 'Santa Rosa', 'Windsor', 'Healdsburg']
*** Sorting sublists of size 4
Merging sublists: ['Petaluma', 'Rohnert Park', 'Santa Rosa', 'Windsor'] and ['Healdsburg']
Merged sublist: ['Healdsburg', 'Petaluma', 'Rohnert Park', 'Santa Rosa', 'Windsor']
List is now ['Healdsburg', 'Petaluma', 'Rohnert Park', 'Santa Rosa', 'Windsor']
The new list of cities is:
0: Healdsburg
1: Petaluma
2: Rohnert Park
3: Santa Rosa
4: Windsor
Name of input file: cities-small.txt Type S for selection sort and M for merge sort: m The original list of cities is: 0: Santa Rosa 1: Petaluma 2: Rohnert Park 3: Windsor 4: Healdsburg ...etc.