def test_alg(alg, size_array): """ Tests """ t_arrays = [] # Create the random arrays for i in range(N_TESTS_ALG): a = [randint(-2**N_BITS, 2**N_BITS) for i in range(size_array)] t_arrays.append(a) # Insertion Sort if alg == INSERTION_SORT: print("INSERTION_SORT", end=",") print("N=%d" % (size_array)) # Test N times to get a more precise average time for test in range(N_TESTS_ALG): A = list.copy(t_arrays[test]) t1 = time() insertion_sort(A) t2 = time() print("T%d," % test, t2 - t1) elif alg == MERGE_SORT: print("MERGE_SORT", end=",") print("N=%d" % (size_array)) # Test N times to get a more precise average time for test in range(N_TESTS_ALG): A = list.copy(t_arrays[test]) t1 = time() merge_sort(A, 0, len(A) - 1) t2 = time() print("T%d," % test, t2 - t1) elif alg == RADIX_SORT: print("RADIX_SORT", end=",") print("N=%d" % (size_array)) # Test N times to get a more precise average time for test in range(N_TESTS_ALG): A = list.copy(t_arrays[test]) t1 = time() radix_sort(A) t2 = time() print("T%d," % test, t2 - t1) elif alg == BUCKET_SORT: print("BUCKET_SORT", end=",") print("N=%d" % (size_array)) # Test N times to get a more precise average time for test in range(N_TESTS_ALG): A = list.copy(t_arrays[test]) t1 = time() bucket_sort(A, len(A)) t2 = time() print("T%d," % test, t2 - t1)
def test_stable(): """Test identical items in list retain stable position on sort.""" from radix_sort import radix_sort check_list = [1, 0, 2, 3, 2] two_a = check_list[2] two_b = check_list[4] radix_sort(check_list) assert check_list[2] is two_a assert check_list[3] is two_b
def test_radix_sort(build_list): x, y = build_list assert radix_sort(x) == y import random for i in xrange(100): x = [random.randint(10,100) for i in xrange(20)] y = radix_sort(x) z = quick_sort(x) assert y == z
def test_radix_sort(build_list): x, y = build_list assert radix_sort(x) == y import random for i in xrange(100): x = [random.randint(10, 100) for i in xrange(20)] y = radix_sort(x) z = quick_sort(x) assert y == z
def test_stable_random(seq): """Test stability property on random lists.""" from radix_sort import radix_sort seq = list(seq) index_a, index_b = sorted(random.sample(range(len(seq)), 2)) val_a, val_b = 0, 0 seq[index_a], seq[index_b] = val_a, val_b radix_sort(seq) assert seq[0] is val_a assert seq[1] is val_b
def test_of_small_array(self): a = [ '4PGC938', '2IYE230', '3CIO720', '2RLA629',] radix_sort(a) self.assertEqual( a, ['2IYE230', '2RLA629', '3CIO720', '4PGC938',])
def menu_de_algoritmos(): print("Analise de algoritmos separadamente") print("(1) - Quick Sort") print("(2) - Merge Sort") print("(3) - Heap Sort") print("(4) - Radix Sort") option = input("Escolha uma opção: ") quantity = [100, 1000, 10000, 100000] limit = 1000**2 if option == '1': for i in quantity: lista = cria_lista(i, limit) inicio = time.time() quick_sort(lista, 0, len(lista)) fim = time.time() result = fim - inicio print("O tempo de ordenação do quicksort em um vetor de {} elementos é de {} segundos.".format(i, result)) elif option == '2': for i in quantity: lista = cria_lista(i, limit) inicio = time.time() merge_sort(lista) fim = time.time() result = fim - inicio print("O tempo de ordenação do mergesort em um vetor de {} elementos é de {} segundos.".format(i, result)) elif option == '3': for i in quantity: lista = cria_lista(i, limit) inicio = time.time() heapsort(lista) fim = time.time() result = fim - inicio print("O tempo de ordenação do heap em um vetor de {} elementos é de {} segundos.".format(i, result)) elif option == '4': for i in quantity: lista = cria_lista(i, limit) inicio = time.time() radix_sort(lista) fim = time.time() result = fim - inicio print("O tempo de ordenação do radix em um vetor de {} elementos é de {} segundos.".format(i, result))
def test_stable_random_2(seq): """Test that stability fails when sorting to end of list.""" from radix_sort import radix_sort if len(seq) < 3: return seq = list(seq) index_a, index_b = sorted(random.sample(range(len(seq)), 2)) val_a, val_b = 1000, 1000 seq[index_a], seq[index_b] = val_a, val_b radix_sort(seq) assert seq[-1] is val_a assert seq[-2] is val_b
def sort_file(filename, newfilename): L = [] print("Initializing list") buildlist(L, filename) print("List Initialized. Length:", len(L)) print("Sorting List") radix_sort.radix_sort(L) print("List sorted") print("Writing outfile") writefile(L, newfilename) print("Outfile written") print("Done!") return
def test_stable_random_3(seq): """Test stability property on random lists with random duplicate values.""" from radix_sort import radix_sort if len(seq) < 2: return seq = list(seq) index_a = random.randrange(len(seq) - 1) index_b = random.randrange(index_a + 1, len(seq)) val_a = seq[index_a] val_b = int(val_a) seq[index_b] = val_b radix_sort(seq) index_a = seq.index(val_a) assert seq[index_a + 1] is val_b
def test_short_list(self): """ Tests if a short list of integers has been sorted """ l = [5, 2, 8, 2, 6, 1, 454, 8, 23] final = [1, 2, 2, 5, 6, 8, 8, 23, 454] self.assertEqual(radix_sort(l), final)
def route_sorting(): try: toPOST = request.args.get('toPOST') # user input points except: print("GET failed") return jsonify({"message": "request body is not json."}), 400 if toPOST != {}: print("RADIX SORT START!") ### Process history data try: history = data_process(url) # entire historical typhoon tracks point_data = history_point_data(history) # P(i, j) except: return jsonify({"message": "Defective JMA API."}), 406 ### Process similarity model try: U = json.loads(toPOST) # convert user inputs to dict except: return jsonify({"message": "Wrong fromat in points data."}), 400 ### Success return jsonify(radix_sort(history, point_data, U)) else: print('NO USER INPUTS!') return jsonify({"message": "user input is empty."}), 400
def test_empty_list(): """An empty list will return unchanged""" lst = [] expected = [] actual = radix_sort(lst) assert actual == expected
def test_randomly_unsorted_list(): """An unsorted list returns sorted""" lst = [23, 22, 476, 35, 1, 86] expected = [1, 22, 23, 35, 86, 476] actual = radix_sort(lst) assert actual == expected
def test_sorted_list(): """A sorted list will return unchanged""" lst = [1, 22, 23, 35, 586, 1476] expected = [1, 22, 23, 35, 586, 1476] actual = radix_sort(lst) assert actual == expected
def test_radix_sort_against_python_sort(): """Test radix sort against python's sort method.""" from radix_sort import radix_sort input_list = [randint(0, 100000) for i in range(1000)] test_list = radix_sort(input_list) input_list.sort() assert test_list == input_list
def test_single_negative(self): arr = [-1] res = radix_sort(arr) expected = [-1] self.assertFalse(not res) self.assertEqual(expected, res) self.assertEqual(len(arr), len(res))
def test_backward_list(): """A backward list will return reversed""" lst = [1476, 586, 35, 23, 22, 1] expected = [1, 22, 23, 35, 586, 1476] actual = radix_sort(lst) assert actual == expected
def test_radix_sort(self): d = 10 A = [randint(10**(d - 1), 10**d - 1) for _ in range(30)] B = A.copy() A.sort() B = radix_sort(B, d) self.assertEqual(A, B)
def test_single_item_list(): """A one-itemed list will return unchanged""" lst = [100] expected = [100] actual = radix_sort(lst) assert actual == expected
def test_float_list(self): """ Tests if a list of floating point numbers has been sorted """ l = [4.526378, 3.345, 1.111, 2.89, 0.3456] final = [0.3456, 1.111, 2.89, 3.345, 4.526378] self.assertEqual(radix_sort(l), final)
def test_radix_sort(self): """ Test that it can sort a list of numbers with the radix algorithm. """ data = [4, 78, 2, 33, 0, 99, 86, 4, 21, 3] expected = [0, 2, 3, 4, 4, 21, 33, 78, 86, 99] output = radix_sort(data) self.assertEqual(output, expected)
def testRadix(self): result = copy(self.original) before = time.time() result = radix_sort(result) after = time.time() print("Radix Sort, size: %d time: %f" % (self.list_length, after-before)) #print "Original List is: ", self.original #print "Result of Radix Sort is: ", result self.assertEqual(self.sorted_list, result, "Radix Sort Failed")
def test_long_list(self): """ Tests if a long list of integers has been sorted """ l = [ 5, 8, 2, 6, 3, 7, 3, 7, 1, 5, 789, 2344, 6, 1, 6, 7, 234, 8, 56, 4, 8, 234, 0, 12, 0 ] final = [ 0, 0, 1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 7, 7, 7, 8, 8, 8, 12, 56, 234, 234, 789, 2344 ] self.assertEqual(radix_sort(l), final)
def comparativo_de_algoritmos(): limit = 1000**2 results_quicksort = [] results_mergesort = [] results_heapsort = [] results_radixsort = [] for i in range(0,100): lista = cria_lista(1000, limit) lista_quick = lista inicio = time.time() quick_sort(lista_quick, 0, len(lista_quick)) fim = time.time() tempo = fim - inicio results_quicksort.append(tempo) lista_merge = lista inicio = time.time() merge_sort(lista_merge) fim = time.time() tempo = fim - inicio results_mergesort.append(tempo) lista_heap = lista inicio = time.time() heapsort(lista_heap) fim = time.time() tempo = fim - inicio results_heapsort.append(tempo) lista_radix = lista inicio = time.time() radix_sort(lista_radix) fim = time.time() tempo = fim - inicio results_radixsort.append(tempo) plota_grafico(results_quicksort, results_mergesort, results_heapsort, results_radixsort)
def test_predictably(self): dict_of_base_two_lists = { 'list_zero': [0, 0, 0, 0, 0, 0, 0, 0], 'list_one': [0, 0, 0, 0, 1, 1, 1, 1], 'list_two': [0, 1, 0, 1, 0, 1, 0, 1], 'list_three': [0, 1, 1, 0, 1, 1, 0, 0], 'list_four': [1101, 1110, 1010, 1011, 1100, 1111, 1000, 1001] } dict_of_base_ten_lists = { 'list_six': [10, 11, 12, 13, 26, 27, 28, 29], 'list_four': [56372, 57353, 98124, 12427, 35243, 25352, 53723], 'list_nine': [26, 27, 28, 29, 10, 11, 12, 13], 'list_five': [9, 8, 4, 3, 5, 6, 2, 1, 0, 7] } for each_key in dict_of_base_two_lists: each_list = dict_of_base_two_lists[each_key] sorted_list = r_s.radix_sort(each_list, base_of_each_digit=2) assert len(sorted_list) == len(each_list) for each_number in range(0, (len(each_list) - 1)): assert (sorted_list[each_number] <= sorted_list[(each_number + 1)]) for each_key in dict_of_base_ten_lists: each_list = dict_of_base_ten_lists[each_key] sorted_list = r_s.radix_sort(each_list) assert len(sorted_list) == len(each_list) for each_number in range(0, (len(each_list) - 1)): assert (sorted_list[each_number] <= sorted_list[(each_number + 1)])
def test_randomly(self): for each_pass in range(0, 1000): random_order_of_magnitude = (10 ** random.randint(1, 10)) random_list = [] for each_number in range(0, random.randint(3, 40)): # E.g., 1000 to 9999 or 10 to 99 or 10000000 to 99999999 topend = (random_order_of_magnitude * 10) - 1 random_number = random.randint(random_order_of_magnitude, topend) random_list.append(random_number) # Verify the above worked... # preferably in the most humorously awkward yet direct way for each_number_index in range(1, len(random_list)): assert (len(str(random_list[each_number_index])) == (len(str(random_list[(each_number_index - 1)])))) sorted_random_list = r_s.radix_sort(random_list) assert len(random_list) == len(sorted_random_list) for each_number in range(0, (len(sorted_random_list) - 1)): assert (sorted_random_list[each_number] <= sorted_random_list[(each_number + 1)])
def test_common(self): arr = [123, 43, 54536565, 134, 220, 562, 6, 54, 0, 87, 4547585756456, 555, 100, 555, 555, 777, 777, 777, 776] res = radix_sort.radix_sort(arr) exp = [0, 6, 43, 54, 87, 100, 123, 134, 220, 555, 555, 555, 562, 776, 777, 777, 777, 54536565, 4547585756456] self.assertEqual(exp, res)
def test_radix_sort_already_sorted(self): d = 10 A = sorted([randint(10**(d - 1), 10**d - 1) for _ in range(30)]) B = A.copy() B = radix_sort(B, d) self.assertEqual(A, B)
def test_worst_case(): worst_case = [random.randint(10 ** 3, 10 ** 4) for x in range(10000)] assert radix_sort(worst_case) == sorted(worst_case)
def test_empty_list(): assert radix_sort([]) == []
def test_repeating_list(): repeating = [2] * 50 assert radix_sort(repeating) == [2] * 50
def test_all_zeros_in_ones_place(): """Handels zeros in ones place.""" assert radix_sort([110, 10, 20, 150]) == [10, 20, 110, 150]
def test_trivial_2(self): arr = [123456789] res = radix_sort(arr) expected = [123456789] self.assertFalse(not res) self.assertEqual(expected, res)
def test_empty(self): arr = [] res = radix_sort(arr) expected = [] self.assertEqual(expected, res)
def test_radix_sort_on_empty(): assert radix_sort([]) == []
def test_radix_sort_on_list_length_one(): assert radix_sort([1]) == [1]
def test_sorted(self): arr = [5, 123, 352, 456, 1235, 1236, 1237, 1238] res = radix_sort.radix_sort(arr) self.assertEqual(arr, res)
def test_odd_list(): """Making sure the radix sort works with an odd list.""" numbers = [17, 11, 14, 99, 20] assert radix_sort(numbers) == sorted(numbers)
attr_dict, attr_values, attr_value_NO, attr_list, data_pic_path = data_pickle.openfile( path) attr_num = len(attr_list) lists = [[] for i in xrange(attr_num)] key = [[] for i in xrange(attr_num)] offset = [[] for i in xrange(attr_num)] # attr_num = 1 total_row = len(attr_values[0]) for idx in range(attr_num): input_data = numpy.array(attr_values[idx]) length = input_data.shape[0] rid = numpy.arange(0, length, dtype='int64') #step1 sort radix_sort.radix_sort(input_data, rid) print rid print rid.dtype cardinality = len(attr_value_NO[idx].items()) literal = numpy.zeros(length, dtype='uint32') chunk_id = numpy.zeros(length, dtype='int64') stream = cuda.stream() #d_rid = cuda.to_device(rid, stream) d_chunk_id = cuda.to_device(chunk_id, stream) d_literal = cuda.to_device(literal, stream) #step2 produce chunk_id and literal produce_chId_lit_gpu[length / tpb + 1, tpb](rid, d_literal, d_chunk_id, length) #d_rid.to_host(stream) d_chunk_id.to_host(stream)
def test_single_item_in_list(): """When there is only one value, what will it do.""" assert radix_sort([1]) == [1]
def test_radix_sort_on_reverse_order_list(): assert radix_sort([4, 3, 2, 1, 1]) == [1, 1, 2, 3, 4]
def test_random(): random_case = [random.randint(0, 50) for i in range(50)] assert radix_sort(random_case) == sorted(random_case)
def test_long_array(self): arr = [random.randint(0, 1000000) for i in range(200000)] res = radix_sort(arr) self.assertTrue(self.check_sorted(res)) self.assertEqual(len(arr), len(res))
def test_reversed_sorted(): reversed_case = range(50, -1, -1) assert radix_sort(reversed_case) == range(51)
def test_radix_sort_on_ordered_list(): assert radix_sort([1, 2, 2, 3, 4]) == [1, 2, 2, 3, 4]
def test_list_len_one(): assert radix_sort([2]) == [2]
def test_multiple_negative(self): arr = [random.randint(-1000000, -1) for i in range(100000)] res = radix_sort(arr) self.assertTrue(self.check_sorted(res)) self.assertEqual(len(arr), len(res))
def test_radix_sort_on_unordered_list(): assert radix_sort([4, 1, 9, 200, 15]) == [1, 4, 9, 15, 200]
def test_random_input_3(self): arr = [random.randint(-1000000, 1000000) for i in range(100000)] res = radix_sort(arr) self.assertTrue(self.check_sorted(res)) self.assertEqual(len(arr), len(res))
offset[i] = offset[i-1] + key_idx_length[i-1] return key_idx_length,offset ''' if __name__ == '__main__': path = 'data.txt' #file path attr_dict,attr_values,attr_value_NO = data.openfile(path) print attr_dict['worker_class'], attr_value_NO[0] total_row = len(attr_values[0]) input_data = numpy.array(attr_values[0]) print input_data length = input_data.shape[0] rid = numpy.arange(0,length) #step1 sort radix_sort.radix_sort(input_data,rid) print input_data f1 = open('input_data.txt', 'w') f1.write(str(list(input_data))) f2 = open("rid.txt", 'w') f2.write(str(list(rid))) f1.close() f2.close() #cardinality = input_data[-1]+1 cardinality = len(attr_dict['worker_class']) print 'rid:\n',rid literal = numpy.zeros(length, dtype = 'uint32') chunk_id = numpy.zeros(length, dtype = 'int64')
def test_radixs_0_1(): """Another radix test.""" sorted_list = [11, 1, 11, 11, 11, 11, 11] sorted_list.sort() assert radix_sort([11, 11, 11, 11, 11, 11, 1]) == sorted_list
def test_trivial_3(self): arr = [0] res = radix_sort(arr) expected = [0] self.assertFalse(not res) self.assertEqual(expected, res)
def test_negative(self): arr = [5, -4, -35, -2, 0, 21, -21, 88] res = radix_sort.radix_sort(arr) exp = [-35, -21, -4, -2, 0, 5, 21, 88] self.assertEqual(res, exp)
def get_pic_path(path): #print 'open source file in bitmap_pickle: '.strip() start = time.time() attr_dict, attr_values, attr_value_NO, attr_list, data_pic_path = data_pickle.openfile( path) end = time.time() #print str(end-start) #print 'index part(get bitmap, keylength and offset): '.strip() start = time.time() attr_num = len(attr_list) lists = [[] for i in xrange(attr_num)] key = [[] for i in xrange(attr_num)] offset = [[] for i in xrange(attr_num)] # attr_num = 1 total_row = len(attr_values[0]) for idx in range(attr_num): input_data = numpy.array(attr_values[idx]) length = input_data.shape[0] rid = numpy.arange(0, length) #step1 sort #print 'time in step1--sort:' start = time.time() radix_sort.radix_sort(input_data, rid) end = time.time() #print str(end-start) cardinality = len(attr_value_NO[idx].items()) literal = numpy.zeros(length, dtype='uint32') chunk_id = numpy.zeros(length, dtype='int64') #print 'time in step2--produce chId_lit:' start = time.time() stream = cuda.stream() #d_rid = cuda.to_device(rid, stream) d_chunk_id = cuda.to_device(chunk_id, stream) d_literal = cuda.to_device(literal, stream) #step2 produce chunk_id and literal produce_chId_lit_gpu[length / tpb + 1, tpb](rid, d_literal, d_chunk_id, length) #d_rid.to_host(stream) d_chunk_id.to_host(stream) d_literal.to_host(stream) stream.synchronize() end = time.time() #print str(end-start) #step3 reduce by key(value, chunk_id) #print 'time in step3--reduce by key:' start = time.time() reduced_input_data, reduced_chunk_id, reduced_literal = reduce_by_key( input_data, chunk_id, literal, length) reduced_length = reduced_input_data.shape[0] #row end = time.time() #print str(end-start) #print '##############################reduced############################' #for i in xrange(reduced_length): # print reduced_input_data[i], reduced_chunk_id[i], bin(reduced_literal[i]) #step4 produce 0-Fill word #print 'time in step4--produce 0-fill word:' start = time.time() fill_word, head = produce_fill(reduced_input_data, reduced_chunk_id, reduced_length) end = time.time() #print str(end-start) #step 5 & 6: get index by interleaving 0-Fill word and literal(also remove all-zeros word) #print 'time in step5--get out_index & length & offset:' start = time.time() out_index, offsets, key_length = getIdx(fill_word, reduced_literal, reduced_length, head, cardinality) end = time.time() #print str(end-start) lists[idx] = out_index key[idx] = key_length offset[idx] = offsets end = time.time() #print str(end-start) ''' print '*****************index:' print lists print '*****************length:' print key print '*****************offset:' print offset ''' print 'put index result into file: '.strip() start = time.time() bitmap_pic_path = 'bitmap_pic.pkl' f1 = open(bitmap_pic_path, 'wb') pickle.dump(lists, f1, True) pickle.dump(key, f1, True) pickle.dump(offset, f1, True) f1.close() end = time.time() print str(end - start) return data_pic_path, bitmap_pic_path, attr_num
def test_different_length_ints(self): arr = [random.randint(0, 1000000) for i in range(1000)] res = radix_sort(arr) self.assertTrue(self.check_sorted(res)) self.assertEqual(len(arr), len(res))