Пример #1
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     self.__quickSortIterative(array,
                               0,
                               array.size - 1,
                               partition_alg=self.__median_of_three,
                               visualization=visualization)
Пример #2
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     for i in range(array.size):
         min_index = i
         for j in range(i + 1, array.size):
             self.count += 1
             if array[min_index] > array[j]:
                 min_index = j
         array[i], array[min_index] = array[min_index], array[i]
         if visualization:
             self.hist_array = np.vstack([self.hist_array, array])
     if visualization:
         self.hist_array = np.vstack([self.hist_array, array])
Пример #3
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     for i in range(1, array.size):
         ele = array[i]
         ins_pos = i - 1
         while ins_pos >= 0 and ele < array[ins_pos]:
             self.count += 1
             array[ins_pos + 1] = array[ins_pos]
             ins_pos -= 1
             if visualization:
                 self.hist_array = np.vstack([self.hist_array, array])
         array[ins_pos + 1] = ele
     if visualization:
         self.hist_array = np.vstack([self.hist_array, array])
Пример #4
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     for i in range(0, array.size):
         exch = False
         for j in range(0, array.size - i - 1):
             self.count += 1
             if array[j] > array[j + 1]:
                 array[j], array[j + 1] = array[j + 1], array[j]
                 exch = True
             if visualization:
                 self.hist_array = np.vstack([self.hist_array, array])
         if not exch:
             break
     if visualization:
         self.hist_array = np.vstack([self.hist_array, array])
Пример #5
0
    def sort(self, array, visualization=False):
        SortingAlgorithm.sort(self, array, visualization)
        # convert aList to heap
        length = array.size - 1
        leastParent = int(length / 2)
        for i in range(leastParent, -1, -1):
            self.moveDown(array, i, length, visualization)

        # flatten heap into sorted array
        for i in range(length, 0, -1):
            if array[0] > array[i]:
                array[0], array[i] = array[i], array[0]
                self.moveDown(array, 0, i - 1, visualization)
        if visualization:
            self.hist_array = np.vstack([self.hist_array, array])
Пример #6
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     unit = 1
     while unit <= array.size:
         for h in range(0, array.size, unit * 2):
             l, r = h, min(array.size, h + 2 * unit)
             mid = h + unit
             # merge xs[h:h + 2 * unit]
             p, q = l, mid
             while p < mid and q < r:
                 self.count += 1
                 if array[p] < array[q]:
                     p += 1
                 else:
                     tmp = array[q]
                     array[p + 1:q + 1] = array[p:q]
                     array[p] = tmp
                     p, mid, q = p + 1, mid + 1, q + 1
                 if visualization:
                     self.hist_array = np.vstack([self.hist_array, array])
         unit *= 2
     if visualization:
         self.hist_array = np.vstack([self.hist_array, array])
Пример #7
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     self.visualization = visualization
     self.split(array, 0, array.size - 1)
Пример #8
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Merge Sort")
Пример #9
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Bubble Sort")
Пример #10
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Selection Sort")
Пример #11
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Insertion Sort")
Пример #12
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Quicksort")
Пример #13
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Heapsort")
Пример #14
0
 def sort(self, array, visualization=False):
     SortingAlgorithm.sort(self, array, visualization)
     self.visualization = visualization
     n = len(array)
     self.quickSort(array, 0, n - 1)
Пример #15
0
 def __init__(self):
     SortingAlgorithm.__init__(self, "Quick Sort Recursive")