|
1 |
| -/* eslint-disable no-prototype-builtins */ |
2 |
| -/* eslint-disable jsx-a11y/label-has-associated-control */ |
3 |
| -import React, { useState, useContext, useEffect } from 'react'; |
4 |
| -import { genRandNumList } from './helpers/ParamHelper'; |
| 1 | +// Adapted from Quicksort - could rename a few things |
| 2 | + |
| 3 | +/* eslint-disable no-unused-vars */ |
| 4 | +import React, { useState, useEffect, useContext } 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'; |
5 | 11 |
|
6 |
| -import ListParam from './helpers/ListParam.js'; |
7 | 12 | import PropTypes from 'prop-types'; // Import this for URL Param
|
8 | 13 | import { withAlgorithmParams } from './helpers/urlHelpers' // Import this for URL Param
|
9 | 14 |
|
10 |
| -import { URLContext } from '../../context/urlState.js'; |
11 |
| -import '../../styles/Param.scss'; |
| 15 | +import { URLContext } from '../../context/urlState'; |
| 16 | + |
| 17 | + |
| 18 | +const DEFAULT_ARRAY_GENERATOR = genRandNumList.bind(null, 12, 1, 99); |
| 19 | +const DEFAULT_ARR = DEFAULT_ARRAY_GENERATOR(); |
| 20 | +const MERGE_SORT = 'Heap Sort'; |
| 21 | +const MERGE_SORT_EXAMPLE = 'Please follow the example provided: 0,1,2,3,4'; |
| 22 | +const UNCHECKED = { |
| 23 | + random: false, |
| 24 | + sortedAsc: false, |
| 25 | + // bestCase: false, |
| 26 | + sortedDesc: false |
| 27 | +}; |
| 28 | + |
| 29 | +const BlueRadio = withStyles({ |
| 30 | + root: { |
| 31 | + color: '#2289ff', |
| 32 | + '&$checked': { |
| 33 | + color: '#027aff', |
| 34 | + }, |
| 35 | + }, |
| 36 | + checked: {}, |
| 37 | + // eslint-disable-next-line react/jsx-props-no-spreading |
| 38 | +})((props) => <Radio {...props} />) |
| 39 | + |
| 40 | +function MergesortParam({ list }) { |
| 41 | + const [message, setMessage] = useState(null) |
| 42 | + |
| 43 | + const [array, setArray] = useState(list || DEFAULT_ARR) |
| 44 | + const { setNodes } = useContext(URLContext) |
| 45 | + |
| 46 | + const [QSCase, setQSCase] = useState({ |
| 47 | + random: true, |
| 48 | + sortedAsc: false, |
| 49 | + // bestCase: false, |
| 50 | + sortedDesc: false |
| 51 | + }); |
| 52 | + |
| 53 | + useEffect(() => { |
| 54 | + setNodes(array); |
| 55 | + }, [array]); |
| 56 | + |
| 57 | + // XXX best case definitely not needed; could skip choice of cases |
| 58 | + // function for choosing the type of input |
| 59 | + const handleChange = (e) => { |
| 60 | + switch (e.target.name) { |
| 61 | + case 'sortedAsc': |
| 62 | + setArray([...array].sort(function (a, b) { |
| 63 | + return (+a) - (+b) |
| 64 | + })); |
| 65 | + break; |
| 66 | + case 'sortedDesc': |
| 67 | + setArray([...array].sort(function (a, b) { |
| 68 | + return (+b) - (+a) |
| 69 | + })); |
| 70 | + break; |
| 71 | + case 'random': |
| 72 | + setArray(DEFAULT_ARRAY_GENERATOR()); |
| 73 | + break; |
| 74 | + case 'bestCase': |
| 75 | + setArray(quicksortPerfectPivotArray(Math.floor(Math.random() * 10), 25 + (Math.floor(Math.random() * 25)))); |
| 76 | + break; |
| 77 | + default: |
| 78 | + break; |
| 79 | + } |
| 80 | + |
| 81 | + setQSCase({ ...UNCHECKED, [e.target.name]: true }) |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + useEffect( |
| 86 | + () => { |
| 87 | + document.getElementById('startBtnGrp').click(); |
| 88 | + }, |
| 89 | + [QSCase], |
| 90 | + ); |
12 | 91 |
|
13 |
| -// export const DEFAULT_NODES = genRandNumList(12, 1, 50); |
14 |
| -const DEFAULT_NODES = genRandNumList(10, 1, 100); |
15 |
| -const HEAP_SORT = 'Heap Sort'; |
16 |
| -const HEAP_SORT_EXAMPLE = 'Please follow the example provided: 0,1,2,3,4'; |
17 |
| - |
18 |
| - |
19 |
| -function HeapsortParam({ list }) { // add the parsing parameters for your algorithm: alg, mode, ...params |
20 |
| - // const { alg, mode, param } = useUrlParams(); |
21 |
| - // const {list, value, xyCoords, edgeWeights, start, end, string, pattern, union} = parseParam(param); |
22 |
| - // const { alg, mode, list } = withAlgorithmParams(HeapsortParam); |
23 |
| - // const DEFAULT_NODES = genRandNumList.bind(null, 12, 1, 50); // Define the default list of nodes |
24 |
| - const [localNodes, setLocalNodes] = useState(list || DEFAULT_NODES); |
25 |
| - const [message, setMessage] = useState(null); |
26 |
| - const { setNodes } = useContext(URLContext); |
27 |
| - |
28 |
| - useEffect(() => { |
29 |
| - setNodes(localNodes); |
30 |
| - }, [localNodes]); |
31 |
| - |
32 |
| - return ( |
33 |
| - <> |
34 |
| - <div className="form"> |
35 |
| - <ListParam |
36 |
| - name="heapSort" |
37 |
| - buttonName="Sort" |
38 |
| - mode="sort" |
39 |
| - formClassName="formLeft" |
40 |
| - DEFAULT_VAL={localNodes} |
41 |
| - SET_VAL={setLocalNodes} |
42 |
| - ALGORITHM_NAME={HEAP_SORT} |
43 |
| - EXAMPLE={HEAP_SORT_EXAMPLE} |
44 |
| - setMessage={setMessage} |
45 |
| - /> |
46 |
| - </div> |
47 |
| - {message} |
48 |
| - </> |
49 |
| - ); |
| 92 | + return ( |
| 93 | + <> |
| 94 | + <div className="form"> |
| 95 | + <ListParam |
| 96 | + name="heapSort" |
| 97 | + buttonName="Reset" |
| 98 | + mode="sort" |
| 99 | + formClassName="formLeft" |
| 100 | + DEFAULT_VAL={array} |
| 101 | + SET_VAL={setArray} |
| 102 | + REFRESH_FUNCTION={ |
| 103 | + (() => { |
| 104 | + if (QSCase.sortedAsc) { |
| 105 | + return () => { |
| 106 | + return (DEFAULT_ARRAY_GENERATOR().sort(function (a, b) { |
| 107 | + return (+a) - (+b) |
| 108 | + })); |
| 109 | + } |
| 110 | + } |
| 111 | + else if (QSCase.sortedDesc) { |
| 112 | + return () => { |
| 113 | + return (DEFAULT_ARRAY_GENERATOR().sort(function (a, b) { |
| 114 | + return (+b) - (+a) |
| 115 | + })); |
| 116 | + } |
| 117 | + } |
| 118 | + else if (QSCase.bestCase) { |
| 119 | + return () => quicksortPerfectPivotArray(Math.floor(Math.random() * 10), 25 + (Math.floor(Math.random() * 25))); |
| 120 | + } |
| 121 | + })() |
| 122 | + } |
| 123 | + ALGORITHM_NAME={MERGE_SORT} |
| 124 | + EXAMPLE={MERGE_SORT_EXAMPLE} |
| 125 | + setMessage={setMessage} |
| 126 | + /> |
| 127 | + </div> |
| 128 | + <span className="generalText">Choose input format: </span> |
| 129 | + {/* create a checkbox for Random array elements */} |
| 130 | + <FormControlLabel |
| 131 | + control={ |
| 132 | + <BlueRadio |
| 133 | + checked={QSCase.random} |
| 134 | + onChange={handleChange} |
| 135 | + name="random" |
| 136 | + /> |
| 137 | + } |
| 138 | + label="Random" |
| 139 | + className="checkbox" |
| 140 | + /> |
| 141 | + <FormControlLabel |
| 142 | + control={ |
| 143 | + <BlueRadio |
| 144 | + checked={QSCase.sortedAsc} |
| 145 | + onChange={handleChange} |
| 146 | + name="sortedAsc" |
| 147 | + /> |
| 148 | + } |
| 149 | + label="Sorted (ascending)" |
| 150 | + className="checkbox" |
| 151 | + /> |
| 152 | + <FormControlLabel |
| 153 | + control={ |
| 154 | + <BlueRadio |
| 155 | + checked={QSCase.sortedDesc} |
| 156 | + onChange={handleChange} |
| 157 | + name="sortedDesc" |
| 158 | + /> |
| 159 | + } |
| 160 | + label="Sorted (descending)" |
| 161 | + className="checkbox" |
| 162 | + /> |
| 163 | + {/* render success/error message */} |
| 164 | + {message} |
| 165 | + </> |
| 166 | + ) |
50 | 167 | }
|
51 | 168 |
|
52 | 169 | // Define the prop types for URL Params
|
53 |
| -HeapsortParam.propTypes = { |
54 |
| - mode: PropTypes.string, |
55 |
| - list: PropTypes.string |
| 170 | +MergesortParam.propTypes = { |
| 171 | + alg: PropTypes.string.isRequired, |
| 172 | + mode: PropTypes.string.isRequired, |
| 173 | + list: PropTypes.string.isRequired |
56 | 174 | };
|
57 | 175 |
|
58 |
| -export default withAlgorithmParams(HeapsortParam); // Export with the wrapper for URL Params |
| 176 | +export default withAlgorithmParams(MergesortParam); // Export with the wrapper for URL Params |
0 commit comments