Skip to content

Commit

Permalink
Add debug info to bubblesort and fix bug
Browse files Browse the repository at this point in the history
Because of the new debugging I found a bug in the optimization, which
barely halted irrelevant swappers. This is now fixed.
  • Loading branch information
Bas van den Heuvel committed Feb 2, 2018
1 parent a4b4f69 commit b813bc6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions bubblesort_opt2.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ def __init__(self, ctl, ctx, a):
self.max_changed = 0
self.changed = False

self.info = [
('a:%s', 'a'),
]

self.init_state = self.setup

def __repr__(self):
Expand Down Expand Up @@ -60,7 +64,7 @@ def check_done(self):
return self.halt

def setup_optimize(self):
self.i = len(self.swappers) - self.max_changed - 1
self.i = len(self.swappers) - self.max_changed + 1
return self.optimize

def optimize(self):
Expand All @@ -80,6 +84,10 @@ def __init__(self, ctl, ctx, a, i):
self.a = a
self.i = i

self.info = [
('i:%d', 'i'),
]

self.init_state = self.setup

def __repr__(self):
Expand All @@ -102,7 +110,7 @@ def swap(self):


if __name__ == '__main__':
ctl = MachineControl(debug=False)
ctl = MachineControl(debug=True)
ctl.run(BubbleSort, [5, 3, 1, 8])
ctl.run(BubbleSort, [5, 4, 3, 2, 3, 4, 5, 67, 1, 1, 6, 9])
ctl.run(BubbleSort, list("test with string"))
Expand Down

0 comments on commit b813bc6

Please sign in to comment.