Skip to content

Commit 4cc652d

Browse files
committed
debug bubble sort
1 parent 0176596 commit 4cc652d

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import Component from '@ember/component';
2+
import { schedule } from '@ember/runloop';
3+
4+
export default Component.extend({
5+
step: false,
6+
7+
createLines() {
8+
let { a } = this;
9+
let container = document.querySelector('.bubble-sort');
10+
for (let i = 0; i < a.length; i++) {
11+
let line = document.createElement('div');
12+
line.classList = ['line'];
13+
line.id = `line-${i}`;
14+
line.style.width = `${a[i] * 10}%`;
15+
container.appendChild(line);
16+
}
17+
schedule('afterRender', this, 'bubbleSort');
18+
},
19+
20+
bubbleSort: async function() {
21+
let { a: lines } = this;
22+
23+
for (let i = 0; i < lines.length; i++) {
24+
for (let j = i; j < lines.length; j++) {
25+
if (lines[i] > lines[j]) {
26+
let temp = lines[j];
27+
lines[j] = lines[i];
28+
lines[i] = temp;
29+
this.updateLines(lines);
30+
31+
await new Promise(resolve => {
32+
let checkStep = () => {
33+
setTimeout(() => {
34+
if (this.step) {
35+
resolve();
36+
} else {
37+
checkStep();
38+
}
39+
}, 400);
40+
};
41+
42+
checkStep();
43+
});
44+
}
45+
}
46+
}
47+
},
48+
49+
syncBubbleSort() { // without async/await
50+
let { a: lines } = this;
51+
let promise = Promise.resolve();
52+
for (let i = 0; i < lines.length; i++) {
53+
for (let j = i; j < lines.length; j++) {
54+
55+
let doSwap = (lines, i, j) => {
56+
promise = promise.then(() => {
57+
return new Promise(resolve => {
58+
setTimeout(() => {
59+
if (lines[i] > lines[j]) {
60+
let temp = lines[j];
61+
lines[j] = lines[i];
62+
lines[i] = temp;
63+
this.updateLines(lines);
64+
}
65+
resolve();
66+
}, 100);
67+
});
68+
});
69+
};
70+
71+
doSwap(lines, i, j);
72+
}
73+
}
74+
},
75+
76+
updateLines() {
77+
let { a } = this;
78+
a.forEach((value, index) => {
79+
let line = document.querySelector(`#line-${index}`);
80+
line.style.width = `${value * 10}%`;
81+
});
82+
},
83+
84+
didInsertElement() {
85+
this._super(...arguments);
86+
87+
this.a = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
88+
89+
schedule('afterRender', this, 'createLines');
90+
},
91+
92+
actions: {
93+
step() {
94+
this.toggleProperty('step');
95+
}
96+
}
97+
});

ember/algos/app/styles/app.scss

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,13 @@
8585
border: 1px solid;
8686
}
8787
}
88+
89+
.bubble-sort {
90+
width: 400px;
91+
92+
.line {
93+
background-color: red;
94+
height: 10px;
95+
margin-bottom: 10px;
96+
}
97+
}

ember/algos/app/templates/application.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
<BubbleSort />
2+
13
<MinHeap />
24

35
<TicTacToe />
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="bubble-sort">
2+
</div>
3+
4+
<button {{action "step"}}>{{if this.step "Pause" "Start"}}</button>

0 commit comments

Comments
 (0)