Skip to content

Commit 881cdbb

Browse files
♻️ refactor: add file for exports and refactor code
1 parent 6c9a7d8 commit 881cdbb

File tree

12 files changed

+253
-195
lines changed

12 files changed

+253
-195
lines changed

src/algorithms/BubbleSort.ts

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,51 @@
1-
import {ICurrentIterationResult,swap} from "../utils/algos"
2-
export default function BubbleSort(inputArr: number[]){
3-
4-
const arr = [...inputArr]
5-
const sortedElementIndexes: number[] = []
1+
import { ICurrentIterationResult } from "../utils/algos";
2+
import swap from "../utils/swap";
3+
export default function BubbleSort(inputArr: number[]) {
4+
const arr = [...inputArr];
5+
const sortedElementIndexes: number[] = [];
66

7-
const initial = {
7+
const initial = {
88
iteratingElementIndex: 0,
99
swappedElementIndex: -1,
1010
arrayState: [...inputArr],
11-
sortedElementIndexes: [...sortedElementIndexes]
12-
}
11+
sortedElementIndexes: [...sortedElementIndexes],
12+
};
1313

14-
const results= [initial]
15-
let n = arr.length
16-
let i, j
14+
const results = [initial];
15+
let n = arr.length;
16+
let i, j;
1717

18-
// main sorting logic
19-
for(i = 0; i< n-1 ;i++){
20-
let isSorted = true
21-
for(j= 0; j< n-i-1 ; j++){
18+
// main sorting logic
19+
for (i = 0; i < n - 1; i++) {
20+
let isSorted = true;
21+
for (j = 0; j < n - i - 1; j++) {
22+
const currentIterationResult: ICurrentIterationResult = {
23+
iteratingElementIndex: j,
24+
swappedElementIndex: -1,
25+
arrayState: [],
26+
sortedElementIndexes: [...sortedElementIndexes],
27+
};
28+
if (arr[j] > arr[j + 1]) {
29+
isSorted = false;
30+
swap(arr, j, j + 1);
31+
currentIterationResult.swappedElementIndex = j;
32+
}
33+
currentIterationResult.arrayState = [...arr];
34+
results.push(currentIterationResult);
35+
}
36+
sortedElementIndexes.push(j);
37+
if (isSorted) {
38+
break;
39+
}
40+
}
2241

23-
const currentIterationResult : ICurrentIterationResult= {iteratingElementIndex: j,swappedElementIndex: -1,arrayState: [], sortedElementIndexes: [...sortedElementIndexes]}
24-
if(arr[j] > arr[j+1]){
25-
isSorted = false
26-
swap(arr,j,j+1)
27-
currentIterationResult.swappedElementIndex = j
28-
}
29-
currentIterationResult.arrayState = [...arr]
30-
results.push(currentIterationResult)
31-
}
32-
sortedElementIndexes.push(j)
33-
if(isSorted){
34-
break
35-
}
36-
}
42+
// Added extra step manually similar to last step but including all array element index in sorted
43+
// indexes since sorting algo is over .
44+
const sortedIndexes = Array.from(Array(n)).map((_i, idx) => idx);
45+
results.push({
46+
...results[results.length - 1],
47+
sortedElementIndexes: sortedIndexes,
48+
});
3749

38-
// Added extra step manually similar to last step but including all array element index in sorted
39-
// indexes since sorting algo is over .
40-
const sortedIndexes = Array.from(Array(n)).map((_i,idx) => idx)
41-
results.push({...results[results.length - 1], sortedElementIndexes: sortedIndexes})
42-
43-
return results
50+
return results;
4451
}

src/algorithms/SelectionSort.ts

Lines changed: 44 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
1-
import {ICurrentIterationResult,swap} from "../utils/algos"
1+
import { ICurrentIterationResult } from "../utils/algos";
2+
import swap from "../utils/swap";
23

3-
export default function SelectionSort(inputArr: number[]){
4-
5-
const sortedElementIndexes: number[] = []
4+
export default function SelectionSort(inputArr: number[]) {
5+
const sortedElementIndexes: number[] = [];
66

7-
const initial = {
7+
const initial = {
88
iteratingElementIndex: 1,
99
swappedElementIndex: -1,
1010
arrayState: [...inputArr],
11-
sortedElementIndexes: [...sortedElementIndexes]
12-
}
11+
sortedElementIndexes: [...sortedElementIndexes],
12+
};
1313

14-
const results= [initial]
15-
let n = inputArr.length
16-
let i: number, j: number
14+
const results = [initial];
15+
let n = inputArr.length;
16+
let i: number, j: number;
1717

18-
// main sorting logic
19-
for( i = 0; i < n; i++) {
20-
// Finding the smallest number in the subarray
21-
let min = i;
22-
for( j = i+1; j < n; j++){
23-
const currentIterationResult : ICurrentIterationResult= {iteratingElementIndex: j,swappedElementIndex: -1,arrayState: [], sortedElementIndexes: [...sortedElementIndexes]}
24-
if(inputArr[j] < inputArr[min]) {
25-
min=j;
26-
}
27-
currentIterationResult.arrayState = [...inputArr]
28-
results.push(currentIterationResult)
29-
}
30-
if (min != i) {
31-
// Swapping the elements
32-
swap(inputArr,i,min)
33-
results[results.length - 1].swappedElementIndex = min
34-
}
35-
sortedElementIndexes.push(i)
36-
results[results.length -1].sortedElementIndexes = [...sortedElementIndexes]
18+
// main sorting logic
19+
for (i = 0; i < n; i++) {
20+
// Finding the smallest number in the subarray
21+
let min = i;
22+
for (j = i + 1; j < n; j++) {
23+
const currentIterationResult: ICurrentIterationResult = {
24+
iteratingElementIndex: j,
25+
swappedElementIndex: -1,
26+
arrayState: [],
27+
sortedElementIndexes: [...sortedElementIndexes],
28+
};
29+
if (inputArr[j] < inputArr[min]) {
30+
min = j;
31+
}
32+
currentIterationResult.arrayState = [...inputArr];
33+
results.push(currentIterationResult);
3734
}
35+
if (min != i) {
36+
// Swapping the elements
37+
swap(inputArr, i, min);
38+
results[results.length - 1].swappedElementIndex = min;
39+
}
40+
sortedElementIndexes.push(i);
41+
results[results.length - 1].sortedElementIndexes = [
42+
...sortedElementIndexes,
43+
];
44+
}
3845

39-
// Added extra step manually similar to last step but including all array element index in sorted
40-
// indexes since sorting algo is over .
41-
const sortedIndexes = Array.from(Array(n)).map((_i,idx) => idx)
42-
results.push({...results[results.length - 1], sortedElementIndexes: sortedIndexes})
46+
// Added extra step manually similar to last step but including all array element index in sorted
47+
// indexes since sorting algo is over .
48+
const sortedIndexes = Array.from(Array(n)).map((_i, idx) => idx);
49+
results.push({
50+
...results[results.length - 1],
51+
sortedElementIndexes: sortedIndexes,
52+
});
4353

44-
return results
54+
return results;
4555
}

src/algorithms/exports.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export { default as BubbleSort } from "./BubbleSort";
2+
export { default as InsertionSort } from "./InsertionSort";
3+
export { default as SelectionSort } from "./SelectionSort";

src/redux/features/sortSlice.ts

Lines changed: 75 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,82 @@
1-
import { getAlgoByName} from "../../utils/algos"
2-
import { getRandomArray } from "../../utils/getRandomArray"
3-
import BubbleSort from "../../algorithms/BubbleSort"
4-
import {createSlice,PayloadAction} from "@reduxjs/toolkit"
1+
import { getAlgoByName, getRandomArray } from "../../utils/exports";
2+
import { BubbleSort } from "../../algorithms/exports";
3+
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
54

6-
export const INITIAL_ARRAY_SIZE = 20
7-
const MIN_ARRAY_ELEMENT_VALUE = 10
8-
const MAX_ARRAY_ELEMENT_VALUE = 60
5+
export const INITIAL_ARRAY_SIZE = 20;
6+
const MIN_ARRAY_ELEMENT_VALUE = 10;
7+
const MAX_ARRAY_ELEMENT_VALUE = 60;
98

10-
//Setting BUBBLESORT as initial algorithm
9+
//Setting BUBBLESORT as initial algorithm
1110

1211
const initialState = {
13-
isRunning: false,
14-
currentStepIndex: 0,
15-
algorithm:'BubbleSort' ,
16-
sortingSteps: BubbleSort(getRandomArray(INITIAL_ARRAY_SIZE,MIN_ARRAY_ELEMENT_VALUE, MAX_ARRAY_ELEMENT_VALUE))
17-
}
12+
isRunning: false,
13+
currentStepIndex: 0,
14+
algorithm: "BubbleSort",
15+
sortingSteps: BubbleSort(
16+
getRandomArray(
17+
INITIAL_ARRAY_SIZE,
18+
MIN_ARRAY_ELEMENT_VALUE,
19+
MAX_ARRAY_ELEMENT_VALUE
20+
)
21+
),
22+
};
1823

1924
export const sortingSlice = createSlice({
20-
name: "Sorting",
21-
initialState,
22-
reducers: {
23-
setRunningState: (state, action: PayloadAction<boolean>) => {
24-
state.isRunning = action.payload
25-
},
26-
increaseCurrentStepIndex: (state) => {
27-
if(state.currentStepIndex !== state.sortingSteps.length - 1){
28-
state.currentStepIndex += 1
29-
}
30-
},
31-
decreaseCurrentStepIndex: (state) => {
32-
if(state.currentStepIndex !== 0){
33-
state.currentStepIndex -= 1
34-
}
35-
},
36-
updateAlgo: (state, action: PayloadAction<string>) => {
37-
state.algorithm= action.payload
38-
state.sortingSteps = getAlgoByName(state.algorithm)(state.sortingSteps[0].arrayState)
39-
},
40-
updateBarsCount: (state,action: PayloadAction<number>) => {
41-
state.sortingSteps = getAlgoByName(state.algorithm)(getRandomArray(action.payload,MIN_ARRAY_ELEMENT_VALUE,MAX_ARRAY_ELEMENT_VALUE))
42-
state.currentStepIndex = 0
43-
},
44-
restartSort: (state) => {
45-
state.currentStepIndex = 0
46-
},
47-
regenerateBars: (state) => {
48-
state.currentStepIndex = 0
49-
state.sortingSteps = getAlgoByName(state.algorithm)(getRandomArray(state.sortingSteps[0].arrayState.length, MIN_ARRAY_ELEMENT_VALUE,MAX_ARRAY_ELEMENT_VALUE))
50-
}
51-
}
52-
})
53-
54-
export const { setRunningState ,increaseCurrentStepIndex,updateAlgo, decreaseCurrentStepIndex,updateBarsCount, restartSort, regenerateBars} = sortingSlice.actions
55-
export default sortingSlice.reducer
25+
name: "Sorting",
26+
initialState,
27+
reducers: {
28+
setRunningState: (state, action: PayloadAction<boolean>) => {
29+
state.isRunning = action.payload;
30+
},
31+
increaseCurrentStepIndex: (state) => {
32+
if (state.currentStepIndex !== state.sortingSteps.length - 1) {
33+
state.currentStepIndex += 1;
34+
}
35+
},
36+
decreaseCurrentStepIndex: (state) => {
37+
if (state.currentStepIndex !== 0) {
38+
state.currentStepIndex -= 1;
39+
}
40+
},
41+
updateAlgo: (state, action: PayloadAction<string>) => {
42+
state.algorithm = action.payload;
43+
state.sortingSteps = getAlgoByName(state.algorithm)(
44+
state.sortingSteps[0].arrayState
45+
);
46+
},
47+
updateBarsCount: (state, action: PayloadAction<number>) => {
48+
state.sortingSteps = getAlgoByName(state.algorithm)(
49+
getRandomArray(
50+
action.payload,
51+
MIN_ARRAY_ELEMENT_VALUE,
52+
MAX_ARRAY_ELEMENT_VALUE
53+
)
54+
);
55+
state.currentStepIndex = 0;
56+
},
57+
restartSort: (state) => {
58+
state.currentStepIndex = 0;
59+
},
60+
regenerateBars: (state) => {
61+
state.currentStepIndex = 0;
62+
state.sortingSteps = getAlgoByName(state.algorithm)(
63+
getRandomArray(
64+
state.sortingSteps[0].arrayState.length,
65+
MIN_ARRAY_ELEMENT_VALUE,
66+
MAX_ARRAY_ELEMENT_VALUE
67+
)
68+
);
69+
},
70+
},
71+
});
5672

73+
export const {
74+
setRunningState,
75+
increaseCurrentStepIndex,
76+
updateAlgo,
77+
decreaseCurrentStepIndex,
78+
updateBarsCount,
79+
restartSort,
80+
regenerateBars,
81+
} = sortingSlice.actions;
82+
export default sortingSlice.reducer;

src/utils/__tests__/algos.test.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
1-
import {swap} from "../algos"
1+
import swap from "../swap";
2+
import { getAlgoByName } from "../algos";
23

3-
it('changes array [1,2,3] to [3,2,1] by swap at position 0 and 2', () => {
4-
const arr = [1,2,3]
5-
swap(arr, 0,2)
6-
expect(arr).toMatchObject([3,2,1])
7-
})
4+
describe("swap", () => {
5+
it("changes array [1,2,3] to [3,2,1] by swap at position 0 and 2", () => {
6+
const arr = [1, 2, 3];
7+
swap(arr, 0, 2);
8+
expect(arr).toMatchObject([3, 2, 1]);
9+
});
810

9-
it('does nothing if indices to be swapped are out of array bounds', () => {
10-
const arr1 = [1,2,3,4,5]
11-
const arr2 = [1,2,3,4,5]
12-
swap(arr1 , 0 , 5)
13-
swap(arr2 , -1, 2)
14-
expect(arr1).toMatchObject([1,2,3,4,5])
15-
expect(arr2).toMatchObject([1,2,3,4,5])
16-
})
11+
it("does nothing if indices to be swapped are out of array bounds", () => {
12+
const arr1 = [1, 2, 3, 4, 5];
13+
const arr2 = [1, 2, 3, 4, 5];
14+
swap(arr1, 0, 5);
15+
swap(arr2, -1, 2);
16+
expect(arr1).toMatchObject([1, 2, 3, 4, 5]);
17+
expect(arr2).toMatchObject([1, 2, 3, 4, 5]);
18+
});
19+
});
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
import {getRandomArray,getRandomNumber} from "../getRandomArray"
1+
import { getRandomArray, getRandomNumber } from "../exports";
22

3-
describe('getRandomNumber', () => {
4-
it('returns a random number between 10 and 20 when passed as arguments', () =>{
5-
const result = getRandomNumber(10,20)
6-
expect(result).toBeLessThanOrEqual(20)
7-
expect(result).toBeGreaterThanOrEqual(10)
8-
})
3+
describe("getRandomNumber", () => {
4+
it("returns a random number between 10 and 20 when passed as arguments", () => {
5+
const result = getRandomNumber(10, 20);
6+
expect(result).toBeLessThanOrEqual(20);
7+
expect(result).toBeGreaterThanOrEqual(10);
8+
});
99

10-
it('returns the same argument when both end points are same', () =>{
11-
const result = getRandomNumber(15,15)
12-
expect(result).toBe(15)
13-
})
14-
})
10+
it("returns the same argument when both end points are same", () => {
11+
const result = getRandomNumber(15, 15);
12+
expect(result).toBe(15);
13+
});
14+
});

0 commit comments

Comments
 (0)