def main(): unsorted = [random.randint(-999, 9999) for i in range(0, 25)] print("unsorted list:", unsorted) bubble_sort.sort(unsorted) merge_sort.sort(unsorted) insertion_sort.sort(unsorted) quick_sort.sort(unsorted)
def test_sort_partially_sorts_items_compared_to_pivot(self): values = range(10) pivot = 5 sort(values, pivot) smaller = values[:pivot] larger = values[pivot + 1:] for item in smaller: assert item < pivot for item in larger: assert item > pivot
def test_sort(self): ''' Compare results of sorting a random list with insertion sort against sorting that same list using Python's built sorting. ''' test_list = [] for x in range(10000): test_list.append(randrange(0, 10000)) expected = sorted(test_list) result = quick_sort.sort(test_list) self.assertEqual(expected, result)
def pair_sum(array: list, target: int) -> tuple: """ Returns tuple of numbers from the array that sum up to the given target. Args: array (list): list of numbers target (int): target Returns: tuple: (first number, second number) """ sort(array) i = 0 j = len(array) - 1 while i <= j: if array[i] + array[j] == target: return [array[i], array[j]] elif array[i] + array[j] > target: j -= 1 elif array[i] + array[j] < target: i += 1 return [None, None]
def main(): from quick_sort import sort print "Test quick_sort", sort, "-" * 20 L = get_random_numbers(10) print L print sort(L) from merge_sort import sort print "Test merge_sort", sort, "-" * 20 L = get_random_numbers(10) print L print sort(L) from insertion_sort import sort print "Test insertion_sort", sort, "-" * 20 L = get_random_numbers(10) print L print sort(L) from radix_sort import sort print "Test radix_sort", sort, "-" * 20 L = get_random_numbers(10) print L print sort(L) from heap_sort import sort print "Test heap_sort", sort, "-" * 20 L = get_random_numbers(10) print L print sort(L) pass
def _build_test(_test, _array): if _test == "quick sort": _init_time = dt.now() _array_sorted = quick_sort.sort(_array) _end_time = dt.now() _time_execution = (_end_time - _init_time) return _array_sorted, _time_execution elif _test == "comb sort": _init_time = dt.now() _array_sorted = comb_sort.sort(_array) _end_time = dt.now() _time_execution = (_end_time - _init_time) return _array_sorted, _time_execution else: return 0
def test_sort_ok(self): result = qs.sort(self.unsorted) self.assertEqual(result, self.sorted)
def test_sort_length(self): result = qs.sort(self.unsorted) self.assertEqual(len(result), len(self.unsorted))
import selection_sort import sieve_of_eratosthenes import transposition_cipher """ SORTING """ bubble_sort.sort(array=[3, 2, 11, -1, 0]) array = [0.48, 0.27, 0.12, 0.21, 0.43, 0.25] bucket_sort.sort(array, max_value=max(array)) insertion_sort.sort(array=[3, 2, 11, -1, 0]) merge_sort.sort(array=[3, 2, 11, -1, 0]) array = [3, 2, 11, -1, 0] quick_sort.sort(array, left=0, right=len(array) - 1) selection_sort.sort(array=[3, 2, 11, -1, 0]) lexicographic_order.order(words=['aab', 'aaa', 'aa', 'aaa']) """ SEARCHING """ pattern_search.search(text=list('karykatura'), pattern=list('ka')) bisection_root_finding.bisect(a=-4, b=4, error=0.001) bisection_search.search(array=[-1, 0, 2, 3, 11], target=3) """ STRING OPS """ anagram.anagram(word1=list('arbuz'), word2=list('burza'))
from partition import randomized_partition from random import randint def random_array(length, min=-100, max=100): array = list() for _ in range(length): array.append(randint(min, max)) return array #select_arr = random_array(30, -100, 100) select_arr = [1, 2, 3] print(select_arr) median = quick_select.select(select_arr, len(select_arr) // 2 + 1, simple_partition) if len(select_arr) % 2 == 0: median2 = quick_select.select(select_arr, len(select_arr) // 2, simple_partition) median = (median + median2) / 2 print(select_arr) quick_sort.sort(select_arr, randomized_partition) print(select_arr) print(median) #arr = random_array(500) #arr2 = arr.copy() #print(timeit.timeit(lambda: quick_sort.sort(arr, simple_partition), number=100)) #print(timeit.timeit(lambda: quick_sort.sort(arr2, randomized_partition), number=100))
def test_sortrandorder(self): arr = [random.randint(0, 10000) for i in xrange(0, 10000)] sortarr = sort(arr) for i in xrange(1, 10000): self.assertGreaterEqual(sortarr[i], sortarr[i-1])
def test_an_allready_sorted_array_returns_the_same_array(self): assert sort([1,2]) == [1,2]
return -1 def search_last_less(items, target): """ 查找最后一个小于等于给定值的元素 """ left, right = 0, len(items) - 1 while left <= right: mid = left + ((right - left) >> 1) if items[mid] <= target: if mid == len(items) - 1 or items[mid + 1] > target: return mid left = mid + 1 else: right = mid - 1 return -1 if __name__ == '__main__': items = [random.randint(1, 10) for _ in range(20)] sort(items) print(f'Before: {items}') for _ in range(5): target = random.randint(1, 10) print(f'Target={target}, index={search_last_less(items, target)}, value={items[search_last_less(items, target)]}')
def should_sort_with_quick_sort_with_dups(): a = rand.randint(20, size=40).tolist() expected = sorted(a) quick_sort.sort(a) assert expected == a
def test_sortrandorder(self): arr = [random.randint(0, 10000) for i in xrange(0, 10000)] sortarr = sort(arr) for i in xrange(1, 10000): self.assertGreaterEqual(sortarr[i], sortarr[i - 1])
def test_sort_revorder(self): arr = [i for i in xrange(10000, 0, -1)] sortarr = sort(arr) for i in xrange(1, 10000): self.assertGreaterEqual(sortarr[i], sortarr[i - 1])
def test_sort_returns_an_array_that_is_sorted_lowest_to_highest(self): assert sort([2,1]) == [1,2]
def test_a_large_array_is_sorted_lowest_to_highest(self): assert sort(shuffle([1,2,3,4,5,6,7,8,9,10])) == [1,2,3,4,5,6,7,8,9,10]
def test_a_more_complicated_array_is_sorted_lowest_to_highest(self): assert sort([4,2,3,1]) == [1,2,3,4]
from quick_sort import sort def binary_search(low, high, key): if high >= low: mid = int(low + (high - low) / 2) if key == array[mid]: return mid elif key > array[mid]: return binary_search(mid, high, key) else: return binary_search(low, mid, key) else: return -1 array = [5, 23, 45, 34, 12, 4, 55, 43, 89, 99, 999] sort(array) # sort array using quick sort key = 43 print(binary_search(0, len(array), key))
def test_quick(self): for array in self.test_array: self.assertEqual(quick_sort.sort(array), sorted(array))
def test_sort_revorder(self): arr = [i for i in xrange(10000, 0, -1)] sortarr = sort(arr) for i in xrange(1, 10000): self.assertGreaterEqual(sortarr[i], sortarr[i-1])
def test_correctness(self): self.assertListEqual(sort([]), []) self.assertListEqual(sort([1]), [1]) self.assertListEqual(sort([2, 1]), [1, 2]) self.assertListEqual(sort([2, 1, 3]), [1, 2, 3]) self.assertListEqual(sort([2, 1, 3, 3]), [1, 2, 3, 3]) self.assertListEqual(sort([2, 2, 1, 3, 3]), [1, 2, 2, 3, 3]) self.assertListEqual(sort([1, 2, 3]), [1, 2, 3]) self.assertListEqual(sort([1, 2, 3, 4]), [1, 2, 3, 4]) self.assertListEqual(sort([1, 2, 1, 4]), [1, 1, 2, 4]) self.assertListEqual(sort([1, 2, 1, 2, 4]), [1, 1, 2, 2, 4]) self.assertListEqual(sort([1, 2, 1, 2, 2]), [1, 1, 2, 2, 2])