def main():
    # create the data object and load the data from the files
    sdata = sortingdata.SortingData()

    # create instances of each sorting object
    bsort = bubblesort.BubbleSort()
    isort = insertionsort.InsertionSort()
    ssort = selectionsort.SelectionSort()
    qsort = quicksort.QuickSort()
    msort = mergesort.MergeSort()
    sorts = [bsort, isort, ssort, qsort, msort]

    # do runs by size, display the results, and record the results
    results = doRunsBySize(sorts, sdata)
    displayResults(
        results
    )  # this pauses the code execution so we typically leave one of these clocks to execute
    with open(f'results/results-bysize-{time.time()}.json',
              'w') as file_object:
        json.dump(results, file_object)

    # do runs by sortedness, display the results, and record the results
    results = doRunsBySortedness(sorts, sdata)
    displayResults(results)
    with open(f'results/results-bysortedness-{time.time()}.json',
              'w') as file_object:
        json.dump(results, file_object)
 def QuickSortFunction(self):
     QuickInitialTime = time.time()
     for i in range(0, 20):
         UnsortedArrayForQuick = self.UnsortedArray.copy()
         quicksort.QuickSort(UnsortedArrayForQuick, 0,
                             len(UnsortedArrayForQuick) - 1)
         self.labelSortedArray.setText(str(UnsortedArrayForQuick))
     QuickFinalTime = time.time()
     QuickTotalTime = (QuickFinalTime - QuickInitialTime) / 20
     self.labelQuickTime.setText(str(QuickTotalTime))
Exemple #3
0
def msd(iterable, depth=0):
    """Iterate over the sequences in ``iterable`` in lexicographical order,
    determined using MSD radix sort.
    
    The optional ``depth`` parameter determines the position from which to
    start comparing sequences.  Default is 0.
    
    Note that this function returns a generator, not a list.  If a sorted
    list is desired, invoke it with ``list(msd(iterable))``.
    
    """
    # A naive recursive implementation would blow Python's recursion
    # limit on larger inputs, so we emulate boundless recursion with
    # a simple stack.
    stack = [(iterable, depth)]
    while stack:
        bucket, depth = stack.pop()
        if depth < 0:
            # Negative depth is used to mark pre-sorted buckets; we can
            # output these as-is.
            for string in bucket:
                yield string
        else:
            # A Python dict would be more intuitive here, but as it does not
            # preserve key ordering, we allocate a list of buckets and use
            # each character's Unicode code point number as its address in
            # the table.
            buckets = [list() for x in range(256)]
            for string in bucket:
                # Strings shorter than the sorting depth come first in the
                # in-bucket ordering, so we can output them as-is.  The
                # rest are appended to the appropriate buckets.
                if len(string) <= depth:
                    yield string
                else:
                    buckets[ord(string[depth])].append(string)
            # The newly filled buckets are pushed to the top of the stack in
            # reverse order.  This guarantees that the lexicographically first
            # bucket is always at the top of the stack, so when its contents
            # are eventually pushed to output, they come first as well.
            for bucket in reversed(buckets):
                # Buckets smaller than the alphabet are better sorted with
                # string quicksort, so we use Arturs's implementation and mark
                # the bucket as pre-sorted.  The rest are pushed to the stack
                # as-is, with the sorting depth incremented by one.
                if len(bucket) < 256:
                    stack.append((quicksort.QuickSort(bucket, depth + 1), -1))
                else:
                    stack.append((bucket, depth + 1))
#<editor-fold desc="Imports">
import quicksort as qsort
import utils as u
#</editor-fold>

#<editor-fold desc="Initialisation">
qs = qsort.QuickSort()
#</editor-fold>


def main():
    u.TimedSortFunction(u.InitialSetup())


if __name__ == '__main__':
    try:
        main()
    except Exception as ex:
        print(ex)
Exemple #5
0
    start, stop = (int(start), int(stop)) if start <= stop else (int(stop),
                                                                 int(start))
    length = int(abs(length)) if length else 0
    random_list = []
    for i in range(length):
        random_list.append(random.randint(start, stop))

    return random_list


arr = [
    1, 4, 2, 3, 6, 6, 43, 6, 8, 45, 3, 1, 53, 7, 45, 3, 64, 7, 10000, 999999,
    44, 2, 8, 5, 3, 7
]
print("example initial array:\n", arr)
quicksort.QuickSort(arr, 0, len(arr) - 1)
print("example result array:\n", arr)
arr1 = random_int_list(1, 1000000, length=1000)
#print("initial array next\n",arr1)
print("array's size", len(arr1))
print("さあ、ゲームが始めよ!")
time_start = time.time()
quicksort.QuickSort(arr1, 0, len(arr1) - 1)
print("終わりましたよ\__嘘です!びっくりした?")
time_end = time.time()
print("running time= ", time_end - time_start, 's')

arr1 = random_int_list(1, 1000000, length=10000)
#print("initial array next\n",arr1)
print("array's size", len(arr1))
print("さあ、ゲームが続けよ!")
Exemple #6
0
import pygame
import bubble_sort
import selection_sort
import insertion_sort
import quicksort
import mergesort

dimensions = (1400, 700)

statuses = ["Currently Sorting", "Sorting Complete!"]

sorting_algorithms = {
    "bubble_sort": bubble_sort.BubbleSort(),
    "selection_sort": selection_sort.SelectionSort(),
    "insertion_sort": insertion_sort.InsertionSort(),
    "quick_sort": quicksort.QuickSort(),
    "merge_sort": mergesort.MergeSort()
}

pygame.init()

# make window of desired dimensions
display = pygame.display.set_mode((dimensions[0], dimensions[1]))

# make background white
display.fill(pygame.Color("white"))


def check_events():
    """As needed in Pygame apps, checks if the algorithm is done. If it is, exit program"""
    for event in pygame.event.get():
    def CompareFunc(self):
        self.graphicsView.clear()
        self.StepsCounter = int(self.spinBoxLengthSteps.value())
        InsertionTimes = []
        MergeTimes = []
        QuickTimes = []
        RandQuickTimes = []
        BubbleTimes = []
        CountingTimes = []
        HeapTimes = []
        RadixTimes = []
        x = []
        x.append(self.StepsCounter)
        while self.StepsCounter < (int(self.spinBoxMaxLength.value()) + 1):
            QApplication.processEvents()
            self.CreateRandomArrayFunctionForComparison()
            if self.checkBoxInsertion.isChecked():
                InsertionInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayForInsertion = self.UnsortedArrayComparison.copy(
                    )
                    QApplication.processEvents()
                    Insertion.sorting(UnsortedArrayForInsertion)
                InsertionFinal = time.time()
                InsertionTotal = (InsertionFinal - InsertionInitial) / 10
                InsertionTimes.append(InsertionTotal)
            else:
                InsertionTimes = []

            if self.checkBoxBubble.isChecked():
                BubbleInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayBubble = self.UnsortedArrayComparison.copy()
                    QApplication.processEvents()
                    Bubble.Sorting(UnsortedArrayBubble)
                BubbleFinal = time.time()
                BubbleTotal = (BubbleFinal - BubbleInitial) / 10
                BubbleTimes.append(BubbleTotal)
            else:
                BubbleTimes = []

            if self.checkBoxCounting.isChecked():
                CountingInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayCounting = self.UnsortedArrayComparison.copy()
                    maxRange = int(self.spinBoxMaxLength.value()) * 2
                    QApplication.processEvents()
                    countingSort.countSort(UnsortedArrayCounting, maxRange)
                CountingFinal = time.time()
                CountingTotal = (CountingFinal - CountingInitial) / 10
                CountingTimes.append(CountingTotal)
            else:
                CountingTimes = []

            if self.checkBoxHeap.isChecked():
                HeapInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayHeap = self.UnsortedArrayComparison.copy()
                    QApplication.processEvents()
                    heapsort.heapSort(UnsortedArrayHeap)
                HeapFinal = time.time()
                HeapTotal = (HeapFinal - HeapInitial) / 10
                HeapTimes.append(HeapTotal)
            else:
                HeapTimes = []

            if self.checkBoxMerge.isChecked():
                MergeInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayMerge = self.UnsortedArrayComparison.copy()
                    QApplication.processEvents()
                    Merge.merge(UnsortedArrayMerge)
                MergeFinal = time.time()
                MergeTotal = (MergeFinal - MergeInitial) / 10
                MergeTimes.append(MergeTotal)
            else:
                MergeTimes = []

            if self.checkBoxQuick.isChecked():
                QuickInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayQuick = self.UnsortedArrayComparison.copy()
                    QApplication.processEvents()
                    quicksort.QuickSort(UnsortedArrayQuick, 0,
                                        self.StepsCounter - 1)
                QuickFinal = time.time()
                QuickTotal = (QuickFinal - QuickInitial) / 10
                QuickTimes.append(QuickTotal)
            else:
                QuickTimes = []

            if self.checkBoxRandQuick.isChecked():
                RandQuickInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayQuick = self.UnsortedArrayComparison.copy()
                    QApplication.processEvents()
                    quicksort.QuickSort(UnsortedArrayQuick, 0,
                                        self.StepsCounter - 1)
                RandQuickFinal = time.time()
                RandQuickTotal = (RandQuickFinal - RandQuickInitial) / 10
                RandQuickTimes.append(RandQuickTotal)
            else:
                RandQuickTimes = []

            if self.checkBoxRadix.isChecked():
                radixInitial = time.time()
                for i in range(0, 10):
                    UnsortedArrayRadix = self.UnsortedArrayComparison.copy()
                    QApplication.processEvents()
                    radixSort.radixSort(UnsortedArrayRadix)
                radixFinal = time.time()
                radixTotal = (radixFinal - radixInitial) / 10
                RadixTimes.append(radixTotal)
            else:
                RadixTimes = []

            self.StepsCounter = self.StepsCounter + int(
                self.spinBoxLengthSteps.value())
            x.append(self.StepsCounter)

        self.graphicsView.setTitle("Time Comparison Graph")
        self.graphicsView.setLabel('left', 'Time', units='s')
        self.graphicsView.setLabel('bottom', 'Array Length')

        if self.checkBoxInsertion.isChecked():
            self.graphicsView.plot(InsertionTimes,
                                   name="Insertion",
                                   pen=pyqtgraph.mkPen('w', width=1.5))
        if self.checkBoxMerge.isChecked():
            self.graphicsView.plot(MergeTimes,
                                   name="Merge",
                                   pen=pyqtgraph.mkPen('r', width=1.5))
        if self.checkBoxQuick.isChecked():
            self.graphicsView.plot(QuickTimes,
                                   name="Quick",
                                   pen=pyqtgraph.mkPen('g', width=1.5))
        if self.checkBoxRandQuick.isChecked():
            self.graphicsView.plot(RandQuickTimes,
                                   name="RandQuick",
                                   pen=pyqtgraph.mkPen('m', width=1.5))
        if self.checkBoxHeap.isChecked():
            self.graphicsView.plot(HeapTimes,
                                   name="Heap",
                                   pen=pyqtgraph.mkPen('y', width=1.5))
        if self.checkBoxBubble.isChecked():
            self.graphicsView.plot(BubbleTimes,
                                   name="Bubble",
                                   pen=pyqtgraph.mkPen('k', width=1.5))
        if self.checkBoxCounting.isChecked():
            self.graphicsView.plot(CountingTimes,
                                   name='Counting',
                                   pen=pyqtgraph.mkPen('g', width=1.5),
                                   style=Qt.DashLine)
        if self.checkBoxRadix.isChecked():
            self.graphicsView.plot(RadixTimes,
                                   name='Radix',
                                   pen=pyqtgraph.mkPen('r',
                                                       width=1.5,
                                                       style=Qt.DotLine))