From df60f7d1c3a300bce2e64c19c1c0a8160b33839f Mon Sep 17 00:00:00 2001 From: Chanchal Kumar Mishra Date: Sun, 9 Oct 2022 12:20:36 +0530 Subject: [PATCH 1/3] Swapsort algorithm and corresponding tests --- Sorts/SwapSort.js | 31 +++++++++++++++++++++++++++++++ Sorts/test/SwapSort.test.js | 18 ++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 Sorts/SwapSort.js create mode 100644 Sorts/test/SwapSort.test.js diff --git a/Sorts/SwapSort.js b/Sorts/SwapSort.js new file mode 100644 index 0000000000..1c796454c1 --- /dev/null +++ b/Sorts/SwapSort.js @@ -0,0 +1,31 @@ +/** + * @function SwapSort + * @description Swap Sort is an algorithm to find the number of swaps required to sort an array. + Time complexity of Swap Sort Algorithm is O(nlogn). + Auxiliary Space required for Swap Sort Algorithm is O(n). + * @param {Integer[]} items - Array of integers + * @return {Integer} - Number of swaps required to sort the array. + * @see [SwapSort](https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/) + */ + +export function swapSort(items) { + const sortedArray = items.slice() + sortedArray.sort() + const indexMap = {} + for (let i = 0; i < items.length; i++) { + indexMap[items[i]] = i + } + let swaps = 0 + for (let i = 0; i < items.length; i++) { + if (items[i] !== sortedArray[i]) { + const temp = items[i] + items[i] = items[indexMap[sortedArray[i]]] + items[indexMap[sortedArray[i]]] = temp + + indexMap[temp] = indexMap[sortedArray[i]] + indexMap[sortedArray[i]] = i + swaps++ + } + } + return swaps +} diff --git a/Sorts/test/SwapSort.test.js b/Sorts/test/SwapSort.test.js new file mode 100644 index 0000000000..fa1833c6cc --- /dev/null +++ b/Sorts/test/SwapSort.test.js @@ -0,0 +1,18 @@ +import { swapSort } from '../SwapSort' + +describe('SwapSort', () => { + it('should work for empty arrays', () => { + expect(swapSort([])).toEqual(0) + }) + + it('should work for sorted arrays', () => { + expect(swapSort([1, 2, 3, 4, 5, 6])).toEqual(0) + }) + + it('should return correct results', () => { + expect(swapSort([7, 6, 2, 5, 11, 0])).toEqual(2) + expect(swapSort([3, 3, 2, 1, 0])).toEqual(2) + expect(swapSort([3, 0, 2, 1, 9, 8, 7, 6])).toEqual(4) + expect(swapSort([1, 0, 14, 0, 8, 6, 8])).toEqual(3) + }) +}) From 653a254feee71f0fb2fc6f6f1a82e3a776469a83 Mon Sep 17 00:00:00 2001 From: Chanchal Kumar Mishra Date: Sun, 9 Oct 2022 12:27:03 +0530 Subject: [PATCH 2/3] fixing style --- Sorts/SwapSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/SwapSort.js b/Sorts/SwapSort.js index 1c796454c1..e701440259 100644 --- a/Sorts/SwapSort.js +++ b/Sorts/SwapSort.js @@ -8,7 +8,7 @@ * @see [SwapSort](https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/) */ -export function swapSort(items) { +export function swapSort (items) { const sortedArray = items.slice() sortedArray.sort() const indexMap = {} From 55ad8944dca953d6b7db7953ba78cc3e4661e092 Mon Sep 17 00:00:00 2001 From: Chanchal Kumar Mishra Date: Mon, 10 Oct 2022 21:15:48 +0530 Subject: [PATCH 3/3] updating function name --- Sorts/SwapSort.js | 2 +- Sorts/test/SwapSort.test.js | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Sorts/SwapSort.js b/Sorts/SwapSort.js index e701440259..65199d2ffa 100644 --- a/Sorts/SwapSort.js +++ b/Sorts/SwapSort.js @@ -8,7 +8,7 @@ * @see [SwapSort](https://www.geeksforgeeks.org/minimum-number-swaps-required-sort-array/) */ -export function swapSort (items) { +export function minSwapsToSort (items) { const sortedArray = items.slice() sortedArray.sort() const indexMap = {} diff --git a/Sorts/test/SwapSort.test.js b/Sorts/test/SwapSort.test.js index fa1833c6cc..3a5d4f1d5a 100644 --- a/Sorts/test/SwapSort.test.js +++ b/Sorts/test/SwapSort.test.js @@ -1,18 +1,18 @@ -import { swapSort } from '../SwapSort' +import { minSwapsToSort } from '../SwapSort' describe('SwapSort', () => { it('should work for empty arrays', () => { - expect(swapSort([])).toEqual(0) + expect(minSwapsToSort([])).toEqual(0) }) it('should work for sorted arrays', () => { - expect(swapSort([1, 2, 3, 4, 5, 6])).toEqual(0) + expect(minSwapsToSort([1, 2, 3, 4, 5, 6])).toEqual(0) }) it('should return correct results', () => { - expect(swapSort([7, 6, 2, 5, 11, 0])).toEqual(2) - expect(swapSort([3, 3, 2, 1, 0])).toEqual(2) - expect(swapSort([3, 0, 2, 1, 9, 8, 7, 6])).toEqual(4) - expect(swapSort([1, 0, 14, 0, 8, 6, 8])).toEqual(3) + expect(minSwapsToSort([7, 6, 2, 5, 11, 0])).toEqual(2) + expect(minSwapsToSort([3, 3, 2, 1, 0])).toEqual(2) + expect(minSwapsToSort([3, 0, 2, 1, 9, 8, 7, 6])).toEqual(4) + expect(minSwapsToSort([1, 0, 14, 0, 8, 6, 8])).toEqual(3) }) })