From ec8cb061abe55ae747bf31fd9be5a6d69fbdd306 Mon Sep 17 00:00:00 2001 From: AminHossain Date: Sat, 19 Feb 2022 16:44:32 +0600 Subject: [PATCH 1/4] BubbleSort enacements for nearly sorted or sorted array and added test cases --- Sorts/test/BubbleSort.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/Sorts/test/BubbleSort.test.js b/Sorts/test/BubbleSort.test.js index fff23721d1..87ff3e15ab 100644 --- a/Sorts/test/BubbleSort.test.js +++ b/Sorts/test/BubbleSort.test.js @@ -6,6 +6,7 @@ describe('bubbleSort', () => { expect(bubbleSort([])).toEqual([]) expect(bubbleSort([1, 2, 3])).toEqual([1, 2, 3]) expect(bubbleSort([5, 6, 7, 8, 1, 2, 12, 14])).toEqual([1, 2, 5, 6, 7, 8, 12, 14]) + expect(bubbleSort([5, 6, 7, 8, 9, 4])).toEqual([4, 5, 6, 7, 8, 9]) }) }) From 7347ff6fb0483cbc6d677bf797ec392dfa62f104 Mon Sep 17 00:00:00 2001 From: AminHossain Date: Sat, 19 Feb 2022 17:06:04 +0600 Subject: [PATCH 2/4] BubbleSort enacements for nearly sorted or sorted array and added test cases --- Sorts/BubbleSort.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 1ed5c70d6e..5ee9ffed40 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -2,6 +2,7 @@ * compares adjacent element and swaps their position * The big O on bubble sort in worst and best case is O(N^2). * Not efficient. +* Somehow if the array is sorted or nearly sorted then we can optimize bubble sort by adding a flag. * * In bubble sort, we keep iterating while something was swapped in * the previous inner-loop iteration. By swapped I mean, in the @@ -17,16 +18,23 @@ */ export function bubbleSort (items) { const length = items.length + let noSwaps - for (let i = (length - 1); i > 0; i--) { + for (let i = length; i > 0; i--) { + //flag for optimisation + noSwaps = true // Number of passes - for (let j = (length - i); j > 0; j--) { + for (let j = 0; j < (i - 1); j++) { // Compare the adjacent positions - if (items[j] < items[j - 1]) { + if (items[j] > items[j + 1]) { // Swap the numbers - [items[j], items[j - 1]] = [items[j - 1], items[j]] + [items[j], items[j + 1]] = [items[j + 1], items[j]] + noSwaps = false } } + if (noSwaps) { + break; + } } return items From 8432a42305475f109ec6dca39af60452aacb58bb Mon Sep 17 00:00:00 2001 From: AminHossain Date: Sat, 19 Feb 2022 17:45:33 +0600 Subject: [PATCH 3/4] Bubble sort requested changes solved --- Sorts/BubbleSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 5ee9ffed40..3c4ad9e087 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -21,7 +21,7 @@ export function bubbleSort (items) { let noSwaps for (let i = length; i > 0; i--) { - //flag for optimisation + //flag for optimization noSwaps = true // Number of passes for (let j = 0; j < (i - 1); j++) { From babfbefef402698b341cba2855fe47774db89568 Mon Sep 17 00:00:00 2001 From: AminHossain Date: Sat, 19 Feb 2022 18:02:55 +0600 Subject: [PATCH 4/4] standard js style issue fixed --- Sorts/BubbleSort.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 3c4ad9e087..202d769a70 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -21,7 +21,7 @@ export function bubbleSort (items) { let noSwaps for (let i = length; i > 0; i--) { - //flag for optimization + // flag for optimization noSwaps = true // Number of passes for (let j = 0; j < (i - 1); j++) { @@ -33,7 +33,7 @@ export function bubbleSort (items) { } } if (noSwaps) { - break; + break } }