diff --git a/DIRECTORY.md b/DIRECTORY.md index 185bae95..066d27f8 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -2,9 +2,11 @@ ## Backtracking * [All Combinations Of Size K](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/all_combinations_of_size_k.ts) * [Generateparentheses](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/generateparentheses.ts) + * [Generatepermutations](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/generatepermutations.ts) * Test * [All Combinations Of Size K.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/all_combinations_of_size_k.test.ts) * [Generateparentheses.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generateparentheses.test.ts) + * [Generatepermutations.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/backtracking/test/generatepermutations.test.ts) ## Bit Manipulation * [Add Binary](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/bit_manipulation/add_binary.ts) @@ -156,6 +158,8 @@ ## Search * [Binary Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/binary_search.ts) + * [Exponential Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/exponential_search.ts) + * [Fibonacci Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/fibonacci_search.ts) * [Interpolation Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/interpolation_search.ts) * [Jump Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/jump_search.ts) * [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts) diff --git a/backtracking/generatepermutations.ts b/backtracking/generatepermutations.ts new file mode 100644 index 00000000..c6196574 --- /dev/null +++ b/backtracking/generatepermutations.ts @@ -0,0 +1,38 @@ +/* + * Problem Statement: Generate all distinct permutations of an array (all permutations should be in sorted order); + * + * What is permutations? + * - Permutation means possible arrangements in a set (here it is an array); + * + * Reference to know more about permutations: + * - https://www.britannica.com/science/permutation + * + */ + +const swap = (arr: T[], i: number, j: number): T[] => { + const newArray: T[] = [...arr] + + const temp: T = newArray[i] + newArray[i] = newArray[j] + newArray[j] = temp + + return newArray +} + +const permutations = (arr: T[]): T[][] => { + const P: T[][] = [] + const permute = (arr: T[], low: number, high: number): T[][] => { + if (low === high) { + P.push([...arr]) + return P + } + for (let i = low; i <= high; i++) { + arr = swap(arr, low, i) + permute(arr, low + 1, high) + } + return P + } + return permute(arr, 0, arr.length - 1) +} + +export { permutations } diff --git a/backtracking/test/generatepermutations.test.ts b/backtracking/test/generatepermutations.test.ts new file mode 100644 index 00000000..27cf52d2 --- /dev/null +++ b/backtracking/test/generatepermutations.test.ts @@ -0,0 +1,39 @@ +import { permutations } from '../generatepermutations' + +const factorial = (n: number): number => { + if (n === 0 || n === 1) { + return 1 + } + return n * factorial(n - 1) +} + +describe('Permutations', () => { + it('Permutations of [a]', () => { + const perms = permutations(['a']) + expect(perms).toHaveLength(factorial(1)) + expect(perms).toContainEqual(['a']) + }) + + it('Permutations of [true, false]', () => { + const perms = permutations([true, false]) + expect(perms).toHaveLength(factorial(2)) + expect(perms).toContainEqual([true, false]) + expect(perms).toContainEqual([false, true]) + }) + + it('Permutations of [1, 2, 3]', () => { + const perms = permutations([1, 2, 3]) + expect(perms).toHaveLength(factorial(3)) + expect(perms).toContainEqual([1, 2, 3]) + expect(perms).toContainEqual([1, 3, 2]) + expect(perms).toContainEqual([2, 1, 3]) + expect(perms).toContainEqual([2, 3, 1]) + expect(perms).toContainEqual([3, 1, 2]) + expect(perms).toContainEqual([3, 2, 1]) + }) + + it('Permutation counts across larger input arrays', () => { + expect(permutations([1, 2, 3, 4, 5, 6, 7, 8])).toHaveLength(factorial(8)) + expect(permutations([1, 2, 3, 4, 5, 6, 7, 8, 9])).toHaveLength(factorial(9)) + }) +})