import logging logger = logging.getLogger(__name__) logging.basicConfig(level=logging.DEBUG) from utils.memory_array import MemoryArray from utils.memory_cell import MemoryCell from utils.literal import Literal from utils.constants import MIN_VALUE from utils.memory_manager import MemoryManager from utils.memory_range import mrange def example(): initial = [6, 5, 3, 8, 1, 7, 2, 4] # initial = [-6, -5, -3, -8, 1, 7, 2, 4] toSort = MemoryArray(initial) # init_from_size not accessible? quickSort(toSort, Literal(0), mode=0) logger.debug(f"sorted {toSort} vs initial {initial}") assert all(toSort[Literal(i)] == Literal(i+1) for i in range(len(initial))), "Array not sorted correctly" # analyze_complexity(quickSort, [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]) def getPivot(z: MemoryArray, l: Literal, r: Literal, mode) -> Literal: if mode == 0: return r else: mid = MemoryCell(l) + MemoryCell(MemoryCell(r) - l) // Literal(2) # Return median of left, middle, and right elements if ((z[l] <= z[mid] and z[mid] <= z[r]) or (z[r] <= z[mid] and z[mid] <= z[l])): return mid elif ((z[mid] <= z[l] and z[l] <= z[r]) or (z[r] <= z[l] and z[l] <= z[mid])): return l else: return r def swap(z: MemoryArray, i: int, j: int): tmp = z[Literal(i)].value z[Literal(i)] = z[Literal(j)] z[Literal(j)].set(tmp) def quickSort(z: MemoryArray, l: Literal = Literal(0), r: Literal = Literal(-1), mode=0): if r == Literal(-1): r = z.length().pred(); if l < r: q = partition(z, l, r, mode) quickSort(z, l, q.pred()) quickSort(z, q.succ(), r) def partition(z: MemoryArray, l: Literal, r: Literal, mode): # Get pivot pivot_idx = getPivot(z, l, r, mode) # If pivot is not already at the right end, swap it there if pivot_idx != r: swap(z, int(pivot_idx), int(r)) # with MemoryCell(z[r]) as pivot, MemoryCell(l) as i, MemoryCell(r.pred()) as j: pivot = MemoryCell(z[r]) i = MemoryCell(l) j = MemoryCell(r.pred()) while i < j: while z[i] < pivot: i.set(i.succ()) while j > l and z[j] >= pivot: j.set(j.pred()) if i < j: swap(z, int(i), int(j)) i.set(i.succ()) j.set(j.pred()) if i == j and z[i] < pivot: i.set(i.succ()) if z[i] != pivot: swap(z, int(i), int(r)) return Literal(i) def analyze_complexity(fn, sizes): """ Analysiert die Komplexität einer maximalen Teilfolgenfunktion. :param max_sequence_func: Die Funktion, die analysiert wird. :param sizes: Eine Liste von Eingabegrößen für die Analyse. """ for size in sizes: MemoryManager.purge() # Speicher zurücksetzen random_array = MemoryArray.create_random_array(size, -100, 100) other_array = MemoryArray([-1] * size) fn(random_array, other_array) MemoryManager.save_stats(size) MemoryManager.plot_stats(["cells", "adds", "compares"]) if __name__ == '__main__': # For debug, assert if working and complexity-analysis example() toSort = MemoryArray.create_array_from_file("data/seq0.txt") print(toSort) quickSort(toSort) print(toSort) for filename in ["data/seq0.txt", "data/seq1.txt", "data/seq2.txt", "data/seq3.txt"]: print(filename) toSort = MemoryArray.create_array_from_file(filename) quickSort(toSort,Literal(0), Literal(-1), mode=0)