Skip to content

Commit 25995cc

Browse files
committed
Added heap, merge sort, Fixed Single sort issue
1 parent f9124e2 commit 25995cc

17 files changed

Lines changed: 798 additions & 6 deletions

File tree

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,20 @@ So far there are 6 segemets
1111
- Convex Hull
1212
- Binary Search Game
1313

14-
I have implemented a total of 11 algorithms so far. And will try to add more later.
14+
I have implemented a total of 13 algorithms so far. And will try to add more later.
15+
ALgorithms implemented yet so far are
16+
- DFS
17+
- BFS
18+
- Djsktra
19+
- Recursive Maze Creation
20+
- Bubble sort
21+
- Selection sort
22+
- Insertion sort
23+
- Heap sort
24+
- Merge sort
25+
- Sieve of Eratosthenes
26+
- N Queen Backtracking
27+
- Graham Scan for Convex Hull
28+
- Binary Search
1529
I am not sure if anyone would like to contribute to this project or not. But any kind of contributions are welcomes. Also if you like this please star this repo. It keeps me motivated.
1630

src/App.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Sort from "./sortComponents/sort";
77
import Queen from "./queenComponents/queen";
88
import ConvexHull from "./convexHullComponents/convexHull";
99
import BinarySearch from "./binarySearchComponent/binarySearch";
10-
10+
import RecursiveSort from "./recursiveSortComponents/recursiveSort";
1111
class App extends Component {
1212
constructor() {
1313
super();
@@ -26,6 +26,7 @@ class App extends Component {
2626
<Route path='/nqueen' component={Queen}/>
2727
<Route path='/convexhull' component={ConvexHull}/>
2828
<Route path='/binarysearch' component={BinarySearch}/>
29+
<Route path='/recursivesort' component={RecursiveSort}/>
2930
<Route path='/' component={Home}/>
3031
</Switch>
3132
</Router>

src/algorithms/heapSort.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
let values = [];
2+
export default function HeapSort(rects2){
3+
let rects = rects2.slice();
4+
values = [];
5+
let sz = rects2.length;
6+
// sz = sz-1;
7+
heapSort(rects,sz);
8+
return values;
9+
}
10+
function heapify(rects,n,i){
11+
let largest = i; // Initialize largest as root
12+
let l = 2 * i + 1; // left = 2*i + 1
13+
let r = 2 * i + 2; // right = 2*i + 2
14+
15+
// If left child is larger than root
16+
if (l < n && rects[l].width > rects[largest].width)
17+
largest = l;
18+
19+
// If right child is larger than largest so far
20+
if (r < n && rects[r].width > rects[largest].width)
21+
largest = r;
22+
23+
// If largest is not root
24+
if (largest != i) {
25+
let temp = rects[i];
26+
rects[i] = rects[largest];
27+
rects[largest] = temp;
28+
let value = {
29+
left:i,
30+
right:largest,
31+
sorted: false
32+
}
33+
values.push(value);
34+
// Recursively heapify the affected sub-tree
35+
heapify(rects, n, largest);
36+
}
37+
}
38+
function heapSort(rects,n){
39+
for(let i = Math.floor(n/2)-1;i>=0;i--){
40+
// console.log("heap ",n," ",i);
41+
heapify(rects,n,i);
42+
}
43+
for (let i = n-1 ; i > 0; i--) {
44+
// Move current root to end
45+
let temp = rects[i];
46+
rects[i] = rects[0];
47+
rects[0] = temp;
48+
let value = {
49+
left:i,
50+
right:0,
51+
sorted:true
52+
}
53+
values.push(value);
54+
// call max heapify on the reduced heap
55+
heapify(rects, i, 0);
56+
}
57+
}

src/algorithms/mergeSort.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
let values = [];
2+
export default function mergeSort(rects2){
3+
let rects = rects2.slice();
4+
values = [];
5+
let sz = rects2.length;
6+
// console.log( "fdsfsd",sz );
7+
sz = sz-1;
8+
mergeS(rects,0,sz);
9+
return values;
10+
}
11+
12+
function merge(rects, l, m, r){
13+
// console.log(l," ",r);
14+
let n1 = m-l+1;
15+
let n2 = r-m;
16+
17+
const L = rects.slice(l, m+1);
18+
const R = rects.slice(m+1,r+1);
19+
let i = 0;
20+
let j = 0;
21+
let k = l;
22+
while(i<n1 && j<n2){
23+
if( L[i].width <= R[j].width ){
24+
rects[k] = L[i];
25+
i++;
26+
} else{
27+
rects[k] = R[j];
28+
j++;
29+
}
30+
k++;
31+
}
32+
while (i < n1) {
33+
rects[k] = L[i];
34+
i++;
35+
k++;
36+
}
37+
while (j < n2) {
38+
rects[k] = R[j];
39+
j++;
40+
k++;
41+
}
42+
43+
}
44+
45+
function mergeS(rects,l,r){
46+
47+
if( l>=r ) return;
48+
let m = l+ (r-l)/2;
49+
m = Math.floor(m);
50+
// console.log("iiiiiiiiiiiiiiiiiiiiiiiii ",m);
51+
mergeS(rects,l,m);
52+
mergeS(rects,m+1,r);
53+
merge(rects,l,m,r);
54+
let rectsCopy = rects.slice(l,r+1);
55+
let value = {
56+
left:l,
57+
right:r,
58+
mid:m,
59+
val:rectsCopy
60+
}
61+
values.push(value);
62+
}

src/homeComponents/cardDetails.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ export function getDetails(){
2929
route:"/sort",
3030
img:sort
3131
},
32+
{
33+
id:9,
34+
title:"Recursive Sorting",
35+
description:"Compare different recursive sorting algorithms",
36+
route:"/recursivesort",
37+
img:sort
38+
},
3239
{
3340
id:4,
3441
title:"N Queen",
@@ -65,5 +72,6 @@ export function getDetails(){
6572
route:"/",
6673
img:puzzle
6774
}
75+
6876
]
6977
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import { withStyles, makeStyles } from '@material-ui/core/styles';
4+
import Slider from '@material-ui/core/Slider';
5+
import Typography from '@material-ui/core/Typography';
6+
import Tooltip from '@material-ui/core/Tooltip';
7+
8+
const useStyles = makeStyles((theme) => ({
9+
root: {
10+
width: 300 + theme.spacing(3) * 2,
11+
},
12+
margin: {
13+
height: theme.spacing(3),
14+
},
15+
}));
16+
17+
function ValueLabelComponent(props) {
18+
const { children, open, value } = props;
19+
20+
return (
21+
<Tooltip open={open} enterTouchDelay={0} placement="top" title={value}>
22+
{children}
23+
</Tooltip>
24+
);
25+
}
26+
27+
ValueLabelComponent.propTypes = {
28+
children: PropTypes.element.isRequired,
29+
open: PropTypes.bool.isRequired,
30+
value: PropTypes.number.isRequired,
31+
};
32+
33+
34+
35+
36+
const AirbnbSlider = withStyles({
37+
root: {
38+
color: '#3a8589',
39+
height: 3,
40+
padding: '13px 0',
41+
},
42+
thumb: {
43+
height: 27,
44+
width: 27,
45+
backgroundColor: '#fff',
46+
border: '1px solid currentColor',
47+
marginTop: -12,
48+
marginLeft: -13,
49+
boxShadow: '#ebebeb 0 2px 2px',
50+
'&:focus, &:hover, &$active': {
51+
boxShadow: '#ccc 0 2px 3px 1px',
52+
},
53+
'& .bar': {
54+
// display: inline-block !important;
55+
height: 9,
56+
width: 1,
57+
backgroundColor: 'currentColor',
58+
marginLeft: 1,
59+
marginRight: 1,
60+
},
61+
},
62+
active: {},
63+
track: {
64+
height: 3,
65+
},
66+
rail: {
67+
color: '#ffffff',
68+
opacity: 1,
69+
height: 3,
70+
},
71+
})(Slider);
72+
73+
function AirbnbThumbComponent(props) {
74+
return (
75+
<span {...props}>
76+
<span className="bar" />
77+
<span className="bar" />
78+
<span className="bar" />
79+
</span>
80+
);
81+
}
82+
83+
export default function CustomizedSlider() {
84+
const classes = useStyles();
85+
86+
return (
87+
<div className={classes.root}>
88+
<Typography gutterBottom>Airbnb</Typography>
89+
<AirbnbSlider
90+
ThumbComponent={AirbnbThumbComponent}
91+
getAriaLabel={(index) => (index === 0 ? 'Minimum price' : 'Maximum price')}
92+
defaultValue={[20, 40]}
93+
valueLabelDisplay="on"
94+
/>
95+
</div>
96+
);
97+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React from 'react';
2+
import {makeStyles, withStyles} from '@material-ui/core/styles';
3+
import Typography from '@material-ui/core/Typography';
4+
import Slider from '@material-ui/core/Slider';
5+
6+
const useStyles = makeStyles({
7+
root: {
8+
width: 200,
9+
},
10+
});
11+
12+
13+
const CSlider = withStyles({
14+
root: {
15+
// color: "#ffffff",
16+
height: 3,
17+
padding: "13px 0",
18+
},
19+
track: {
20+
height: 4,
21+
borderRadius: 2,
22+
},
23+
thumb: {
24+
backgroundColor: "#fff",
25+
//color: "#fff",
26+
},
27+
})(Slider);
28+
29+
function valuetext(value) {
30+
return `${value}`;
31+
}
32+
33+
34+
35+
export default function RangeSlider(props) {
36+
const classes = useStyles();
37+
const [value, setValue] = React.useState([20, 37]);
38+
39+
const handleChange = (event, newValue) => {
40+
setValue(newValue);
41+
42+
};
43+
const handleCommit = (event, newValue) => {
44+
console.log(newValue);
45+
};
46+
47+
return (
48+
<div className={classes.root}>
49+
<CSlider
50+
disabled={props.disable}
51+
value={value}
52+
onChange={handleChange}
53+
onChangeCommitted={handleCommit}
54+
valueLabelDisplay="auto"
55+
aria-labelledby="range-slider"
56+
getAriaValueText={valuetext}
57+
valueLabelDisplay="off"
58+
/>
59+
<Typography id="range-slider" gutterBottom>
60+
Value range
61+
</Typography>
62+
</div>
63+
);
64+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import FormGroup from '@material-ui/core/FormGroup';
3+
import FormControlLabel from '@material-ui/core/FormControlLabel';
4+
import Switch from '@material-ui/core/Switch';
5+
6+
export default function SwitchLabels(props) {
7+
const [state, setState] = React.useState({
8+
checkedA: false,
9+
});
10+
11+
const handleChange = (event) => {
12+
setState({ ...state, [event.target.name]: event.target.checked });
13+
props.onDoubleChange(event.target.checked);
14+
};
15+
16+
return (
17+
<FormGroup row>
18+
<FormControlLabel
19+
control={<Switch checked={state.checkedA} onChange={handleChange} name="checkedA" />}
20+
label="Duo"
21+
disabled={props.disable}
22+
/>
23+
24+
</FormGroup>
25+
);
26+
}

0 commit comments

Comments
 (0)