def quicksortHelper(lyst, left, right, profiler): ''' Modified to utilize insertion sort when subsegment length was strictly less than specified threshold. ''' # subsegment length below which program switches from quickSort to # insertionSort threshold = 50 profiler.comparison() if left < right: profiler.comparison() if right - left < threshold: insertionSort(lyst, profiler) else: pivotLocation = partition(lyst, left, right, profiler) quicksortHelper(lyst, left, pivotLocation - 1, profiler) quicksortHelper(lyst, pivotLocation + 1, right, profiler)
def test_insertionSort(self): self.assertEqual(insertionSort([10, 5, -4, 25]), [-4, 5, 10, 25]) self.assertEqual(insertionSort([22, 4, 1, 4]), [1, 4, 4, 22]) self.assertEqual(insertionSort([100, -100, 1, 0]), [-100, 0, 1, 100]) self.assertEqual(insertionSort([0, 4, 0, -5, 1, 4]), [-5, 0, 0, 1, 4, 4]) self.assertEqual(insertionSort([]), []) self.assertEqual(insertionSort([0,0,0]), [0,0,0]) self.assertEqual(insertionSort([12314, 235987, -54621]), [-54621, 12314, 235987])
def quicksortHelper(lyst, left, right, profiler): if left < right: mod = profiler.mod() if mod: subLyst = lyst[left:right + 1] if len(subLyst) < 12: insertionSort(subLyst, profiler) lyst[left:right + 1] = subLyst else: pivotLocation = partition(lyst, left, right, profiler) quicksortHelper(lyst, left, pivotLocation - 1, profiler) quicksortHelper(lyst, pivotLocation + 1, right, profiler) else: pivotLocation = partition(lyst, left, right, profiler) quicksortHelper(lyst, left, pivotLocation - 1, profiler) quicksortHelper(lyst, pivotLocation + 1, right, profiler)
from algorithms import bubbleSort, selectionSort, insertionSort elements = input("Enter no of elements, (array will generate automatically) : ") array = [] for x in range(0,int(elements)): array.append(randint(-500,1000)) print("array : \n",array) start = timer() sorted_array = bubbleSort(array) end = timer() print("Sorted array : \n",sorted_array) bubbleSort_time = end - start print("elapsed time for bubble sort :", bubbleSort_time) start = timer() sorted_array = selectionSort(array) end = timer() selectionSort_time = end - start print("elapsed time for selection sort :", selectionSort_time) start = timer() sorted_array = insertionSort(array) end = timer() insertionSort_time = end - start print("elapsed time for insertion sort :", insertionSort_time)
for i in range(len(array)): x = (i * bar_width) + (i * space) # + (WIDTH - (len(array) * bar_width + len(array) * space)) / 2 colour = (80, 0, 255) if swap1 == array[i]: colour = (0, 255, 0) elif swap2 == array[i]: colour = (255, 0, 0) pygame.draw.rect(sc, colour, (x, WIDTH, bar_width, -array[i])) check_events() pygame.display.update() def generate_arr(): a = random.sample(range(100, HEIGHT), 50) return a if __name__ == '__main__': import algorithms array = generate_arr() # for i in array: # print(i, end=" ") updateDisplay(array) # algorithms.bubbleSort(array,"Bubble Sort") # algorithms.selectionSort(array, "Selection Sort") algorithms.insertionSort(array, "Insertion Sort") while True: check_events()