Skip to content

Commit 83b840d

Browse files
committed
Merge branch 'master' of github.com:charliejmoore/Javascript into cycle-sort-tests
2 parents 1346414 + 6607f27 commit 83b840d

File tree

11 files changed

+12691
-33
lines changed

11 files changed

+12691
-33
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Problem: Given two numbers, n and k, make all unique combinations of k numbers from 1 to n and in sorted order
3+
4+
What is combinations?
5+
- Combinations is selecting items froms a collections without considering order of selection
6+
7+
Example:
8+
- We have an apple, a banana, and a jackfruit
9+
- We have three objects, and need to choose two items, then combinations will be
10+
11+
1. Apple & Banana
12+
2. Apple & Jackfruit
13+
3. Banana & Jackfruit
14+
15+
To read more about combinations, you can visit the following link:
16+
- https://betterexplained.com/articles/easy-permutations-and-combinations/
17+
18+
Solution:
19+
- We will be using backtracking to solve this questions
20+
- Take one element, and make all them combinations for k-1 elements
21+
- Once we get all combinations of that element, pop it and do same for next element
22+
*/
23+
24+
class Combinations {
25+
constructor (n, k) {
26+
this.n = n
27+
this.k = k
28+
this.combinationArray = [] // will be used for storing current combination
29+
}
30+
31+
findCombinations (high = this.n, total = this.k, low = 1) {
32+
if (total === 0) {
33+
console.log(this.combinationArray)
34+
return
35+
}
36+
for (let i = low; i <= high; i++) {
37+
this.combinationArray.push(i)
38+
this.findCombinations(high, total - 1, i + 1)
39+
this.combinationArray.pop()
40+
}
41+
}
42+
}
43+
44+
export { Combinations }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { Combinations } from '../AllCombinationsOfSizeK'
2+
3+
describe('AllCombinationsOfSizeK', () => {
4+
it('should return 3x2 matrix solution for n = 3 and k = 2', () => {
5+
const test1 = new Combinations(3, 2)
6+
expect(test1.findCombinations).toEqual([[1, 2], [1, 3], [2, 3]])
7+
})
8+
9+
it('should return 6x2 matrix solution for n = 3 and k = 2', () => {
10+
const test2 = new Combinations(4, 2)
11+
expect(test2.findCombinations).toEqual([[1, 2], [1, 3], [1, 4], [2, 3], [2, 4], [3, 4]])
12+
})
13+
})

DIRECTORY.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
## Backtracking
3+
* [AllCombinationsOfSizeK](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/AllCombinationsOfSizeK.js)
34
* [GeneratePermutations](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/GeneratePermutations.js)
45
* [KnightTour](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/KnightTour.js)
56
* [NQueen](https://github.com/TheAlgorithms/Javascript/blob/master/Backtracking/NQueen.js)
@@ -156,6 +157,7 @@
156157
* [IsDivisible](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsDivisible.js)
157158
* [IsEven](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/IsEven.js)
158159
* [isOdd](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/isOdd.js)
160+
* [LeapYear](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/LeapYear.js)
159161
* [Mandelbrot](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/Mandelbrot.js)
160162
* [MatrixExponentiationRecursive](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixExponentiationRecursive.js)
161163
* [MatrixMultiplication](https://github.com/TheAlgorithms/Javascript/blob/master/Maths/MatrixMultiplication.js)

Maths/LeapYear.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* isLeapYear :: Number -> Boolean
3+
*
4+
* Check if a year is a leap year or not. A leap year is a year which has 366 days.
5+
* For the extra +1 day the February month contains 29 days instead of 28 days.
6+
*
7+
* The logic behind the leap year is-
8+
* 1. If the year is divisible by 400 then it is a leap year.
9+
* 2. If it is not divisible by 400 but divisible by 100 then it is not a leap year.
10+
* 3. If the year is not divisible by 400 but not divisible by 100 and divisible by 4 then a leap year.
11+
* 4. Other cases except the describing ones are not a leap year.
12+
*
13+
* @param {number} year
14+
* @returns {boolean} true if this is a leap year, false otherwise.
15+
*/
16+
export const isLeapYear = (year) => {
17+
if (year % 400 === 0) return true
18+
if (year % 100 === 0) return false
19+
if (year % 4 === 0) return true
20+
21+
return false
22+
}

Maths/test/LeapYear.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { isLeapYear } from '../LeapYear'
2+
3+
describe('Leap Year', () => {
4+
it('Should return true on the year 2000', () => {
5+
expect(isLeapYear(2000)).toBe(true)
6+
})
7+
it('Should return false on the year 2001', () => {
8+
expect(isLeapYear(2001)).toBe(false)
9+
})
10+
it('Should return false on the year 2002', () => {
11+
expect(isLeapYear(2002)).toBe(false)
12+
})
13+
it('Should return false on the year 2003', () => {
14+
expect(isLeapYear(2003)).toBe(false)
15+
})
16+
it('Should return false on the year 2004', () => {
17+
expect(isLeapYear(2004)).toBe(true)
18+
})
19+
it('Should return false on the year 1900', () => {
20+
expect(isLeapYear(1900)).toBe(false)
21+
})
22+
})

Sorts/BucketSort.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
2+
[Wikipedia says](https://en.wikipedia.org/wiki/Bucket_sort#:~:text=Bucket%20sort%2C%20or%20bin%20sort,applying%20the%20bucket%20sorting%20algorithm.&text=Sort%20each%20non%2Dempty%20bucket.): Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
33
elements of an array into a number of buckets. Each bucket is then sorted individually, either using
44
a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a
55
distribution sort, and is a cousin of radix sort in the most to least significant digit flavour.
@@ -11,6 +11,14 @@ Time Complexity of Solution:
1111
Best Case O(n); Average Case O(n); Worst Case O(n)
1212
1313
*/
14+
15+
/**
16+
* bucketSort returns an array of numbers sorted in increasing order.
17+
*
18+
* @param {number[]} list The array of numbers to be sorted.
19+
* @param {number} size The size of the buckets used. If not provided, size will be 5.
20+
* @return {number[]} An array of numbers sorted in increasing order.
21+
*/
1422
function bucketSort (list, size) {
1523
if (undefined === size) {
1624
size = 5
@@ -45,20 +53,12 @@ function bucketSort (list, size) {
4553
const sorted = []
4654
// now sort every bucket and merge it to the sorted list
4755
for (let iBucket = 0; iBucket < buckets.length; iBucket++) {
48-
const arr = buckets[iBucket].sort()
56+
const arr = buckets[iBucket].sort((a, b) => a - b)
4957
for (let iSorted = 0; iSorted < arr.length; iSorted++) {
5058
sorted.push(arr[iSorted])
5159
}
5260
}
5361
return sorted
5462
}
5563

56-
// Testing
57-
const arrOriginal = [5, 6, 7, 8, 1, 2, 12, 14]
58-
// > bucketSort(arrOriginal)
59-
// [1, 2, 5, 6, 7, 8, 12, 14]
60-
// Array before Sort
61-
console.log(arrOriginal)
62-
const arrSorted = bucketSort(arrOriginal)
63-
// Array after sort
64-
console.log(arrSorted)
64+
export { bucketSort }

Sorts/CombSort.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
* Wikipedia: https://en.wikipedia.org/wiki/Comb_sort
1717
*/
1818

19+
/**
20+
* combSort returns an array of numbers sorted in increasing order.
21+
*
22+
* @param {number[]} list The array of numbers to sort.
23+
* @return {number[]} The array of numbers sorted in increasing order.
24+
*/
1925
function combSort (list) {
2026
if (list.length === 0) {
2127
return list
@@ -43,14 +49,4 @@ function combSort (list) {
4349
return list
4450
}
4551

46-
/**
47-
* Implementation of Comb Sort
48-
*/
49-
const array = [5, 6, 7, 8, 1, 2, 12, 14]
50-
// Before Sort
51-
console.log('\n- Before Sort | Implementation of Comb Sort -')
52-
console.log(array)
53-
// After Sort
54-
console.log('- After Sort | Implementation of Comb Sort -')
55-
console.log(combSort(array))
56-
console.log('\n')
52+
export { combSort }

Sorts/test/BucketSort.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { bucketSort } from '../BucketSort'
2+
3+
describe('Tests for bucketSort function', () => {
4+
it('should correctly sort an input list that is sorted backwards', () => {
5+
const array = [5, 4, 3, 2, 1]
6+
expect(bucketSort(array)).toEqual([1, 2, 3, 4, 5])
7+
})
8+
9+
it('should correctly sort an input list that is unsorted', () => {
10+
const array = [15, 24, 3, 2224, 1]
11+
expect(bucketSort(array)).toEqual([1, 3, 15, 24, 2224])
12+
})
13+
14+
describe('Variations of input array lengths', () => {
15+
it('should return an empty list with the input list is an empty list', () => {
16+
expect(bucketSort([])).toEqual([])
17+
})
18+
19+
it('should correctly sort an input list of length 1', () => {
20+
expect(bucketSort([100])).toEqual([100])
21+
})
22+
23+
it('should correctly sort an input list of an odd length', () => {
24+
expect(bucketSort([101, -10, 321])).toEqual([-10, 101, 321])
25+
})
26+
27+
it('should correctly sort an input list of an even length', () => {
28+
expect(bucketSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
29+
})
30+
})
31+
32+
describe('Variations of input array elements', () => {
33+
it('should correctly sort an input list that contains only positive numbers', () => {
34+
expect(bucketSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
35+
})
36+
37+
it('should correctly sort an input list that contains only negative numbers', () => {
38+
expect(bucketSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
39+
})
40+
41+
it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
42+
expect(bucketSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
43+
})
44+
45+
it('should correctly sort an input list that contains only whole numbers', () => {
46+
expect(bucketSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
47+
})
48+
49+
it('should correctly sort an input list that contains only decimal numbers', () => {
50+
expect(bucketSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
51+
})
52+
53+
it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
54+
expect(bucketSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
55+
})
56+
57+
it('should correctly sort an input list that contains only fractional numbers', () => {
58+
expect(bucketSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
59+
})
60+
61+
it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
62+
expect(bucketSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
63+
})
64+
65+
it('should correctly sort an input list that contains duplicates', () => {
66+
expect(bucketSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
67+
})
68+
})
69+
})

Sorts/test/CombSort.test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { combSort } from '../CombSort'
2+
3+
describe('combSort function', () => {
4+
it('should correctly sort an input list that is sorted backwards', () => {
5+
const array = [5, 4, 3, 2, 1]
6+
expect(combSort(array)).toEqual([1, 2, 3, 4, 5])
7+
})
8+
9+
it('should correctly sort an input list that is unsorted', () => {
10+
const array = [15, 24, 3, 2224, 1]
11+
expect(combSort(array)).toEqual([1, 3, 15, 24, 2224])
12+
})
13+
14+
describe('Variations of input array lengths', () => {
15+
it('should return an empty list with the input list is an empty list', () => {
16+
expect(combSort([])).toEqual([])
17+
})
18+
19+
it('should correctly sort an input list of length 1', () => {
20+
expect(combSort([100])).toEqual([100])
21+
})
22+
23+
it('should correctly sort an input list of an odd length', () => {
24+
expect(combSort([101, -10, 321])).toEqual([-10, 101, 321])
25+
})
26+
27+
it('should correctly sort an input list of an even length', () => {
28+
expect(combSort([40, 42, 56, 45, 12, 3])).toEqual([3, 12, 40, 42, 45, 56])
29+
})
30+
})
31+
32+
describe('Variations of input array elements', () => {
33+
it('should correctly sort an input list that contains only positive numbers', () => {
34+
expect(combSort([50, 33, 11, 2])).toEqual([2, 11, 33, 50])
35+
})
36+
37+
it('should correctly sort an input list that contains only negative numbers', () => {
38+
expect(combSort([-1, -21, -2, -35])).toEqual([-35, -21, -2, -1])
39+
})
40+
41+
it('should correctly sort an input list that contains only a mix of positive and negative numbers', () => {
42+
expect(combSort([-40, 42, 56, -45, 12, -3])).toEqual([-45, -40, -3, 12, 42, 56])
43+
})
44+
45+
it('should correctly sort an input list that contains only whole numbers', () => {
46+
expect(combSort([11, 3, 12, 4, -15])).toEqual([-15, 3, 4, 11, 12])
47+
})
48+
49+
it('should correctly sort an input list that contains only decimal numbers', () => {
50+
expect(combSort([1.0, 1.42, 2.56, 33.45, 13.12, 2.3])).toEqual([1.0, 1.42, 2.3, 2.56, 13.12, 33.45])
51+
})
52+
53+
it('should correctly sort an input list that contains only a mix of whole and decimal', () => {
54+
expect(combSort([32.40, 12.42, 56, 45, 12, 3])).toEqual([3, 12, 12.42, 32.40, 45, 56])
55+
})
56+
57+
it('should correctly sort an input list that contains only fractional numbers', () => {
58+
expect(combSort([0.98, 0.4259, 0.56, -0.456, -0.12, 0.322])).toEqual([-0.456, -0.12, 0.322, 0.4259, 0.56, 0.98])
59+
})
60+
61+
it('should correctly sort an input list that contains only a mix of whole, decimal, and fractional', () => {
62+
expect(combSort([-40, -0.222, 5.6, -4.5, 12, 0.333])).toEqual([-40, -4.5, -0.222, 0.333, 5.6, 12])
63+
})
64+
65+
it('should correctly sort an input list that contains duplicates', () => {
66+
expect(combSort([4, 3, 4, 2, 1, 2])).toEqual([1, 2, 2, 3, 4, 4])
67+
})
68+
})
69+
})

0 commit comments

Comments
 (0)