|
2 | 2 | class InsertionSort extends Sort {
|
3 | 3 | constructor() {
|
4 | 4 | super();
|
5 |
| - this.index = 1; |
6 | 5 | this.done = 0;
|
7 | 6 | this.comparing = 0;
|
| 7 | + this.low = 0; |
| 8 | + this.high = 0; |
8 | 9 | }
|
9 | 10 |
|
10 | 11 | step() {
|
11 |
| - if (this.done === arrayToSort.length - 1) { |
| 12 | + if (this.done === arrayToSort.length) { |
12 | 13 | return true;
|
13 | 14 | }
|
14 |
| - if (getValue(arrayToSort, this.index, false) > getValue(arrayToSort, this.comparing)) { |
15 |
| - move(this.index, this.comparing + 1, arrayToSort); |
16 |
| - this.done++; |
17 |
| - this.index = this.done + 1; |
18 |
| - this.comparing = this.done; |
19 |
| - } else if (this.comparing === 0) { |
20 |
| - move(this.index, 0, arrayToSort); |
| 15 | + |
| 16 | + if (this.low >= this.high) { |
| 17 | + move(this.done, this.low, arrayToSort); |
21 | 18 | this.done++;
|
22 |
| - this.index = this.done + 1; |
23 |
| - this.comparing = this.done; |
| 19 | + this.low = 0; |
| 20 | + this.high = this.done; |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
| 24 | + const mid = Math.floor((this.low + this.high) / 2); |
| 25 | + let value = getValue(arrayToSort, mid); |
| 26 | + let compare = getValue(arrayToSort, this.done, false); |
| 27 | + if (compare < value) { |
| 28 | + this.high = mid; |
| 29 | + } else if (compare > value) { |
| 30 | + this.low = mid + 1; |
24 | 31 | } else {
|
25 |
| - this.comparing--; |
| 32 | + this.low = mid; |
| 33 | + this.high = mid; |
26 | 34 | }
|
| 35 | + return false; |
27 | 36 | }
|
28 | 37 |
|
29 | 38 | draw(cnt) {
|
30 | 39 | cnt.fillStyle = "#000";
|
31 | 40 | cnt.fillRect(0, 0, cnt.canvas.width, cnt.canvas.height);
|
32 | 41 | const sizeOfBlock = cnt.canvas.width / arrayToSort.length;
|
33 | 42 | arrayToSort.forEach((element, index) => {
|
34 |
| - if (index === this.index) { |
| 43 | + cnt.fillStyle = "#c6d618"; |
| 44 | + cnt.fillRect((this.high+1) * sizeOfBlock, 0, 1, cnt.canvas.height); |
| 45 | + cnt.fillStyle = "#d6a318"; |
| 46 | + cnt.fillRect(this.low * sizeOfBlock, 0, 1, cnt.canvas.height); |
| 47 | + |
| 48 | + if (index === this.done) { |
35 | 49 | cnt.fillStyle = "#0e66c9";
|
36 |
| - } else if (index === this.comparing) { |
| 50 | + }else if(index === this.low && index === this.high){ |
| 51 | + cnt.fillStyle = "#c54816"; |
| 52 | + } else if (index === this.low) { |
37 | 53 | cnt.fillStyle = "#c6d618";
|
38 |
| - } else if (index <= this.done) { |
| 54 | + } else if (index === this.high) { |
| 55 | + cnt.fillStyle = "#d6a318"; |
| 56 | + } else if (index < this.done) { |
39 | 57 | cnt.fillStyle = "#35d618";
|
40 | 58 | } else {
|
41 | 59 | cnt.fillStyle = getColorBasedOnValue(element);
|
42 | 60 | }
|
43 |
| - cnt.fillRect(index * sizeOfBlock + sizeOfBlock * 0.025, 0, sizeOfBlock * 0.99, cnt.canvas.height * element); |
| 61 | + cnt.fillRect(index * sizeOfBlock + sizeOfBlock * 0.05, 0, sizeOfBlock * 0.99, cnt.canvas.height * element); |
44 | 62 | });
|
45 | 63 | }
|
46 | 64 | }
|
0 commit comments