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 test_partition_does_not_contain_pivot(self): values = range(10) pivot = 5 a, b = partition(values, pivot) assert pivot not in a assert pivot not in b
def test_partition_returns_two_split_arrays(self): values = range(10) pivot = 5 a, b = partition(values, pivot) assert isinstance(a, list) assert isinstance(b, list)
def test_partition(): arr = [0, 5, 2, 6, 8, 4] pivot_index = partition(arr, 0, len(arr) - 1) assert arr == [0, 2, 4, 6, 8, 5] assert pivot_index == 2
def quickselect_(A, k): original = A.copy() pivot_location, C = partition(A) #print("original {} modified {} pivot_location {} .. pivot {} ... finding k {}".format(original, C, pivot_location, C[pivot_location], k)) if pivot_location == k: # exact kth element return C[pivot_location] elif pivot_location > k: # search in left side return quickselect_(C[:pivot_location], k) else: # search in right side return quickselect_(C[pivot_location + 1:], k - pivot_location - 1)
def find_mid_number(number_list): len_number = len(number_list) low, high = 0, len_number - 1 mid_index = len_number / 2 while high > low: j = partition(number_list, low, high) if j == mid_index: return number_list[j] elif j > mid_index: high = j - 1 else: low = j + 1
def quick_select(nums, k): pi = qs.partition(nums, 0, len(nums) - 1) #print nums #print pi while pi != k - 1: if pi > k - 1: ## the kth element is in the left partition return quick_select(nums[:pi], k) else: # the kth element is in the right partition return quick_select(nums[pi + 1:], k) if pi == k - 1: return nums[0:k] #if you return nums[k-1] , you can do kth element
def kth_int(arr, k, start, end): if start == end: return start p = partition(arr, start, end, start) if p == k - 1: return p elif p > k - 1: p = kth_int(arr, k, start, p - 1) elif p < k - 1: p = kth_int(arr, k, p + 1, end) else: print "Exception!" return p
def find_k(arr, k): start = 0 end = len(arr) - 1 if end < 2: return None # 目标下标 dist = len(arr) - k r = None while dist != r: r = partition(arr, start, end) if r > dist: end = r - 1 elif r < dist: start = r + 1 return arr[r]
def select(list, k, start=0, end=None): '''this function takes a list and a integer k as input, and will return the kth largest element''' list_length = len(list) if start == 0 and end == None: list = list.copy() #copy the original input if end == None: end = list_length index = random.randint(start, end - 1) last_num = list[end - 1] list[end - 1] = list[index] list[index] = last_num #exchange a random element to the last position _k_ = partition(list, start, end) if k == _k_: pass if k > _k_: select(list, k, _k_ + 1, end) if k < _k_: select(list, k, start, _k_) return list[k]
def quick_sort_m3(numbers: list, l:int , r: int) -> None: """Divide and Conquer sort algorithm. Partition elements until smallest arrays. Parameters: numbers (list): the main list l (int): most left of the list r (int): most right of the list Returns: None """ if r <= l: return # 3 lines to improve partition performance cmpexch(numbers, (l + r)//2, r) cmpexch(numbers, l, (l+r)//2) cmpexch(numbers, r, (l+r)//2) j = partition(numbers, l, r) quick_sort_m3(numbers, l, j-1) quick_sort_m3(numbers, j+1, r)
def randomized_partition(A, l, r): i = randint(l, r) A[r], A[i] = A[i], A[r] return partition(A, l, r)
def random_partition(array, start, end): i = random.randint(start, end) array[i], array[end] = array[end], array[i] return partition(array, start, end)
def test_partition_given_odd_count_of_nums(self): actual = partition([3, 5, 1, 6, 0]) expected = [6, 5, 1, 3, 0] self.assertListEqual(expected, actual)
def test_partition_for_given_sample(self): actual = partition([1, 2, 0, 8, 9, 3, 5, 4, 7, 6]) expected = [9, 8, 5, 7, 6, 3, 0, 4, 1, 2] self.assertListEqual(expected, actual)
def test_1(self): self.test_array = [2, 8, 7, 1, 3, 5, 6, 4] self.assertEqual(quick_sort.partition(self.test_array, 0, len(self.test_array) - 1), 3)
def test_partition(self): arr = [4, 2, 8, 5, 7, 1] i = partition(arr, 0, 5) self.assertEqual(arr[i], 4)
def random_partition(array, start, end): i = random.randint(start, end) array[i], array[end] = array[end],array[i] return partition(array, start, end)
from quick_sort import partition def quick_select.py(givenList, left, right, k): split = partition(givenList, left, right) if split == k: return givenList[split] elif split < k: return quick_select(givenList, split + 1, right, k) else: return quick_select(givenList, left, split - 1, k)
def test_partition_given_even_count_of_nums(self): actual = partition([16, 4, 10, 14, 7, 9, 3, 2, 8, 1]) expected = [16, 14, 10, 8, 7, 9, 3, 2, 4, 1] self.assertListEqual(expected, actual)
def rank(self, integer): array = [i.value for i in self.integers] index = array.index(integer) left, right = partition(array[0:index] + array[index + 1:], integer) return len(left)