Skip to content

Commit a315d8a

Browse files
committed
added selection sort
1 parent bf241e7 commit a315d8a

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
var SelectionSortAlgo=(array)=>{
2+
var animations=[];
3+
var index=0;
4+
var minindex;
5+
for (var i=0;i<array.length-1;i++)
6+
{
7+
var min= i;
8+
for(var j=i+1;j<array.length;j++)
9+
{
10+
animations[index]=[i,j,0];
11+
if (array[j]<array[min])
12+
{
13+
minindex=index;
14+
min=j;
15+
}
16+
index++;
17+
}
18+
if(min!==i)
19+
{
20+
animations[minindex][2]=1;
21+
var t=array[i];
22+
array[i]=array[min];
23+
array[min]=t;
24+
}
25+
}
26+
// console.log(animations);
27+
return animations;
28+
}
29+
30+
export default SelectionSortAlgo;

src/components/animations/bubbleAnimations.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, {Component} from 'react';
22
import './bubbleAnimations.css';
33
import BubbleSortAlgo from '../algorithms/bubbleSort';
4+
import SelectionSortAlgo from '../algorithms/selectionSort'
45

56
class BubbleSort extends Component
67
{
@@ -10,9 +11,13 @@ class BubbleSort extends Component
1011
componentDidMount(){
1112
this.generateRandom();
1213
}
13-
sortArray=()=>{
14+
bubbleSort=()=>{
1415
var animationsArray=BubbleSortAlgo(this.state.array);
15-
console.log("hi");
16+
this.startAnimations(animationsArray);
17+
}
18+
19+
selectionSort=()=>{
20+
var animationsArray=SelectionSortAlgo(this.state.array);
1621
this.startAnimations(animationsArray);
1722
}
1823
startAnimations(animationsArray)
@@ -23,7 +28,7 @@ class BubbleSort extends Component
2328
var swap = comparision[2];
2429
// console.log(firstElem,secondElem)
2530
setTimeout(()=>{
26-
console.log(firstElem,secondElem)
31+
// console.log(firstElem,secondElem)
2732
this.changeColourCompare(firstElem,secondElem);
2833
},(i+0.25)*50)
2934
setTimeout(()=>{this.changeBackColour(firstElem,secondElem);},(i+0.5)*50)
@@ -70,7 +75,8 @@ class BubbleSort extends Component
7075
return(
7176
<>
7277
<button className="newArray" onClick={this.generateRandom}>New Array</button>
73-
<button className="sortArray" onClick={this.sortArray}>Sort Array</button>
78+
<button className="bubbleSort" onClick={this.bubbleSort}>Bubble Sort</button>
79+
<button className="selectionSort" onClick={this.selectionSort}>Selection Sort</button>
7480
<div className='bars'>
7581
{array.map((value,i)=>{
7682
return (

0 commit comments

Comments
 (0)