forked from hofmannol/AlgoDatSoSe25
Compare commits
2 Commits
2bcd77f9ec
...
38c099a94e
Author | SHA1 | Date | |
---|---|---|---|
38c099a94e | |||
2af96a1b4e |
@ -107,7 +107,7 @@ def partitionIterative(arr : MemoryArray, l : Literal, h : Literal, mode=0):
|
|||||||
|
|
||||||
# Carefull that we do not use a reference. I suppose python would return one here if we just assign without value>Literal cast.
|
# Carefull that we do not use a reference. I suppose python would return one here if we just assign without value>Literal cast.
|
||||||
# At least this helped fix weird issue
|
# At least this helped fix weird issue
|
||||||
pivotValue : Literal = Literal(arr[h].value)
|
pivotValue : Literal = arr[h]
|
||||||
i : MemoryCell = MemoryCell(l.pred())
|
i : MemoryCell = MemoryCell(l.pred())
|
||||||
|
|
||||||
for j in mrange(l, h):
|
for j in mrange(l, h):
|
||||||
@ -115,7 +115,7 @@ def partitionIterative(arr : MemoryArray, l : Literal, h : Literal, mode=0):
|
|||||||
i += Literal(1) # increment index of smaller element
|
i += Literal(1) # increment index of smaller element
|
||||||
swap(arr, int(i), int(j))
|
swap(arr, int(i), int(j))
|
||||||
|
|
||||||
swap(arr, int(i.succ()), int(h))
|
swap(arr, i.succ().value, h.value)
|
||||||
return i.succ()
|
return i.succ()
|
||||||
|
|
||||||
def LEGACY_quickSort(z: MemoryArray, l: Literal = Literal(0), r: Literal = Literal(-1), mode=0):
|
def LEGACY_quickSort(z: MemoryArray, l: Literal = Literal(0), r: Literal = Literal(-1), mode=0):
|
||||||
@ -185,6 +185,6 @@ if __name__ == '__main__':
|
|||||||
print(filename)
|
print(filename)
|
||||||
toSort = MemoryArray.create_array_from_file(filename)
|
toSort = MemoryArray.create_array_from_file(filename)
|
||||||
timeMS(quickSortIterative, toSort, Literal(0), toSort.length().pred(), mode=1)
|
timeMS(quickSortIterative, toSort, Literal(0), toSort.length().pred(), mode=1)
|
||||||
# print(toSort)
|
print(toSort)
|
||||||
|
|
||||||
print("Kann durch die Modifikation eine besser Laufzeit als nlog(n) erreicht werden? Nein! nlog(n) ist das Minimum. Durch die Änderung kann aber der Worst-Case fall von n^2 für z.B. bereits vorsortierte Arrays oder Arrays mit vielen Duplikaten vermieden werden.")
|
print("Kann durch die Modifikation eine besser Laufzeit als nlog(n) erreicht werden? Nein! nlog(n) ist das Minimum. Durch die Änderung kann aber der Worst-Case fall von n^2 für z.B. bereits vorsortierte Arrays oder Arrays mit vielen Duplikaten vermieden werden.")
|
||||||
|
@ -89,11 +89,12 @@ class PriorityQueue:
|
|||||||
|
|
||||||
i = self.size
|
i = self.size
|
||||||
self.heap[i].set([entry])
|
self.heap[i].set([entry])
|
||||||
self.size += Literal(1)
|
|
||||||
|
|
||||||
while i > Literal(0) and self.heap[self.parent(i)].value[0] < self.heap[i].value[0]:
|
while i > Literal(0) and self.heap[self.parent(i)].value[0] < self.heap[i].value[0]:
|
||||||
self.swap(i, self.parent(i))
|
self.swap(i, self.parent(i))
|
||||||
i = self.parent(i)
|
i = self.parent(i)
|
||||||
|
|
||||||
|
self.size += Literal(1)
|
||||||
|
|
||||||
def pop(self):
|
def pop(self):
|
||||||
if self.isEmpty():
|
if self.isEmpty():
|
||||||
@ -119,6 +120,36 @@ class PriorityQueue:
|
|||||||
def __len__(self):
|
def __len__(self):
|
||||||
return self.size
|
return self.size
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
entries = []
|
||||||
|
for i in mrange(self.size.value):
|
||||||
|
entry = self.heap[i].value[0]
|
||||||
|
if entry.getItem() != MAX_VALUE:
|
||||||
|
entries.append(str(entry))
|
||||||
|
return "[" + ", ".join(entries) + "]"
|
||||||
|
|
||||||
|
def testQueueRandom(number: int):
|
||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
|
pq = PriorityQueue(Literal(101))
|
||||||
|
|
||||||
|
entries = []
|
||||||
|
for _ in range(number):
|
||||||
|
value = ''.join(random.choices(string.ascii_uppercase + string.digits, k=3))
|
||||||
|
priority = random.randint(1, 100)
|
||||||
|
entry = HeapEntry(value, priority)
|
||||||
|
entries.append(entry)
|
||||||
|
pq.insert(entry)
|
||||||
|
|
||||||
|
print(pq)
|
||||||
|
for entry in entries:
|
||||||
|
print(entry)
|
||||||
|
|
||||||
|
while not pq.isEmpty():
|
||||||
|
print(pq.pop())
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# Proof of Concept
|
# Proof of Concept
|
||||||
@ -153,6 +184,11 @@ if __name__ == '__main__':
|
|||||||
pq.insert(HeapEntry("A", 1))
|
pq.insert(HeapEntry("A", 1))
|
||||||
pq.insert(HeapEntry("C", 3))
|
pq.insert(HeapEntry("C", 3))
|
||||||
pq.insert(HeapEntry("B", 2))
|
pq.insert(HeapEntry("B", 2))
|
||||||
print(pq.pop())
|
pq.insert(HeapEntry(42, 4))
|
||||||
print(pq.pop())
|
pq.insert(HeapEntry(42, 1))
|
||||||
print(pq.pop())
|
pq.insert(HeapEntry("C", 2))
|
||||||
|
print(pq)
|
||||||
|
while not pq.isEmpty():
|
||||||
|
print(pq.pop())
|
||||||
|
|
||||||
|
testQueueRandom(100)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user