def sort_visualize(array, desc=False, visualize=True, sleep_time=SLEEP_TIME, title='', xlabel='', ylabel='', xticks=[], yticks=[], color='b'): """ Function to visualize bubble sort. Parameters: array (arraylike): Array to be sorted. desc (bool): Sort in ascending order if True, else sort in descending order. visualize (bool): Generates visualization if True. sleep_time (float): Time in ms between frames. Other parameters are for the matplotlib plot. Returns: tuple: Tuple containing the sorted array and the time taken for sorting. """ # Sorting a copy of array so that the original array is unchanged array_copy = np.array(array.copy()) # Plotting initial state if(visualize == True): fig, ax = plot.bar(array_copy, title=title, xlabel=xlabel, ylabel=ylabel, xticks=[], yticks=[], color=color) plt.show(block=False) plt.pause(sleep_time) plt.close() sort_time = 0 # Time for sorting (excluding plotting) sorted_upto = -1 # Index upto which array_copy is sorted while(not is_sorted.is_sorted(array_copy, desc=desc)): for i in range(len(array_copy) - 2, sorted_upto, -1): start_time = time.time() # Sorting in ascending order if(desc == False): if(array_copy[i] > array_copy[i + 1]): # Swapping array_copy[i] with array_copy[i + 1] tmp = array_copy[i + 1] array_copy[i + 1] = array_copy[i] array_copy[i] = tmp # Sorting in descending order elif(desc == True): if(array_copy[i] < array_copy[i + 1]): # Swapping array_copy[i] with array_copy[i + 1] tmp = array_copy[i + 1] array_copy[i + 1] = array_copy[i] array_copy[i] = tmp stop_time = time.time() # Adding time taken for iteration to sort_time sort_time += (stop_time - start_time) # Plotting if(visualize == True): fig, ax = plot.bar(array_copy, title=title, xlabel=xlabel, ylabel=ylabel, xticks=[], yticks=[], color=color) ax.annotate(s=MARKER, xy=(i, 0), xytext=(i, MARKER_Y), xycoords='data', textcoords='data', fontsize=MARKER_SIZE, ha='center', fontweight=MARKER_WEIGHT, color=MARKER_COLOR) plt.show(block=False) plt.pause(sleep_time) plt.close() sorted_upto += 1 return array_copy, sort_time
def plot_figure(array, mid, sleep_time=SLEEP_TIME, title='', xlabel='', ylabel='', xticks=[], yticks=[], color='b'): # Plotting fig, ax = plot.bar(array, title=title, xlabel=xlabel, ylabel=ylabel, xticks=xticks, yticks=yticks, color=color) ax.annotate(s=MARKER, xy=(mid, 0), xytext=(mid, MARKER_Y), xycoords='data', textcoords='data', fontsize=MARKER_SIZE, ha='center', fontweight=MARKER_WEIGHT, color=MARKER_COLOR) plt.show(block=False) plt.pause(sleep_time) plt.close()
def plot_figure(array, sleep_time=SLEEP_TIME, title='', xlabel='', ylabel='', xticks=[], yticks=[], color='b'): # Plotting fig, ax = plot.bar(array, title=title, xlabel=xlabel, ylabel=ylabel, xticks=xticks, yticks=yticks, color=color) plt.show(block=False) plt.pause(sleep_time) plt.close()
def sort_visualize(array, desc=False, visualize=True, sleep_time=SLEEP_TIME, title='', xlabel='', ylabel='', xticks=[], yticks=[], color='b'): """Function to visualize insertion sort. Docs same as for bubble sort.""" # Sorting a copy of array so that the original array is unchanged array_copy = np.array(array.copy()) # Plotting initial state if (visualize == True): fig, ax = plot.bar(array_copy, title=title, xlabel=xlabel, ylabel=ylabel, xticks=[], yticks=[], color=color) plt.show(block=False) plt.pause(sleep_time) plt.close() sort_time = 0 # Time for sorting (excluding plotting) for i in range(1, len(array_copy)): start_time = time.time() j = i - 1 element = array_copy[i] stop_time = time.time() # Adding time taken for initialization to sort_time sort_time += (stop_time - start_time) while (j >= 0): # Plotting if (visualize == True): fig, ax = plot.bar(array_copy, title=title, xlabel=xlabel, ylabel=ylabel, xticks=[], yticks=[], color=color) ax.annotate(s=MARKER, xy=(j, 0), xytext=(j, MARKER_Y), xycoords='data', textcoords='data', fontsize=MARKER_SIZE, ha='center', fontweight=MARKER_WEIGHT, color=MARKER_COLOR) plt.show(block=False) plt.pause(sleep_time) plt.close() start_time = time.time() # Sorting in ascending order if (desc == False): if (array_copy[j] > element): array_copy[j + 1] = array_copy[j] j -= 1 else: break # Sorting in descending order elif (desc == True): if (array_copy[j] < element): array_copy[j + 1] = array_copy[j] j -= 1 else: break stop_time = time.time() # Adding time taken for iteration to sort_time sort_time += (stop_time - start_time) array_copy[j + 1] = element return array_copy, sort_time
def sort_visualize(array, desc=False, visualize=True, sleep_time=SLEEP_TIME, title='', xlabel='', ylabel='', xticks=[], yticks=[], color='b'): """Function to visualize selection sort. Docs same as for bubble sort.""" # Sorting a copy of array so that the original array is unchanged array_copy = np.array(array.copy()) sort_time = 0 # Time for sorting (excluding plotting) i = 0 # Iteration index while (not is_sorted.is_sorted(array_copy, desc=desc)): # Part 1: Finding min_index/max_index in array_copy[i:] min_index = i max_index = i for j in range(i + 1, len(array_copy)): start_time = time.time() # Sorting in ascending order if (desc == False): if (array_copy[j] < array_copy[min_index]): min_index = j # Sorting in descending order elif (desc == True): if (array_copy[j] > array_copy[max_index]): max_index = j stop_time = time.time() # Adding time taken for finding min_index/max_index to sort_time sort_time += (stop_time - start_time) # Plotting if (visualize == True): fig, ax = plot.bar(array_copy, title=title, xlabel=xlabel, ylabel=ylabel, color=color) ax.annotate(s=MARKER, xy=(j, 0), xytext=(j, MARKER_Y), xycoords='data', textcoords='data', fontsize=MARKER_SIZE, ha='center', fontweight=MARKER_WEIGHT, color=MARKER_COLOR) plt.show(block=False) plt.pause(sleep_time) plt.close() # Part 2: Swapping array_copy[i] with array_copy[min_index]/ # array_copy[max_index] start_time = time.time() # Sorting in ascending order if (desc == False): tmp = array_copy[min_index] array_copy[min_index] = array_copy[i] array_copy[i] = tmp i += 1 # Sorting in descending order elif (desc == True): tmp = array_copy[max_index] array_copy[max_index] = array_copy[i] array_copy[i] = tmp i += 1 stop_time = time.time() # Adding time taken for finding min_index/max_index to sort_time sort_time += (stop_time - start_time) # Plotting after last swap if (visualize == True): fig, ax = plot.bar(array_copy, title=title, xlabel=xlabel, ylabel=ylabel, color=color) ax.annotate(s=MARKER, xy=(j, 0), xytext=(j, MARKER_Y), xycoords='data', textcoords='data', fontsize=MARKER_SIZE, ha='center', fontweight=MARKER_WEIGHT, color=MARKER_COLOR) plt.show(block=False) plt.pause(sleep_time) plt.close() return (array_copy, sort_time)