Skip to content

Commit 181e14b

Browse files
author
wakidurrahman
committed
feat: variation Recursive
1 parent 46e15bc commit 181e14b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/code-challenges/bubble-sort.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,24 @@ function bubbleSortVariationNormal(arr) {
2626
}
2727

2828
bubbleSortVariationNormal([6, 5, 3, 1, 8, 7, 2, 4]);
29+
30+
// Recursively
31+
const bubbleSortVariationRecursive = function (array, pointer = array.length - 1) {
32+
// Base check
33+
if (pointer === 0) {
34+
return array;
35+
}
36+
37+
for (let i = 0; i < pointer; i++) {
38+
if (array[i] > array[i + 1]) {
39+
let temp = array[i + 1];
40+
array[i + 1] = array[i];
41+
array[i] = temp;
42+
}
43+
}
44+
45+
// Recursive call on smaller portion of the array
46+
return bubbleSortVariationRecursive(array, pointer - 1);
47+
};
48+
49+
bubbleSortVariationRecursive([6, 5, 3, 1, 8, 7, 2, 4]);

0 commit comments

Comments
 (0)