def testLostRecords(self): """ Checks that the array has not lost records """ #mock mk1 = [ 767, 554, 1, 4, 554, 767, 554, 14, 45, 554155415541554, 767554, 4 ] mk2 = [ 7671, 554554, 1767, 0, 898, 7671, 554554, 767554, 45, 554155415541554, 767554, 4767 ] mk3 = [ 7675541, 554767554, 17671, 0, 898, 76751, 5546554, 7677554, 47675, 554155415541554, 767554, 4767 ] #check that not lost records def CheckLost(arr1, arr2): return all(i in arr1 for i in arr2) #test self.assertTrue(CheckLost(bubble_sort.bubbleSort(mk1), mk1)) self.assertTrue((CheckLost(bubble_sort.bubbleSort(mk2), mk2))) self.assertTrue(CheckLost(bubble_sort.bubbleSort(mk3), mk3))
def test_large_inputs(self): print("Testing large inputs...") random_list = random.sample(range(-800000, 800000), 10000) sorted_random_list = sorted(random_list) self.assertEqual(bubbleSort(random_list), sorted_random_list) self.assertEqual(insertionSort(random_list), sorted_random_list) self.assertEqual(selectionSort(random_list), sorted_random_list)
def testAnyValue(self): """ tests if the bubblesort returns any value """ #mock mk1 = [767, 554, -1, 4, 554, 767, 554, 14] mk2 = [ 7671, -554554, 1767, 0, 898, 7671, 554554, 767554, 45, 554155415541554, 767554, 4767 ] mk3 = [ -767554554, 554767554, 17671, 0, 898, 76751, 5546554, 7677554, 47675, 554155415541554, 767554, 4767 ] #test self.assertTrue(bubble_sort.bubbleSort(mk1)) self.assertTrue(bubble_sort.bubbleSort(mk2)) self.assertTrue(bubble_sort.bubbleSort(mk3))
def testIfSorted(self): """ Checking that the array is sorted """ #mock mk1 = [ 767, 554, -1, 4, 554, 767, 554, 14, 45, 554155415541554, 767554, 4 ] mk2 = [ 7671, -554554, 1767, 0, 898, 7671, 554554, 767554, 45, 554155415541554, 767554, 4767 ] mk3 = [ -767554554, 554767554, 17671, 0, 898, 76751, 5546554, 7677554, 47675, 554155415541554, 767554, 4767 ] #test self.assertTrue(BubbleSortTest.TestSorted(bubble_sort.bubbleSort(mk1))) self.assertTrue(BubbleSortTest.TestSorted(bubble_sort.bubbleSort(mk2))) self.assertTrue(BubbleSortTest.TestSorted(bubble_sort.bubbleSort(mk3)))
def test_bubbleSort_fail(self): words = self.test_create_WordData_array() sorted_words = bubbleSort(words) self.assertEqual(sorted_words[7].word, 'h')
def test_nativeSort_array_last_val(self): arr = self.test_create_array() sorted_arr = bubbleSort(arr) self.assertEqual(sorted_arr[3], 2)
def test_bubbleSort_fail(self): arr = self.test_create_array() sorted_arr = bubbleSort(arr) self.assertEqual(sorted_arr[7], 1)
################################## ### Title: Module Sorting ######## ### Author: GuoChen Hou ######## ################################## # This program sorts the 7-character module codes of different class sizes in ascending order from bubble_sort import bubbleSort f = open('module_sorting.txt', 'r') enrol = {} population = [] for line in f: module, students = line.split() enrol[module] = int(students) population.append(int(students)) bubbleSort(population) for element in population: for key in enrol.keys(): if enrol[key] == element: print '%s\t\t%d' % (key, element) break
def test_empty_input(self): print("Testing edge cases...") self.assertEqual(bubbleSort([]), []) self.assertEqual(insertionSort([]), []) self.assertEqual(selectionSort([]), [])
f.write(str(a)) del a a = arr[:] print '/nStart merge sort without new space...' t1 = time.clock() merge_sort.mergeSortWithoutNewSpace(a) t2 = time.clock() print 'Merge sort without new space finished. Time used=%fs' % (t2 - t1) f.write('/n/nMerge sort without new space [Time used=%fs]/n' % (t2 - t1)) f.write(str(a)) del a a = arr[:] print '/nStart insert sort...' t1 = time.clock() insert_sort.insertSort(a) t2 = time.clock() print 'Insert sort finished. Time used=%fs' % (t2 - t1) f.write('/n/nInsert sort [Time used=%fs]/n' % (t2 - t1)) f.write(str(a)) del a a = arr[:] print '/nStart bubble sort...' t1 = time.clock() bubble_sort.bubbleSort(a) t2 = time.clock() print 'Bubble sort finished. Time used=%fs' % (t2 - t1) f.write('/n/nBubble sort [Time used=%fs]/n' % (t2 - t1)) f.write(str(a)) del a f.close()
def test_bubbleSort(self): self.assertEqual(bubbleSort([8, 5, 2, 9, 5, 6, 3]), [2, 3, 5, 5, 6, 8, 9]) self.assertEqual(bubbleSort([1]), [1]) self.assertEqual(bubbleSort([1, 2]), [1, 2]) self.assertEqual(bubbleSort([2, 1]), [1, 2]) self.assertEqual(bubbleSort([1, 3, 2]), [1, 2, 3]) self.assertEqual(bubbleSort([3, 1, 2]), [1, 2, 3]) self.assertEqual(bubbleSort([1, 2, 3]), [1, 2, 3]) self.assertEqual( bubbleSort([ -4, 5, 10, 8, -10, -6, -4, -2, -5, 3, 5, -4, -5, -1, 1, 6, -7, -6, -7, 8 ]), [ -10, -7, -7, -6, -6, -5, -5, -4, -4, -4, -2, -1, 1, 3, 5, 5, 6, 8, 8, 10 ]) self.assertEqual( bubbleSort([ -7, 2, 3, 8, -10, 4, -6, -10, -2, -7, 10, 5, 2, 9, -9, -5, 3, 8 ]), [-10, -10, -9, -7, -7, -6, -5, -2, 2, 2, 3, 3, 4, 5, 8, 8, 9, 10]) self.assertEqual( bubbleSort([ 8, -6, 7, 10, 8, -1, 6, 2, 4, -5, 1, 10, 8, -10, -9, -10, 8, 9, -2, 7, -2, 4 ]), [ -10, -10, -9, -6, -5, -2, -2, -1, 1, 2, 4, 4, 6, 7, 7, 8, 8, 8, 8, 9, 10, 10 ]) self.assertEqual( bubbleSort([5, -2, 2, -8, 3, -10, -6, -1, 2, -2, 9, 1, 1]), [-10, -8, -6, -2, -2, -1, 1, 1, 2, 2, 3, 5, 9]) self.assertEqual( bubbleSort([ 2, -2, -6, -10, 10, 4, -8, -1, -8, -4, 7, -4, 0, 9, -9, 0, -9, -9, 8, 1, -4, 4, 8, 5, 1, 5, 0, 0, 2, -10 ]), [ -10, -10, -9, -9, -9, -8, -8, -6, -4, -4, -4, -2, -1, 0, 0, 0, 0, 1, 1, 2, 2, 4, 4, 5, 5, 7, 8, 8, 9, 10 ]) self.assertEqual( bubbleSort([ 4, 1, 5, 0, -9, -3, -3, 9, 3, -4, -9, 8, 1, -3, -7, -4, -9, -1, -7, -2, -7, 4 ]), [ -9, -9, -9, -7, -7, -7, -4, -4, -3, -3, -3, -2, -1, 0, 1, 1, 3, 4, 4, 5, 8, 9 ]) self.assertEqual( bubbleSort([ 427, 787, 222, 996, -359, -614, 246, 230, 107, -706, 568, 9, -246, 12, -764, -212, -484, 603, 934, -848, -646, -991, 661, -32, -348, -474, -439, -56, 507, 736, 635, -171, -215, 564, -710, 710, 565, 892, 970, -755, 55, 821, -3, -153, 240, -160, -610, -583, -27, 131 ]), [ -991, -848, -764, -755, -710, -706, -646, -614, -610, -583, -484, -474, -439, -359, -348, -246, -215, -212, -171, -160, -153, -56, -32, -27, -3, 9, 12, 55, 107, 131, 222, 230, 240, 246, 427, 507, 564, 565, 568, 603, 635, 661, 710, 736, 787, 821, 892, 934, 970, 996 ]) self.assertEqual( bubbleSort([ 991, -731, -882, 100, 280, -43, 432, 771, -581, 180, -382, -998, 847, 80, -220, 680, 769, -75, -817, 366, 956, 749, 471, 228, -435, -269, 652, -331, -387, -657, -255, 382, -216, -6, -163, -681, 980, 913, -169, 972, -523, 354, 747, 805, 382, -827, -796, 372, 753, 519, 906 ]), [ -998, -882, -827, -817, -796, -731, -681, -657, -581, -523, -435, -387, -382, -331, -269, -255, -220, -216, -169, -163, -75, -43, -6, 80, 100, 180, 228, 280, 354, 366, 372, 382, 382, 432, 471, 519, 652, 680, 747, 749, 753, 769, 771, 805, 847, 906, 913, 956, 972, 980, 991 ]) self.assertEqual( bubbleSort([ 384, -67, 120, 759, 697, 232, -7, -557, -772, -987, 687, 397, -763, -86, -491, 947, 921, 421, 825, -679, 946, -562, -626, -898, 204, 776, -343, 393, 51, -796, -425, 31, 165, 975, -720, 878, -785, -367, -609, 662, -79, -112, -313, -94, 187, 260, 43, 85, -746, 612, 67, -389, 508, 777, 624, 993, -581, 34, 444, -544, 243, -995, 432, -755, -978, 515, -68, -559, 489, 732, -19, -489, 737, 924 ]), [ -995, -987, -978, -898, -796, -785, -772, -763, -755, -746, -720, -679, -626, -609, -581, -562, -559, -557, -544, -491, -489, -425, -389, -367, -343, -313, -112, -94, -86, -79, -68, -67, -19, -7, 31, 34, 43, 51, 67, 85, 120, 165, 187, 204, 232, 243, 260, 384, 393, 397, 421, 432, 444, 489, 508, 515, 612, 624, 662, 687, 697, 732, 737, 759, 776, 777, 825, 878, 921, 924, 946, 947, 975, 993 ]) self.assertEqual( bubbleSort([ 544, -578, 556, 713, -655, -359, -810, -731, 194, -531, -685, 689, -279, -738, 886, -54, -320, -500, 738, 445, -401, 993, -753, 329, -396, -924, -975, 376, 748, -356, 972, 459, 399, 669, -488, 568, -702, 551, 763, -90, -249, -45, 452, -917, 394, 195, -877, 153, 153, 788, 844, 867, 266, -739, 904, -154, -947, 464, 343, -312, 150, -656, 528, 61, 94, -581 ]), [ -975, -947, -924, -917, -877, -810, -753, -739, -738, -731, -702, -685, -656, -655, -581, -578, -531, -500, -488, -401, -396, -359, -356, -320, -312, -279, -249, -154, -90, -54, -45, 61, 94, 150, 153, 153, 194, 195, 266, 329, 343, 376, 394, 399, 445, 452, 459, 464, 528, 544, 551, 556, 568, 669, 689, 713, 738, 748, 763, 788, 844, 867, 886, 904, 972, 993 ]) self.assertEqual( bubbleSort([ -19, 759, 168, 306, 270, -602, 558, -821, -599, 328, 753, -50, -568, 268, -92, 381, -96, 730, 629, 678, -837, 351, 896, 63, -85, 437, -453, -991, 294, -384, -628, -529, 518, 613, -319, -519, -220, -67, 834, 619, 802, 207, 946, -904, 295, 718, -740, -557, -560, 80, 296, -90, 401, 407, 798, 254, 154, 387, 434, 491, 228, 307, 268, 505, -415, -976, 676, -917, 937, -609, 593, -36, 881, 607, 121, -373, 915, -885, 879, 391, -158, 588, -641, -937, 986, 949, -321 ]), [ -991, -976, -937, -917, -904, -885, -837, -821, -740, -641, -628, -609, -602, -599, -568, -560, -557, -529, -519, -453, -415, -384, -373, -321, -319, -220, -158, -96, -92, -90, -85, -67, -50, -36, -19, 63, 80, 121, 154, 168, 207, 228, 254, 268, 268, 270, 294, 295, 296, 306, 307, 328, 351, 381, 387, 391, 401, 407, 434, 437, 491, 505, 518, 558, 588, 593, 607, 613, 619, 629, 676, 678, 718, 730, 753, 759, 798, 802, 834, 879, 881, 896, 915, 937, 946, 949, 986 ]) self.assertEqual( bubbleSort([ -823, 164, 48, -987, 323, 399, -293, 183, -908, -376, 14, 980, 965, 842, 422, 829, 59, 724, -415, -733, 356, -855, -155, 52, 328, -544, -371, -160, -942, -51, 700, -363, -353, -359, 238, 892, -730, -575, 892, 490, 490, 995, 572, 888, -935, 919, -191, 646, -120, 125, -817, 341, -575, 372, -874, 243, 610, -36, -685, -337, -13, 295, 800, -950, -949, -257, 631, -542, 201, -796, 157, 950, 540, -846, -265, 746, 355, -578, -441, -254, -941, -738, -469, -167, -420, -126, -410, 59 ]), [ -987, -950, -949, -942, -941, -935, -908, -874, -855, -846, -823, -817, -796, -738, -733, -730, -685, -578, -575, -575, -544, -542, -469, -441, -420, -415, -410, -376, -371, -363, -359, -353, -337, -293, -265, -257, -254, -191, -167, -160, -155, -126, -120, -51, -36, -13, 14, 48, 52, 59, 59, 125, 157, 164, 183, 201, 238, 243, 295, 323, 328, 341, 355, 356, 372, 399, 422, 490, 490, 540, 572, 610, 631, 646, 700, 724, 746, 800, 829, 842, 888, 892, 892, 919, 950, 965, 980, 995 ])
def toIntList(a_list): """Converts a list elements to int type""" number_list = [] for char in char_list: num = int(char) number_list.append(num) return number_list numbers = raw_input('Enter points in succession in (x1 y1 x2 y2 ...) order: ') char_list = list(numbers.split(' ')) number_list = toIntList(char_list) coord = [] coord_list = [] count = 0 for num in number_list: coord.append(num) count += 1 if count == 2: coord_list.append(coord) coord = [] count = 0 coord_value = computeDistance(coord_list) coord_value_mapping = mapCoordValue(coord_value, coord_list) bubbleSort(coord_value) for value in coord_value: print coord_value_mapping[value]
from bubble_sort import bubbleSort alist = [54,26,93,17,77,31,44,55,20] bubbleSort(alist) print(alist)
f.write(str(a)) del a a=arr[:] print '/nStart merge sort without new space...' t1=time.clock() merge_sort.mergeSortWithoutNewSpace(a) t2=time.clock() print 'Merge sort without new space finished. Time used=%fs'%(t2-t1) f.write('/n/nMerge sort without new space [Time used=%fs]/n'%(t2-t1)) f.write(str(a)) del a a=arr[:] print '/nStart insert sort...' t1=time.clock() insert_sort.insertSort(a) t2=time.clock() print 'Insert sort finished. Time used=%fs'%(t2-t1) f.write('/n/nInsert sort [Time used=%fs]/n'%(t2-t1)) f.write(str(a)) del a a=arr[:] print '/nStart bubble sort...' t1=time.clock() bubble_sort.bubbleSort(a) t2=time.clock() print 'Bubble sort finished. Time used=%fs'%(t2-t1) f.write('/n/nBubble sort [Time used=%fs]/n'%(t2-t1)) f.write(str(a)) del a f.close()
from merge_sort import mergeSort from bubble_sort import bubbleSort from normalise_list import normalise import matplotlib.pyplot as plt from timeit import timeit from random import randint listSize = 1000 #Generate a large random list print("Generating list...") listToSort = [randint(0, 1000) for i in range(listSize)] #Create anonymous functions to use with timeit bs = lambda: bubbleSort(listToSort) ms = lambda: mergeSort(listToSort) bis = lambda: sorted(listToSort) print("Timing sorting algorithms...") #Time the function for 100 runs each mergeTime = timeit(ms, number=100) bubbleTime = timeit(bs, number=100) builtInTime = timeit(bis, number=100) #Assemble times into a list for plotting a graph times = [mergeTime, bubbleTime, builtInTime] #Get units for time