def sampleMedianSelect(list, left, right, m): if m >= len(list[left:right + 1]): #print("m>len(list)->decreasing m") m = len(list[left:right + 1]) S = random.sample(list[left:right + 1], m) if m <= 20: selectionSort(S) else: heapSort(S) return S[ceil(len(S) / 2)]
def testSort(self): testSorted = [1, 3, 5, 6] test1 = [1 ,3, 5, 6] self.assertEqual(selectionSort(test1), testSorted, 0) self.assertEqual(insertionSort(test1), testSorted, 0) test2 = [1, 3, 6, 5] self.assertEqual(selectionSort(test2), testSorted, 0) self.assertEqual(insertionSort(test2), testSorted, 0) test3 = [6, 5, 3, 1] self.assertEqual(selectionSort(test3), testSorted, 0) self.assertEqual(insertionSort(test3), testSorted, 0)
def StartAlgorithm(): global data if not data: return if (algMenu.get() == 'Intserion Sort'): insertionSort(data, drawData, speedScale.get()) if (algMenu.get() == 'Bubble Sort'): bubble_sort(data, drawData, speedScale.get()) if (algMenu.get() == 'Selection Sort'): selectionSort(data, drawData, speedScale.get()) if (algMenu.get() == 'Merge Sort'): merge_sort(data, drawData, speedScale.get()) if (algMenu.get() == 'Quick Sort'): quick_sort(data, 0, len(data) - 1, drawData, speedScale.get()) drawData(data, ["green" for x in range(len(data))])
sorted_list.append(seq1[index_a]) index_a += 1 else: sorted_list.append(seq2[index_b]) index_b += 1 while index_a < len(seq1): sorted_list.append(seq1[index_a]) index_a += 1 while index_b < len(seq2): sorted_list.append(seq2[index_b]) index_b += 1 return sorted_list if __name__ == '__main__': #randlist1 = np.random.randint(1,100, (10)) #randlist2 = np.random.randint(1,100, (10)) randlist1 = [random.randint(1, 100) for i in range(10)] randlist2 = [random.randint(1, 100) for i in range(10)] print(type(randlist1), type(randlist2)) sorted_list1, _ = selectionSort(randlist1) sorted_list2, _ = selectionSort(randlist2) print(sorted_list1, sorted_list2) mergedSortList = mergeSort(sorted_list1, sorted_list2) print("MergedList", mergedSortList)
def test_already_sorted(self): input = [0,1,2,3,4,5] output = input[:] selectionSort(output) self.assertEqual(output, sorted(input), "Should be [0,1,2,3,4,5]")
def test_backwards_array(self): input = [5, 4, 3, 2, 1] output = input[:] selectionSort(output) self.assertEqual(output, sorted(input), "Should be [1, 2, 3, 4, 5]")
def test_unsorted_array(self): input = [1, 5, 63, 3, 54, 1] output = input[:] selectionSort(output) self.assertEqual(output, sorted(input), "Should be [1, 1, 3, 5, 54, 63]")
def test_empty_array(self): input = [] output = input[:] selectionSort(output) self.assertEqual(output, sorted(input), "Should be []")