Skip to content

Commit 7cf837a

Browse files
committed
Merge branch 'main' into functional_board
2 parents 1707401 + c41bb94 commit 7cf837a

6 files changed

Lines changed: 98 additions & 5 deletions

File tree

‎src/components/Animation/Animation.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ class Animation {
5151
}
5252

5353

54+
55+
static changeHeight(i, value, oldIndex) {
56+
let unSortedBars = document.querySelectorAll('.array-bars');
57+
unSortedBars[i].style.height = value+'px';
58+
unSortedBars[i].id = value; // SG 07/11/2022 09:27 temp1
59+
unSortedBars[i].setAttribute("data", unSortedBars[oldIndex].getAttribute("data"));
60+
}
61+
62+
5463
static async getAnimation(delay) {
5564
if (!Animation.stop) {
5665
await new Promise((resolve) =>

‎src/components/Canvas/AlgorithmSelection.js‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RadixSortLSD } from '../Strategy/Algorithms/RadixSortLSD';
55
import { QuickSort } from '../Strategy/Algorithms/QuickSort.js'
66
import SelectionSort from '../Strategy/Algorithms/SelectionSort';
77
import { HeapSort } from '../Strategy/Algorithms/Heapsort';
8+
import { ShellSort } from '../Strategy/Algorithms/ShellSort';
89
function AlgorithmSelection({strategy, options}) {
910

1011
const [currentAlgo, setCurrentAlgo] = useState(options.state.algorithm);
@@ -14,7 +15,7 @@ function AlgorithmSelection({strategy, options}) {
1415
const Selection = new SelectionSort();
1516
const Quick = new QuickSort();
1617
const Heap = new HeapSort();
17-
18+
const Shell = new ShellSort();
1819

1920
useEffect(() => {
2021
var dropdown = document.getElementById('algo-selection-dropdown');
@@ -32,6 +33,7 @@ function AlgorithmSelection({strategy, options}) {
3233
else if(currentAlgo === "Selection") strategy.setStrategy = Selection;
3334
else if(currentAlgo === "Quick") strategy.setStrategy = Quick;
3435
else if(currentAlgo === "Heap") strategy.setStrategy = Heap;
36+
else if(currentAlgo === "Shell") strategy.setStrategy = Shell;
3537
}, [])
3638

3739

@@ -94,6 +96,18 @@ function AlgorithmSelection({strategy, options}) {
9496
>
9597
<p className={`${currentAlgo === 'Quick' && 'text-black group-hover:text-white'}`}>Quick Sort</p>
9698
</div>
99+
100+
<div className={`${currentAlgo === 'Shell' && 'bg-gray-200'} group cursor-pointer w-full hover:bg-gray-700 font-semibold text-white h-[2.8em] flex items-center justify-start pl-4`}
101+
onClick={() => {
102+
setCurrentAlgo('Shell');
103+
options.setState({algorithm: "Shell"});
104+
strategy.setStrategy = Shell;
105+
localStorage.setItem("algorithm", "Shell");
106+
setShowDropdown(!showDropdown)
107+
}}
108+
>
109+
<p className={`${currentAlgo === 'Shell' && 'text-black group-hover:text-white'}`}>Shell Sort</p>
110+
</div>
97111

98112

99113
<div className={`${currentAlgo === 'Selection' && 'bg-gray-200'} group cursor-pointer w-full rounded-b-md hover:bg-gray-700 font-semibold text-white h-[2.8em] flex items-center justify-start pl-4`}

‎src/components/Canvas/Canvas.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class Canvas extends Component {
2727

2828
return (
2929
<div className={`w-full flex justify-center dark:bg-gradient-to-b from-lightGray to-richBlue relative`} style={{minHeight:CANVAS_HEIGHT}}>
30-
<p className="absolute top-3 font-medium text-red-600 " id="sort-canvas-error-msg"></p>
30+
<p className="absolute top-3 z-40 font-semibold text-red-600 font-sans" id="sort-canvas-error-msg"></p>
3131
<div className="flex items-end z-1">
3232
{arrayLength > 1 ? generatedArray.map((element) => {
3333
return (
@@ -36,7 +36,7 @@ class Canvas extends Component {
3636
)
3737
}) : <SortingCanvasDescription/>}
3838
</div>
39-
<div className="main-canvas-grid z-0 dark:opacity-[0.05]"/>
39+
<div className="main-canvas-grid z-0 dark:opacity-[0.03]"/>
4040
</div>
4141
);
4242
}

‎src/components/Canvas/InputField.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ class InputField extends Component {
8282
<AlgorithmSelection strategy={this.Strategy} options={this}/>
8383
<div onClick={async () => {
8484
if(generatedArray.length === 0){
85-
document.getElementById('sort-canvas-error-msg').innerHTML ="Please enter an array size first."
85+
document.getElementById('sort-canvas-error-msg').innerHTML ="Please generate an array first."
8686
} else {
8787
this.scrollDown();
8888
this.Strategy.setOptions = options;

‎src/components/Canvas/options/Option.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function Option({options, mainCanvasInputHandler, algorithm}) {
5454
}
5555

5656
// SG 07/13/2022 13:06 Making sure that only Selection sort will have the option to change the optimum key color
57-
if((algorithm === "Radix" || algorithm === "Merge") && (options.name === "CURRENT_MIN")) return null;
57+
if((algorithm === "Radix" || algorithm === "Merge" || algorithm === "Heap" || algorithm === "Shell") && (options.name === "CURRENT_MIN")) return null;
5858
return (
5959
<div className="flex flex-col items-center relative space-y-1 cursor-pointer">
6060
<div id={`${options.name}-option`} className="p-2 mb-1 hover:bg-gray-200 showDefaultBarColor rounded-md peer" onClick={() => {
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { Animation } from "../../Animation/Animation";
2+
3+
class ShellSort {
4+
5+
constructor() {
6+
this.options = null;
7+
}
8+
9+
getIndexDOM(element, DOM) {
10+
for(const e of DOM) {
11+
if(element === parseInt(e.id)) return DOM.indexOf(e);
12+
}
13+
}
14+
15+
async shellSort(array, n = array.length) {
16+
for (let gap = Math.floor(n / 2); gap > 0; gap = Math.floor(gap / 2)) {
17+
let firstLoopEnds = !Math.floor(gap / 2) > 0;
18+
for (let i = gap; i < n; i += 1) {
19+
if(!this.options.skipJ) {
20+
await Animation.getAnimation(this.options.delay);
21+
}
22+
23+
let temp = array[i];
24+
let j;
25+
26+
for (j = i; j >= gap && array[j - gap] > temp; j -= gap) {
27+
let DOM = Array.from(document.querySelectorAll('.array-bars'));
28+
DOM[j].style.backgroundColor = this.options.processingColor;
29+
DOM[j-gap].style.backgroundColor = this.options.processingColor;
30+
let index = this.getIndexDOM(array[j], DOM);
31+
32+
// SG 07/15/2022 17:17 changing heights
33+
await Animation.getAnimation(this.options.delay);
34+
Animation.changeHeight(index, array[j - gap], array.indexOf(array[j - gap]));
35+
DOM[j].style.backgroundColor = this.options.defaultBarColor;
36+
DOM[j-gap].style.backgroundColor = this.options.defaultBarColor;
37+
38+
// SG 07/15/2022 17:23 coloring sorted bars at the last iteration
39+
if(firstLoopEnds) {
40+
DOM[j].style.backgroundColor = this.options.sortedBarColor;
41+
DOM[j-gap].style.backgroundColor = this.options.sortedBarColor;
42+
}
43+
44+
array[j] = array[j - gap];
45+
}
46+
47+
48+
let DOM = Array.from(document.querySelectorAll('.array-bars'));
49+
50+
// SG 07/15/2022 17:23 coloring sorted bars at the last iteration
51+
if(firstLoopEnds) {
52+
DOM[0].style.backgroundColor = this.options.sortedBarColor;
53+
DOM[j].style.backgroundColor = this.options.sortedBarColor;
54+
}
55+
let index = this.getIndexDOM(array[j], DOM);
56+
57+
array[j] = temp;
58+
Animation.changeHeight(index, temp, array.indexOf(array[j]));
59+
}
60+
}
61+
return array;
62+
}
63+
64+
perform(options, array) {
65+
this.options = options;
66+
this.shellSort(array);
67+
}
68+
}
69+
70+
export { ShellSort };

0 commit comments

Comments
 (0)