def merge_insertion_sort(k, items, p, r): if k > r - p: insertion_sort.insertion_sort(items, p, r) else: q = (p + r) / 2 merge_insertion_sort(k, items, p, q) merge_insertion_sort(k, items, q + 1, r) merge_sort.merge(items, p, q, r)
def test_merge(self): # a is exhausted first a = [1, 2] b = [3, 4] merged_arr = merge(a, b) assert merged_arr == [1, 2, 3, 4] # b is exhausted first a = [3, 4] b = [1, 2] merged_arr = merge(a, b) assert merged_arr == [1, 2, 3, 4]
def test_merge_returns_sorted_array(self): list_a = [i for i in range(5)] list_b = [i for i in range(5, 11)] controll_list = list(list_a + list_b) merged_list = merge(list_a, list_b) self.assertEqual(merged_list, controll_list)
def timsort(arr): if len(arr) <= 32: return insertionSort(arr) mid = len(arr) // 2 left = timsort(arr[:mid]) right = timsort(arr[mid:]) return merge(left, right)
def merge_quick_sort(L): """ This function inputs the value of unsorted list and invokes split function in the file merge_sort to split the list into two halves Then the function invokes the quick_sort function, then again it invokes the merge function in the merge_sort file and returns the sorted list :param L: The list which has to sorted :return: sorted list """ (half1, half2) = merge_sort.split(L) (l1, l2) = quick_sort.quick_sort(half1), quick_sort.quick_sort(half2) return merge_sort.merge(l1, l2)
def merge_quick_sort(L): """A sorting algorithm that utilizes the merge and quick sort algorithms. Each recursion it runs a layer of quick sort and a layer of merge sort.""" # Checks to see if the list is blank if L == []: return [] else: half1, half2 = split(L) less1, same1, more1 = partition(half1) less2, same2, more2 = partition(half2) half1 = merge_quick_sort(less1) + same1 + merge_quick_sort(more1) half2 = merge_quick_sort(less2) + same2 + merge_quick_sort(more2) return merge_sort.merge(half1, half2)
def merge_quick_sort(lst): """ Takes an unsorted list, splits it in half, applies quick_sort on both halves, then merges both halves back together. This will result in a perfectly sorted list. Pre-Conditions: "lst" should be unsorted Post-Conditions: The original "lst" should be sorted :param lst: A inputted unsorted list :return: merge_sort.merge(sorted_lst1, sorted_lst2) """ lst1, lst2 = merge_sort.split(lst) sorted_lst1 = quick_sort.quick_sort(lst1) sorted_lst2 = quick_sort.quick_sort(lst2) return merge_sort.merge(sorted_lst1, sorted_lst2)
def quick_merge_sort(lst): ''' Sorts an unsorted list using a hybrid of quick and merge sort Parameters: lst must be a list Returns: Sorted list ''' if lst == []: return [] half1, half2 = merge_sort.split(lst) (less1, same1, more1) = quick_sort.partition(get_pivot(half1), half1) (less2, same2, more2) = quick_sort.partition(get_pivot(half1), half2) lst1 = quick_merge_sort(less1) + same1 + quick_merge_sort(more1) lst2 = quick_merge_sort(less2) + same2 + quick_merge_sort(more2) return merge_sort.merge(lst1, lst2)
def merge_quick_sort(L): """ This is a sorting algorithm that combines quick_sort and merge_sort. Pre: the argument must be a list Post: list elements are sorted , sorted list is returned :param L: the list that will be sorted """ list1 = [] list2 = [] (evens, odds) = merge_sort.split(L) list1 += quick_sort.quick_sort(evens) list2 += quick_sort.quick_sort(odds) x = merge_sort.merge(list1,list2) return x
def test_basic_sort_should_work(self): data = [ [5, 4, 3], [7, 3, 3, 6], [1, 9, 8, 1], [4, 6, 0] ] result = merge_sort.merge(data) def concat(a, b): a.extend(b) return a expected_result = sorted(reduce(concat, data, [])) for i, value in enumerate(expected_result): assert result[i] == value, "Expected %s, got %s: %s" % (value, result[i], result)
def timesort(array): min_run = 32 n = len(array) # Start by slicing and sorting small portions of the # input array. The size of these slices is defined by # your `min_run` size. for i in range(0, n, min_run): insertion_sort(array, i, min((i + min_run - 1), n - 1)) # Now you can start merging the sorted slices. # Start from `min_run`, doubling the size on # each iteration until you surpass the length of # the array size = min_run while size < n: # Determine the arrays that will # be merged together for start in range(0, n, size * 2): # Compute the `midpoint` (where the first array ends # and the second starts) and the `endpoint` (where # the second array ends) midpoint = start + size - 1 end = min((start + size * 2 - 1), (n - 1)) # Merge the two subarrays. # The `left` array should go from `start` to # `midpoint + `, while the `right` array should # go from `midpoint + 1` to `end + 1` merged_array = merge(left=array[start:midpoint], right=array[midpoint + 1:end + 1]) # Finally, put the merged array back into # your array array[start:start + len(merged_array)] = merged_array # Each iteration should double the size of your arrays size *= 2 return array
def merge_test(): a = [1, 4, 5, 8, 9] b = [2, 3, 22] res = merge(a, b) print(res)
def test_merging(self): merge(self.list_, 0, len(self.list_) / 2, len(self.list_)) self.assertEqual(self.list_, [1, 2, 2, 3, 4, 5, 6, 7])
def test_merge(self): left_side = [1, 3, 5, 7] right_side = [2, 4, 6] merged = merge(left_side, right_side) self.assertEqual(merged, [i for i in range(1, 8)])
def test_merge_given_mixed_values1(self): actual = merge([1, 5], [0, 6, 7]) expected = [0, 1, 5, 6, 7] self.assertListEqual(expected, actual)
def test_merge(): left = [1, 3, 5, 7] right = [2, 4, 5, 6, 8] assert merge(left, right) == [1, 2, 3, 4, 5, 5, 6, 7, 8]
def natural_merge_sort(collection, verbose=False): """Implementation of natural merge sort in Python. Args: collection (list): Input to sort. verbose (bool): Print every rotation if true. Returns: list: The same as the collection, with sort ascending applied. Example: >>> natural_merge_sort([3, 1, 7, 0, 4, 8, 2]) [0, 1, 2, 3, 4, 7, 8] >>> natural_merge_sort([-91, -123, -1]) [-123, -91, -1] >>> natural_merge_sort([32, 13, 46, 78, 96, 15, 27]) [13, 15, 27, 32, 46, 78, 96] >>> natural_merge_sort([]) [] """ if len(collection) == 0: return collection runs = make_runs(collection) if verbose: print("Initial runs: ", end="") [print(collection[run[0]:run[1]+1], end=" ") for run in runs] print("") while len(runs) > 1: i = 0 merged_runs = [] while i < len(runs): # Leave last single item to the next stage. if i + 1 >= len(runs): merged_runs.append(runs[-1]) break left = runs[i][0] middle = runs[i][1] right = runs[i + 1][1] if verbose: print(" Merge " + str(collection[left:middle+1]) + " and " + str(collection[middle+1:right+1])) merge(collection, left, middle, right, verbose, 2) merged_runs.append((left, right)) i += 2 runs = merged_runs return collection
def test_merge_given_largest_in_list2(self): actual = merge([3, 4], [0, 2, 5]) expected = [0, 2, 3, 4, 5] self.assertListEqual(expected, actual)
print '[DEBUG] Connected with ' + addr[0] + ':' + str(addr[1]) addr_list.append(addr) # Start and time distributed computing sorting process start_time = time.time() sections = breakarray(array, procno) # splits array into sections for every client for i in range(procno - 1): # Converts array section into string to be sent arraystring = repr(sections[i+1]) conn.sendto(arraystring, addr_list[i]) print '[DEBUG] Data sent, sorting array...' array = ms.mergesort(sections[procID]) # Sorts section and stores it in array print '[DEBUG] Array sorted.' for i in range(procno - 1): # Receives sorted sections from each client arraystring = '' print '[DEBUG] Receiving data from clients...' while 1: data = conn.recv(4096) arraystring += data if ']' in data: # When end of data is received break print '[DEBUG] Data received, merging arrays...' array = ms.merge(array, eval(arraystring)) # Merges current array with section from client print '[DEBUG] Arrays merged.' conn.close() time_taken = time.time() - start_time print '[DEBUG] Time taken to sort is ', time_taken, 'seconds.'
from merge_sort import merge lst = [10, 8, 4, 12, 13, 14, 5, 7, 6] left = [4, 8, 10, 12, 13] right = [5, 6, 7, 14] merge(left, right, lst) print(lst)
def test_merge_01(): from merge_sort import merge assert merge([3, 5], [2, 9]) == [2, 3, 5, 9]
def test_merge_given_mixed_values2(self): actual = merge([2, 3, 4, 8, 9], [0, 1, 5, 6, 7]) expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] self.assertListEqual(expected, actual)
def test_merges_to_items(self): self.assertEqual([1, 2], merge([1], [2]))
def test_merge_04(): from merge_sort import merge assert merge([], []) == []
def test_merge_05(): from merge_sort import merge assert merge([5, 9, 200], [2, 6, 30]) == [2, 5, 6, 9, 30, 200]
def merge_arrays(array1, array2): return merge_sort(merge(array1, array2))
def test_merge_given_even_pair(self): actual = merge([3, 5, 6], [2, 4, 7]) expected = [2, 3, 4, 5, 6, 7] self.assertListEqual(expected, actual)
def test_merge_02(): from merge_sort import merge assert merge([5], [2]) == [2, 5]
def test_merge_03(): from merge_sort import merge assert merge([5], []) == [5]