Skip to content

Commit d0a8021

Browse files
initial submit
1 parent 02f24d8 commit d0a8021

File tree

8 files changed

+205
-0
lines changed

8 files changed

+205
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Merge Sort (Natural, for arrays)
2+
3+
---
4+

src/algorithms/extra-info/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as QSInfo } from './QSInfo.md';
44
export { default as msort_arr_td } from './msort_arr_td.md';
55
export { default as msort_arr_bup } from './msort_arr_bup.md';
66
export { default as msort_lista_td } from './msort_lista_td.md';
7+
export { default as msort_arr_nat } from './msort_arr_nat.md';
78
export { default as Prims_oldInfo } from './PRIM_oldInfo.md';
89
export { default as PrimsInfo } from './PRIMInfo.md';
910
export { default as KruskalInfo } from './KRUSKALInfo.md';
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
<style>
3+
a:link {
4+
color: #1e28f0;
5+
}
6+
a:visited{
7+
color: #3c1478;
8+
}
9+
a:hover{
10+
color: #1e288c;
11+
}
12+
</style>
13+
14+
## Extra Info
15+
16+
-----
17+
18+
Geeks for Geeks Link: [**Iterative Merge Sort**][G4GLink]
19+
20+
21+
[G4GLink]: https://www.geeksforgeeks.org/iterative-merge-sort/
22+

src/algorithms/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,23 @@ const allalgs = {
121121
},
122122
},
123123

124+
'msort_arr_nat': {
125+
name: 'Merge Sort (natural)',
126+
noDeploy: false,
127+
category: 'Sort',
128+
explanation: Explanation.msort_arr_nat,
129+
param: <Param.msort_arr_nat />,
130+
instructions: Instructions.msort_arr_nat,
131+
extraInfo: ExtraInfo.msort_arr_nat,
132+
pseudocode: {
133+
sort: Pseudocode.msort_arr_nat,
134+
},
135+
controller: {
136+
sort: Controller.msort_arr_nat,
137+
},
138+
},
139+
140+
124141
'msort_lista_td': {
125142
name: 'Merge Sort (lists)',
126143
category: 'Sort',

src/algorithms/instructions/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export const HSInstruction = sortInstructions;
118118
export const QSInstruction = sortInstructions;
119119
export const msort_arr_td = sortInstructions;
120120
export const msort_arr_bup = sortInstructions;
121+
export const msort_arr_nat = sortInstructions;
121122
export const msort_lista_td = sortInstructions;
122123
export const TCInstruction = graphInstructionsTC;
123124
export const Prims_oldInstruction = graphInstructions;

src/algorithms/parameters/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as BSTParam } from './BSTParam';
44
export { default as QSParam } from './QSParam';
55
export { default as msort_arr_td } from './msort_arr_td';
66
export { default as msort_arr_bup } from './msort_arr_bup';
7+
export { default as msort_arr_nat } from './msort_arr_nat';
78
export { default as msort_lista_td } from './msort_lista_td';
89
export { default as Prims_oldParam } from './PRIM_oldParam';
910
export { default as PrimsParam } from './PRIMParam';
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
// Adapted from Quicksort - could rename a few things
2+
3+
/* eslint-disable no-unused-vars */
4+
import React, { useState, useEffect } from 'react';
5+
import FormControlLabel from '@mui/material/FormControlLabel';
6+
import Radio from '@mui/material/Radio';
7+
import { withStyles } from '@mui/styles';
8+
import { genRandNumList, quicksortPerfectPivotArray } from './helpers/ParamHelper';
9+
import ListParam from './helpers/ListParam';
10+
import '../../styles/Param.scss';
11+
12+
const DEFAULT_ARRAY_GENERATOR = genRandNumList.bind(null, 12, 1, 50);
13+
const DEFAULT_ARR = DEFAULT_ARRAY_GENERATOR();
14+
const MERGE_SORT = 'Merge Sort (bottom up)';
15+
const MERGE_SORT_EXAMPLE = 'Please follow the example provided: 0,1,2,3,4';
16+
const UNCHECKED = {
17+
random: false,
18+
sortedAsc: false,
19+
// bestCase: false,
20+
sortedDesc: false
21+
};
22+
23+
const BlueRadio = withStyles({
24+
root: {
25+
color: '#2289ff',
26+
'&$checked': {
27+
color: '#027aff',
28+
},
29+
},
30+
checked: {},
31+
// eslint-disable-next-line react/jsx-props-no-spreading
32+
})((props) => <Radio {...props} />)
33+
34+
function MergesortParam() {
35+
const [message, setMessage] = useState(null)
36+
const [array, setArray] = useState(DEFAULT_ARR)
37+
const [QSCase, setQSCase] = useState({
38+
random: true,
39+
sortedAsc: false,
40+
// bestCase: false,
41+
sortedDesc: false
42+
});
43+
44+
45+
46+
// XXX best case definitely not needed; could skip choice of cases
47+
// function for choosing the type of input
48+
const handleChange = (e) => {
49+
switch (e.target.name) {
50+
case 'sortedAsc':
51+
setArray([...array].sort(function (a, b) {
52+
return (+a) - (+b)
53+
}));
54+
break;
55+
case 'sortedDesc':
56+
setArray([...array].sort(function (a, b) {
57+
return (+b) - (+a)
58+
}));
59+
break;
60+
case 'random':
61+
setArray(DEFAULT_ARRAY_GENERATOR());
62+
break;
63+
case 'bestCase':
64+
setArray(quicksortPerfectPivotArray(Math.floor(Math.random() * 10), 25 + (Math.floor(Math.random() * 25))));
65+
break;
66+
default:
67+
break;
68+
}
69+
70+
setQSCase({ ...UNCHECKED, [e.target.name]: true })
71+
72+
}
73+
74+
useEffect(
75+
() => {
76+
document.getElementById('startBtnGrp').click();
77+
},
78+
[QSCase],
79+
);
80+
81+
return (
82+
<>
83+
<div className="form">
84+
<ListParam
85+
name="msort_arr_bup"
86+
buttonName="Reset"
87+
mode="sort"
88+
formClassName="formLeft"
89+
DEFAULT_VAL={array}
90+
SET_VAL={setArray}
91+
REFRESH_FUNCTION={
92+
(() => {
93+
if (QSCase.sortedAsc) {
94+
return () => {
95+
return (DEFAULT_ARRAY_GENERATOR().sort(function (a, b) {
96+
return (+a) - (+b)
97+
}));
98+
}
99+
}
100+
else if (QSCase.sortedDesc) {
101+
return () => {
102+
return (DEFAULT_ARRAY_GENERATOR().sort(function (a, b) {
103+
return (+b) - (+a)
104+
}));
105+
}
106+
}
107+
else if (QSCase.bestCase) {
108+
return () => quicksortPerfectPivotArray(Math.floor(Math.random() * 10), 25 + (Math.floor(Math.random() * 25)));
109+
}
110+
})()
111+
}
112+
ALGORITHM_NAME={MERGE_SORT}
113+
EXAMPLE={MERGE_SORT_EXAMPLE}
114+
setMessage={setMessage}
115+
/>
116+
</div>
117+
<span className="generalText">Choose input format: &nbsp;&nbsp;</span>
118+
{/* create a checkbox for Random array elements */}
119+
<FormControlLabel
120+
control={
121+
<BlueRadio
122+
checked={QSCase.random}
123+
onChange={handleChange}
124+
name="random"
125+
/>
126+
}
127+
label="Random"
128+
className="checkbox"
129+
/>
130+
<FormControlLabel
131+
control={
132+
<BlueRadio
133+
checked={QSCase.sortedAsc}
134+
onChange={handleChange}
135+
name="sortedAsc"
136+
/>
137+
}
138+
label="Sorted (ascending)"
139+
className="checkbox"
140+
/>
141+
<FormControlLabel
142+
control={
143+
<BlueRadio
144+
checked={QSCase.sortedDesc}
145+
onChange={handleChange}
146+
name="sortedDesc"
147+
/>
148+
}
149+
label="Sorted (descending)"
150+
className="checkbox"
151+
/>
152+
{/* render success/error message */}
153+
{message}
154+
</>
155+
)
156+
}
157+
158+
export default MergesortParam

src/algorithms/pseudocode/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export { default as heapSort } from './heapSort';
44
export { default as quickSort } from './quickSort';
55
export { default as msort_arr_td } from './msort_arr_td';
66
export { default as msort_arr_bup } from './msort_arr_bup';
7+
export { default as msort_arr_nat } from './msort_arr_nat';
78
export { default as msort_lista_td } from './msort_lista_td';
89
export { default as transitiveClosure } from './transitiveClosure';
910
export { default as prim_old } from './prim_old';

0 commit comments

Comments
 (0)