def closest_to_zero(self): self._map_negatives() abs_array = [abs(self._array[x]) for x in range(len(self._array))] sa = sort(abs_array) min_dist = sys.maxint a1 = 0 a2 = 0 for i in range(len(sa)): if (sa[i] in self._neg.keys()): self._neg[sa[i]] = self._neg[sa[i]] - 1 sa[i] = -1 * sa[i] if ((i + 1) < len(sa) and abs(sa[i] + sa[i + 1]) <= min_dist): if (abs(sa[i]) in self._neg.keys() and self._neg[abs(sa[i])] == 0): min_dist = abs(sa[i] + sa[i + 1]) a1 = sa[i] a2 = sa[i + 1] if ((i - 1) >= 0 and abs(sa[i] + sa[i - 1]) <= min_dist): min_dist = abs(sa[i] + sa[i - 1]) a1 = sa[i] a2 = sa[i - 1] return (a1, a2)
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, 100000)) expected = sorted(test_list) result = merge_sort.sort(test_list) self.assertEqual(expected, result)
def test_sorting(self): sorted_list = sort([5, 2, 4, 7, 8, 1, 3, 2, 6]) self.assertEqual(sorted_list, [1, 2, 2, 3, 4, 5, 6, 7, 8])
import insertion_sort import merge_sort import numpy as np import time if __name__ == '__main__': # generate data np.random.seed(233) array = np.random.randint(10000, size=10000) # calculate execution time prev_time = time.time() sorted_array = merge_sort.sort(array) exec_time = time.time() - prev_time # assertion assert (sorted_array == sorted(array)) print(exec_time)
jon = [] #ran = 9000000000000000000000000000000000000000000000000000000000000000000 ran = 2 ** 140 length = [1000, 10000, 30000, 50000] for lent in length: for i in range(0,lent): num = random.randint(100000,ran) A.append(num) #B.append(num) C.append(num) start2 = time.clock() radix_sort.radixsort(C) radix_time = time.clock() - start2 print ("tong use time: ", time.clock() - start2) start = time.clock() merge_sort.sort(A, 0, len(A)) merge_time = time.clock() - start print ("merge sort use: ", time.clock() - start ) jon.append({"x": lent, "mst": merge_time, "rst": radix_time}) f = open('result.json','w') f.write(json.dumps(jon)) #start1 = time.clock() #maopao.mp(B) #print ("maopao use time:", time.clock() - start1)
def test_mergesort_should_handle_edge_cases(self): for k, v in self.i_o: self.assertEqual(merge_sort.sort(k), v)
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_on_sample_list(self): data = [2, -2, 1, 3, 0, 2, -3] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
from random import randint import merge_sort import selection import statistics as s import os rand_array = x = [randint(0, 10000) for p in range(0, 100000)] out = merge_sort.sort(rand_array) array = [4, 2, 3, 1] sorted, operations = merge_sort.sort(array, out_ops=True) print(sorted) print(operations) medsEst, operations = selection.select(rand_array, out_ops=True) medsReal = s.median_low(rand_array) print(medsEst) print(medsReal) print(operations) print(len(rand_array))
def testThreeElementArray(self): arr = [1, 2, 3] arr_sorted = arr self.assertEqual(merge_sort.sort(arr), arr_sorted) arr = [3, 1, 2] self.assertEqual(merge_sort.sort(arr), arr_sorted)
def testLargeArray(self): arr = [random.randint(0, 100000) for i in range(10000)] self.assertEqual(merge_sort.sort(arr), sorted(arr))
def testSingleElementArray(self): arr = [1] self.assertEqual(merge_sort.sort(arr), arr)
def testTwoElementArray(self): arr = [1, 2] self.assertEqual(merge_sort.sort(arr), arr) arr = [2, 1] self.assertEqual(merge_sort.sort(arr), [1, 2])
def test_sort_results_are_in_order(self): array_to_sort = [7, 1, 11, 3, 5, 2, 9, 10, 8, 4, 6] sorted_array = merge_sort.sort(array_to_sort) expected_sort_results = [11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1] self.assertEqual(expected_sort_results, sorted_array)
def test_mergesort_should_handle_edge_cases(self): for k,v in self.i_o: self.assertEqual(merge_sort.sort(k), v)
def test_mergesort_should_sort_lists_correctly(self): for k,v in self.i_o: self.assertEqual(merge_sort.sort(k), v)
def test_merge(self): for arr in self.test_arr: self.assertEqual(merge_sort.sort(arr), sorted(arr))
def testEmptyArray(self): self.assertIsNone(merge_sort.sort([]))
def test_sort_on_negative_elements(self): data = [-2, -1, 0, -3] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
def test_unsorted_list(): unsorted_list = [6, 2, 8, 4, 5, 3, 3, 4, 88, 2, 99, -43, 0] m_sort.sort(unsorted_list) assert unsorted_list == [-43, 0, 2, 2, 3, 3, 4, 4, 5, 6, 8, 88, 99]
def test_sort_on_empty_list(self): data = [] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
def test_empty_list(): empty = [] m_sort.sort(empty) assert empty == []
def test_mergesort_should_sort_lists_correctly(self): for k, v in self.i_o: self.assertEqual(merge_sort.sort(k), v)
def test_sorted_list(): sorted_list = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6] m_sort.sort(sorted_list) assert sorted_list == [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6]
from merge_sort import sort import random n = 100 array = list() while n > 0: array.append(random.randint(1, 10000)) n -= 1 array = sort(array) print(array)
def test_if_sorted_for_different_list(self): list = [8, 7, 6, 5, 4, 3, 2, 1] sorted_list = list.copy() sorted_list.sort() self.assertEqual(sort(list), sorted_list)
def test_sort_inorder(self): arr = [i for i in xrange(0, 10000)] sortarr = sort(arr) for i in xrange(1, 10000): self.assertGreaterEqual(sortarr[i], sortarr[i-1])
def test_single_element_list(self): list = [1] sorted_list = list.copy() sorted_list.sort() self.assertEqual(sort(list), sorted_list)
def test_sort_on_single_element(self): data = [0] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort(data), sorted(data))
import prime_factorisation import prime_number import quick_sort 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 """
def test_two_element_list(self): list = [2, 1] sorted_list = list.copy() sorted_list.sort() self.assertEqual(sort(list), sorted_list)
def test_sort_on_two_elements_out_of_order(self): data = [2, 1] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
def test_sort_on_odd_number_of_elements(self): data = [2, 1, 3] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
def test_sort_on_duplicate_elements(self): data = [2, 1, 2, 3, 1, 3] self.assertEqual(merge_sort.sort(data), sorted(data)) self.assertEqual(merge_sort.sort_efficiently(data), sorted(data))
import sys import globalsed if not "/home/zh/git/python/sort/" in sys.path: sys.path.append("/home/zh/git/python/sort/") import merge_sort def open_txt(): try: infile = file('interArray.txt', 'r') for x in infile.readlines(): passlist.append(x) infile.close() except: return 0 globalsed.init() passlist = [] open_txt() #print len(passlist) #passlist = [3,6,2,4,8] merge_sort.sort(passlist, 0, len(passlist)) print("chang", globalsed.inter)
def test_sort(self): array = [1, 5, 6, 3, 2, 1, 0] merge_sort.sort(array) self.assertEqual(array, [0, 1, 1, 2, 3, 5, 6])