Skip to content

Commit 87234d1

Browse files
committed
fix algorithms and refactor player UI
1 parent 7942d50 commit 87234d1

33 files changed

+510
-809
lines changed

src/App.jsx

+8-25
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
import { useCallback } from 'react';
2-
import Array from './components/Array/Array';
2+
33
import { usePlayer, useHistory, useAlgorithm } from './hooks';
4-
import { ResetIcon } from './components/icon/ResetIcon';
5-
import PauseIcon from './components/icon/PauseIcon';
6-
import PlayIcon from './components/icon/PlayIcon';
7-
import Player from './components/Player/Player';
4+
85
import { disableScroll } from './utils/disableScroll';
96

7+
import BarContainer from './components/BarContainer/BarContainer';
8+
import Player from './components/Player/Player';
9+
1010
const App = () => {
1111
const history = useHistory();
1212

1313
const player = usePlayer();
14-
// console.log('history:', history, 'player:', player);
1514
const {
1615
array,
1716
algorithmState,
@@ -37,22 +36,6 @@ const App = () => {
3736
}
3837
}, [player, runAlgorithm, resetAll, algorithmState]);
3938

40-
// refactor - move logic to Player
41-
const getButtonText = useCallback(() => {
42-
if (algorithmState === 'notStarted') {
43-
return <PlayIcon />;
44-
} else if (algorithmState === 'finished') {
45-
return <ResetIcon />;
46-
}
47-
48-
if (player.playerState === 'play') {
49-
return <PauseIcon />;
50-
}
51-
{
52-
return <PlayIcon />;
53-
}
54-
}, [algorithmState, player]);
55-
5639
// refactor - use CSS instead of JS logic (hint: media queries, dvh units)
5740
const barWidth = window.screen.width / array.length;
5841
disableScroll();
@@ -62,19 +45,19 @@ const App = () => {
6245
return (
6346
<section>
6447
<span className="title-algorithm">{selectedAlgorithm} Sort</span>
65-
<Array {...step} barWidth={barWidth} />
48+
<BarContainer {...step} barWidth={barWidth} />
6649
<Player
6750
selectAlgorithm={selectAlgorithm}
6851
goToNextStep={() => player.setPlayerState('forward')}
69-
getButtonText={getButtonText}
7052
goToPreviousStep={() => player.setPlayerState('backward')}
7153
speed={player.speed}
7254
resetAlgorithm={resetAll}
7355
setSpeed={(speed) => player.setSpeed(speed)}
7456
handleAlgorithmRun={handleAlgorithmRun}
7557
selectedAlgorithm={selectedAlgorithm}
76-
evalState={player.evalState}
7758
currentTrack={history.currentTrack}
59+
algorithmState={algorithmState}
60+
player={player}
7861
/>
7962
</section>
8063
);

src/algorithms/index.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { bubbleSort } from './bubbleSort';
2-
// import { insertionSort } from './insertionSort';
3-
// import { quickSort } from './quickSort';
4-
// import { selectionSort } from './selectionSort';
2+
import { insertionSort } from './insertionSort';
3+
import { selectionSort } from './selectionSort';
4+
import { quickSort } from './quickSort';
55

66
export const algorithms = {
7-
bubble: bubbleSort,
8-
// selection: selectionSort,
9-
// quick: quickSort,
10-
// insertion: insertionSort
11-
}
7+
bubble: bubbleSort,
8+
insertion: insertionSort,
9+
selection: selectionSort,
10+
quick: quickSort,
11+
};

src/algorithms/insertionSort.js

+14-30
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,24 @@
1-
import { sleep } from '../../utils/sleep';
2-
import { pause } from '../../utils/pause';
1+
import { pause } from '../utils/pause';
32

4-
export const insertionSort = async (
3+
export const insertionSort = async ({
54
array,
65
updateArray,
7-
setActiveIndex,
8-
setCompareIndex,
9-
evalStateRef,
10-
speedRef,
11-
playerStateRef,
12-
trackRef,
13-
) => {
14-
let arr = [...array];
15-
for (let i = 1; i < arr.length; i++) {
16-
const temp = arr[i];
6+
updateTracking,
7+
history,
8+
player,
9+
}) => {
10+
for (let i = 1; i < array.length; i++) {
11+
const temp = array[i];
1712
let j = i - 1;
1813

19-
await pause(evalStateRef, playerStateRef, trackRef, speedRef);
20-
if (evalStateRef.current === 'notStarted') {
21-
throw new Error('cancelSort');
22-
}
23-
setActiveIndex(i);
24-
updateArray([...arr], i, null, null);
14+
await pause({ history, player });
15+
updateTracking({ activeIndex: i, compareIndex: j });
2516

26-
await sleep(100, speed.current);
27-
while (j >= 0 && arr[j] > temp) {
28-
arr[j + 1] = arr[j];
17+
while (j >= 0 && array[j] > temp) {
18+
array[j + 1] = array[j];
2919
j--;
30-
setCompareIndex(j);
31-
updateArray([...arr], null, j, null);
32-
await sleep(100, speedRef.current);
3320
}
34-
arr[j + 1] = temp;
35-
await sleep(100, speedRef.current);
36-
updateArray([...arr], temp, null, null);
21+
array[j + 1] = temp;
22+
updateArray(array);
3723
}
38-
setActiveIndex(null);
39-
setCompareIndex(null);
4024
};

src/algorithms/quickSort.js

+45-102
Original file line numberDiff line numberDiff line change
@@ -1,150 +1,93 @@
1-
import { sleep } from '../../utils/sleep';
2-
import { pause } from '../../utils/pause';
1+
import { pause } from '../utils/pause';
32

4-
const partition = async (
3+
const partition = async ({
54
arr,
65
start,
76
end,
8-
setActiveIndex,
9-
setCompareIndex,
10-
setPivotIndex,
11-
evalStateRef,
12-
speedRef,
13-
playerStateRef,
14-
trackRef,
157
updateArray,
16-
) => {
8+
updateTracking,
9+
history,
10+
player,
11+
}) => {
1712
const pivotElement = arr[end];
1813
let partitionIndex = start;
19-
// setActiveIndex(end);
20-
// setPivotIndex(partitionIndex);
21-
// await sleep(100, speedRef.current);
2214

2315
for (let i = start; i < end; i++) {
24-
await pause(evalStateRef, playerStateRef, trackRef, speedRef);
25-
if (evalStateRef === 'notStarted') {
26-
throw new Error('cancelSort');
27-
}
28-
setCompareIndex(i);
29-
// await sleep(100, speedRef.current);
30-
console.log('arrrr,', arr);
16+
await pause({ history, player });
17+
updateTracking({ activeIndex: partitionIndex, pivotIndex: end });
3118

3219
if (arr[i] < pivotElement) {
3320
[arr[i], arr[partitionIndex]] = [arr[partitionIndex], arr[i]];
3421

35-
updateArray([...arr], null, null, null);
36-
await sleep(20, speedRef.current);
3722
partitionIndex++;
3823
}
3924
}
4025

4126
[arr[partitionIndex], arr[end]] = [arr[end], arr[partitionIndex]];
42-
updateArray([...arr], null, null, null);
43-
await sleep(50, speedRef.current);
27+
updateArray(arr);
4428

4529
return partitionIndex;
4630
};
4731

48-
const quickSortRecursive = async (
32+
const quickSortRecursive = async ({
4933
arr,
5034
start,
5135
end,
52-
setActiveIndex,
53-
setCompareIndex,
54-
setPivotIndex,
55-
evalStateRef,
56-
speedRef,
57-
playerStateRef,
58-
trackRef,
5936
updateArray,
60-
) => {
37+
updateTracking,
38+
history,
39+
player,
40+
}) => {
6141
if (start >= end) {
6242
return;
6343
}
64-
await pause(evalStateRef, playerStateRef, speedRef, trackRef);
65-
if (evalStateRef === 'notStarted') {
66-
throw new Error('cancelSort');
67-
}
44+
await pause({ history, player });
6845

69-
const index = await partition(
46+
const index = await partition({
7047
arr,
7148
start,
7249
end,
73-
setActiveIndex,
74-
setCompareIndex,
75-
setPivotIndex,
76-
evalStateRef,
77-
speedRef,
78-
playerStateRef,
79-
trackRef,
8050
updateArray,
81-
);
51+
updateTracking,
52+
history,
53+
player,
54+
});
8255

83-
await quickSortRecursive(
56+
await quickSortRecursive({
8457
arr,
8558
start,
86-
index - 1,
87-
setActiveIndex,
88-
setCompareIndex,
89-
setPivotIndex,
90-
evalStateRef,
91-
speedRef,
92-
playerStateRef,
93-
trackRef,
59+
end: index - 1,
9460
updateArray,
95-
);
96-
await quickSortRecursive(
61+
updateTracking,
62+
history,
63+
player,
64+
});
65+
await quickSortRecursive({
9766
arr,
98-
index + 1,
67+
start: index + 1,
9968
end,
100-
setActiveIndex,
101-
setCompareIndex,
102-
setPivotIndex,
103-
evalStateRef,
104-
speedRef,
105-
playerStateRef,
106-
trackRef,
10769
updateArray,
108-
);
70+
updateTracking,
71+
history,
72+
player,
73+
});
10974
};
11075

111-
// array,
112-
// updateArray,
113-
// setActiveIndex,
114-
// setCompareIndex,
115-
// evalStateRef,
116-
// speedStateRef,
117-
// playerStateRef,
118-
// trackRef,
119-
120-
export const quickSort = async (
76+
export const quickSort = async ({
12177
array,
12278
updateArray,
123-
setActiveIndex,
124-
setCompareIndex,
125-
setPivotIndex,
126-
evalStateRef,
127-
speedRef,
128-
playerStateRef,
129-
trackRef,
130-
) => {
131-
await quickSortRecursive(
132-
array,
133-
0,
134-
array.length - 1,
135-
setActiveIndex,
136-
setCompareIndex,
137-
setPivotIndex,
138-
evalStateRef,
139-
speedRef,
140-
playerStateRef,
141-
trackRef,
79+
updateTracking,
80+
history,
81+
player,
82+
}) => {
83+
await quickSortRecursive({
84+
arr: array,
85+
start: 0,
86+
end: array.length - 1,
14287
updateArray,
143-
);
144-
145-
setActiveIndex(null);
146-
setCompareIndex(null);
147-
setPivotIndex(null);
148-
88+
updateTracking,
89+
history,
90+
player,
91+
});
14992
return array;
15093
};

src/algorithms/selectionSort.js

+15-32
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,26 @@
1-
import { sleep } from '../../utils/sleep';
2-
import { pause } from '../../utils/pause';
1+
import { pause } from '../utils/pause';
32

4-
export const selectionSort = async (
3+
export const selectionSort = async ({
54
array,
65
updateArray,
7-
setActiveIndex,
8-
setCompareIndex,
9-
evalStateRef,
10-
speedRef,
11-
playerStateRef,
12-
trackRef,
13-
) => {
14-
let arr = [...array];
15-
for (let i = 0; i < arr.length; i++) {
6+
updateTracking,
7+
history,
8+
player,
9+
}) => {
10+
for (let i = 0; i < array.length; i++) {
1611
let indexMin = i;
17-
setActiveIndex(indexMin);
18-
updateArray([...arr], indexMin, null, null);
19-
for (let j = i + 1; j < arr.length; j++) {
20-
setCompareIndex(j);
21-
updateArray([...arr], indexMin, j, null);
22-
await pause(evalStateRef, playerStateRef, trackRef, speedRef);
23-
if (evalStateRef.current === 'notStarted') {
24-
throw new Error('cancelSort');
25-
}
26-
27-
if (arr[j] < arr[indexMin]) {
12+
for (let j = i + 1; j < array.length; j++) {
13+
await pause({ history, player });
14+
updateTracking({ activeIndex: indexMin, compareIndex: j });
15+
if (array[j] < array[indexMin]) {
2816
indexMin = j;
29-
setActiveIndex(indexMin);
30-
updateArray([...arr], indexMin, null, null);
3117
}
3218
}
3319

34-
let tmp = arr[i];
35-
arr[i] = arr[indexMin];
36-
arr[indexMin] = tmp;
37-
38-
updateArray([...arr], indexMin, null, null);
20+
let tmp = array[i];
21+
array[i] = array[indexMin];
22+
array[indexMin] = tmp;
3923

40-
await sleep(100, speedRef.current);
41-
setCompareIndex(null);
24+
updateArray(array);
4225
}
4326
};

0 commit comments

Comments
 (0)