Exemple #1
0
    def runTest(self):
        classes = [
            sa.BubbleSort(),
            sa.InsertionSort(),
            sa.MergeSort(),
            sa.ShellSort(),
            sa.QuickSort(),
            sa.SelectionSort()
        ]

        for cls in classes:
            a_list_copy = list(self.a_list)
            cls.sort(a_list_copy)
            error_msg = "List not sorted for %s..." % cls.__class__.__name__
            self.assertEqual(a_list_copy, [0, 2, 3, 4.9, 54, 100, 505],
                             error_msg)
Note - This visualiser only for understanding how sorting algorithms work
and should not be used to compare time complexities as most of the time
spent by the program is drawing the array on screen and not on sorting
If you want to see a direct comarison between algorithm in terms of their
scaling and time complexities go download my sorting algorithm comparison program.
"""

import sorting_algorithms, time, os, sys
os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame

dimensions = (1024, 512)  # The y value should be equal to the array length
algorithms = {"SelectionSort": sorting_algorithms.SelectionSort(), \
              "BubbleSort": sorting_algorithms.BubbleSort(), \
              "InsertionSort": sorting_algorithms.InsertionSort(), \
              "ShellSort": sorting_algorithms.ShellSort(), \
              "RadixSort": sorting_algorithms.RadixSort(), \
              "CocktailSort": sorting_algorithms.CocktailSort(), \
              "GnomeSort": sorting_algorithms.GnomeSort(), \
              "MergeSort": sorting_algorithms.MergeSort(), \
              "QuickSort": sorting_algorithms.QuickSort(), \
              "HeapSort": sorting_algorithms.HeapSort(), \
              "BitonicSort": sorting_algorithms.BitonicSort(), \
              "BucketSort": sorting_algorithms.BucketSort()}

if len(sys.argv) > 1:
    if sys.argv[1] == "list":
        for key in algorithms.keys():
            print(key, end=" ")
        print("")
        sys.exit(0)
Some suggested settings (Leave all else as default)
cutoff = True, max_values[0] = 10000, step = 10
cutoff = False, max_values[0] = 1000, step = 1
"""
import sorting_algorithms, time, csv, os, sys, math

os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
import pygame

# Sorting Algorithm Variables
cutoff = False  # If true sorting algorithms above graph will no longer be used for sorting
# This is good for large max values as sorting will only be done by more efficient algorithms
# Note - this will make the max and min values only based on the current times for algorithms still in use
iterations = 10  # Amount of results incuded in average values
step = 1  # Step between each length of the array
algorithms = [sorting_algorithms.SelectionSort(), sorting_algorithms.BubbleSort(), sorting_algorithms.InsertionSort(), sorting_algorithms.ShellSort(), \
              sorting_algorithms.RadixSort(), sorting_algorithms.CocktailSort(), sorting_algorithms.GnomeSort(), sorting_algorithms.MergeSort(), \
              sorting_algorithms.QuickSort(), sorting_algorithms.HeapSort(), sorting_algorithms.BucketSort()]
off_graph = [
    False
] * 11  # Array of booleans to check if algorithms are off the graph

# Graph Variables
window = (1920, 800)  # Window size
dimensions = (1800, 800)  # Graph size
padding = 80  # Padding of axis from side of window
text_padding = 40  # Padding from axis to text
scale_padding = 10  # Padding from axis to scale
legend_padding = 20  # Padding between legend keys
legend_square_padding = 20  # Padding between the colour square and legend text
legend_square_size = 15  # Size of colour square size
Exemple #4
0
def main():
    global SORTING
    screen.fill(v.BLACK)
    SORTING = False

    # Create panels and call sorting functions
    # Right Side
    bubble_sort = Panel(int(v.width - 450), 90, 300, 70, "BubbleSort")
    quick_sort = Panel(int(v.width - 450), 190, 300, 70, "QuickSort")
    insertion_sort = Panel(int(v.width - 450), 290, 300, 70, "InsertionSort")
    bogo_sort = Panel(int(v.width - 450), 390, 300, 70, "BogoSort")
    # Left side
    cocktail_sort = Panel(int(v.width / 6 - 100), 90, 300, 70, "CocktailSort")
    shell_sort = Panel(int(v.width / 6 - 100), 190, 300, 70, "ShellSort")
    gnome_sort = Panel(int(v.width / 6 - 100), 290, 300, 70, "GnomeSort")
    radix_sort = Panel(int(v.width / 6 - 100), 390, 300, 70, "RadixSort")

    alg = {
        "BubbleSort": sa.BubbleSort(),
        "QuickSort": sa.QuickSort(),
        "InsertionSort": sa.InsertionSort(),
        "BogoSort": sa.BogoSort(),
        "CocktailSort": sa.CocktailSort(),
        "ShellSort": sa.ShellSort(),
        "GnomeSort": sa.GnomeSort(),
        "RadixSort": sa.RadixSort()
    }

    # Choosing sorting function
    flag = True
    while flag:
        sa.status_continue = True
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
            elif event.type == pygame.MOUSEBUTTONDOWN and SORTING == False:
                # PANELS FUNCTIONS
                if pygame.mouse.get_pos(
                )[0] >= bubble_sort.x and pygame.mouse.get_pos(
                )[0] <= bubble_sort.x + bubble_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= bubble_sort.y and pygame.mouse.get_pos(
                    )[1] <= bubble_sort.y + bubble_sort.y0:
                        alg["BubbleSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= quick_sort.x and pygame.mouse.get_pos(
                )[0] <= quick_sort.x + quick_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= quick_sort.y and pygame.mouse.get_pos(
                    )[1] <= quick_sort.y + quick_sort.y0:
                        alg["QuickSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= insertion_sort.x and pygame.mouse.get_pos(
                )[0] <= insertion_sort.x + insertion_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= insertion_sort.y and pygame.mouse.get_pos(
                    )[1] <= insertion_sort.y + insertion_sort.y0:
                        alg["InsertionSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= bogo_sort.x and pygame.mouse.get_pos(
                )[0] <= bogo_sort.x + bogo_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= bogo_sort.y and pygame.mouse.get_pos(
                    )[1] <= bogo_sort.y + bogo_sort.y0:
                        alg["BogoSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= cocktail_sort.x and pygame.mouse.get_pos(
                )[0] <= cocktail_sort.x + cocktail_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= cocktail_sort.y and pygame.mouse.get_pos(
                    )[1] <= cocktail_sort.y + cocktail_sort.y0:
                        alg["CocktailSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= shell_sort.x and pygame.mouse.get_pos(
                )[0] <= shell_sort.x + shell_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= shell_sort.y and pygame.mouse.get_pos(
                    )[1] <= shell_sort.y + shell_sort.y0:
                        alg["ShellSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= gnome_sort.x and pygame.mouse.get_pos(
                )[0] <= gnome_sort.x + gnome_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= gnome_sort.y and pygame.mouse.get_pos(
                    )[1] <= gnome_sort.y + gnome_sort.y0:
                        alg["GnomeSort"].play()
                        flag = False
                if pygame.mouse.get_pos(
                )[0] >= radix_sort.x and pygame.mouse.get_pos(
                )[0] <= radix_sort.x + radix_sort.x0:
                    if pygame.mouse.get_pos(
                    )[1] >= radix_sort.y and pygame.mouse.get_pos(
                    )[1] <= radix_sort.y + radix_sort.y0:
                        alg["RadixSort"].play()
                        flag = False

        pygame.display.update()