From 5d0e476622d4e32d6145a408ea24eb7981263080 Mon Sep 17 00:00:00 2001 From: wakidurrahman Date: Tue, 13 Jun 2023 17:13:58 +0900 Subject: [PATCH 1/2] feat: implement problem solving --- src/hackerrank/problem-solving/algorithms-011-020.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/hackerrank/problem-solving/algorithms-011-020.js b/src/hackerrank/problem-solving/algorithms-011-020.js index 11c334b..b4d87b8 100644 --- a/src/hackerrank/problem-solving/algorithms-011-020.js +++ b/src/hackerrank/problem-solving/algorithms-011-020.js @@ -54,3 +54,9 @@ function gradingStudents(grades) { } return count; } + +/** + * 013: HackerLand University has the following grading policy: + * + * + */ From e9552091f2662c920ed44594319575ca0bbb18ed Mon Sep 17 00:00:00 2001 From: wakidurrahman Date: Wed, 14 Jun 2023 16:12:48 +0900 Subject: [PATCH 2/2] feat: bubbleSortVariationNormal --- src/code-challenges/bubble-sort.js | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 src/code-challenges/bubble-sort.js diff --git a/src/code-challenges/bubble-sort.js b/src/code-challenges/bubble-sort.js new file mode 100644 index 0000000..9543620 --- /dev/null +++ b/src/code-challenges/bubble-sort.js @@ -0,0 +1,28 @@ +/** + * 001: Bubble Sort: is based on the idea of repeatedly comparing pairs of adjacent elements and + * then swapping their positions if they are in the wrong order. + * Bubble sort is a stable, in-place sort algorithm. + */ + +// Normal +function bubbleSortVariationNormal(arr) { + let swaps; + do { + swaps = false; + for (let i = 0; i < arr.length - 1; i++) { + const element = arr[i]; + console.log(element); + if (arr[i] > arr[i + 1]) { + // start with the first two elements and sort them in ascending order. (Compare the element to check which one is greater). + let temp = arr[i + 1]; // store second element for swap + arr[i + 1] = arr[i]; // Swap first element to second element + arr[i] = temp; // // Swap second element to first element + swaps = true; // true for loop continue until Compare not false condition + } + } + } while (swaps); + + return arr; +} + +bubbleSortVariationNormal([6, 5, 3, 1, 8, 7, 2, 4]);