From 917a8175bfa723c1b347a31d8c7976deb91c6dc8 Mon Sep 17 00:00:00 2001 From: Ankush263 Date: Thu, 31 Mar 2022 16:15:37 +0530 Subject: [PATCH] Add test case and fix HeapSort Algorithm --- Sorts/HeapSortV2.js | 1 + Sorts/test/HeapSortV2.test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 Sorts/test/HeapSortV2.test.js diff --git a/Sorts/HeapSortV2.js b/Sorts/HeapSortV2.js index 8f0d91ace8..d32859a630 100644 --- a/Sorts/HeapSortV2.js +++ b/Sorts/HeapSortV2.js @@ -38,4 +38,5 @@ export function heapSort (input) { heapRoot(input, 0) } + return input } diff --git a/Sorts/test/HeapSortV2.test.js b/Sorts/test/HeapSortV2.test.js new file mode 100644 index 0000000000..760ba0df8a --- /dev/null +++ b/Sorts/test/HeapSortV2.test.js @@ -0,0 +1,19 @@ +import { heapSort } from '../HeapSortV2' + +test('The heapSort of the array [4, 3, 2, 1] is [1, 2, 3, 4]', () => { + const arr = [4, 3, 2, 1] + const res = heapSort(arr) + expect(res).toEqual([1, 2, 3, 4]) +}) + +test('The heapSort of the array [] is []', () => { + const arr = [] + const res = heapSort(arr) + expect(res).toEqual([]) +}) + +test('The heapSort of the array [41, 31, 32, 31] is [31, 31, 32, 41]', () => { + const arr = [41, 31, 32, 31] + const res = heapSort(arr) + expect(res).toEqual([31, 31, 32, 41]) +})