Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 44 additions & 5 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,43 @@
* [Xor Cipher](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/ciphers/xor_cipher.ts)

## Data Structures
* [Array Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/array_queue.ts)
* [Linked Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/linked_queue.ts)
* [Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue.ts)
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack.ts)
* [Stack Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack_queue.ts)
* List
* [Doubly Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/doubly_linked_list.ts)
* [Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/linked_list.ts)
* [Singly Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/singly_linked_list.ts)
* Test
* [Doubly Linked List.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/test/doubly_linked_list.test.ts)
* [Linked List](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/test/linked_list.ts)
* [Singly Linked List.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/list/test/singly_linked_list.test.ts)
* Map
* [Hash Map](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/map/hash_map.ts)
* [Map](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/map/map.ts)
* Test
* [Hash Map.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/map/test/hash_map.test.ts)
* Queue
* [Array Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/array_queue.ts)
* [Linked Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/linked_queue.ts)
* [Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/queue.ts)
* [Stack Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/stack_queue.ts)
* Test
* [Array Queue.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/test/array_queue.test.ts)
* [Linked Queue.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/test/linked_queue.test.ts)
* [Queue](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/test/queue.ts)
* [Stack Queue.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/queue/test/stack_queue.test.ts)
* Set
* [Hash Map Set](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/set/hash_map_set.ts)
* [Map Set](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/set/map_set.ts)
* [Set](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/set/set.ts)
* Stack
* [Linked List Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack/linked_list_stack.ts)
* [Stack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack/stack.ts)
* Test
* [Linked List Stack.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack/test/linked_list_stack.test.ts)
* [Stack.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/stack/test/stack.test.ts)
* Tree
* [Binary Search Tree](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tree/binary_search_tree.ts)
* Test
* [Binary Search Tree.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/data_structures/tree/test/binary_search_tree.test.ts)

## Dynamic Programming
* [Knapsack](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/dynamic_programming/knapsack.ts)
Expand All @@ -18,9 +50,11 @@
* [Armstrong Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/armstrong_number.ts)
* [Binary Convert](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/binary_convert.ts)
* [Calculate Mean](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/calculate_mean.ts)
* [Calculatemedian](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/calculatemedian.ts)
* [Degrees To Radians](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/degrees_to_radians.ts)
* [Digit Sum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/digit_sum.ts)
* [Factorial](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factorial.ts)
* [Factors](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/factors.ts)
* [Fibonacci](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/fibonacci.ts)
* [Find Min](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/find_min.ts)
* [Greatest Common Factor](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/greatest_common_factor.ts)
Expand All @@ -29,8 +63,10 @@
* [Is Even](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_even.ts)
* [Is Leap Year](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_leap_year.ts)
* [Is Odd](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_odd.ts)
* [Is Prime](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_prime.ts)
* [Is Square Free](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/is_square_free.ts)
* [Lowest Common Multiple](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/lowest_common_multiple.ts)
* [Number Of Digits](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/number_of_digits.ts)
* [Perfect Square](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/perfect_square.ts)
* [Pronic Number](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/pronic_number.ts)
* [Radians To Degrees](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/radians_to_degrees.ts)
Expand All @@ -40,6 +76,7 @@
* [Hexagonal Numbers.Test](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/series/test/hexagonal_numbers.test.ts)
* [Sieve Of Eratosthenes](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/sieve_of_eratosthenes.ts)
* [Signum](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/signum.ts)
* [Zellers Congruence](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/maths/zellers_congruence.ts)

## Other
* [Parse Nested Brackets](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/other/parse_nested_brackets.ts)
Expand All @@ -51,8 +88,10 @@
* [Linear Search](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/search/linear_search.ts)

## Sorts
* [Binaryinsertionsort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/binaryinsertionsort.ts)
* [Bubble Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/bubble_sort.ts)
* [Gnome Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/gnome_sort.ts)
* [Insertion Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/insertion_sort.ts)
* [Merge Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/merge_sort.ts)
* [Quick Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/quick_sort.ts)
* [Selection Sort](https://github.com/TheAlgorithms/TypeScript/blob/HEAD/sorts/selection_sort.ts)
File renamed without changes.
61 changes: 61 additions & 0 deletions sorts/binaryinsertionsort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* Pure Implementation of Binary Search Algorithm
*
* Binary insertion sort is a sorting algorithm similar to insertion sort,
* but instead of using linear search to find the position
* where the element should be inserted, we use binary search.
* Thus, we reduce the number of comparisons for inserting one element from O(N)
* (Time complexity in Insertion Sort) to O(log N).
*
*/

/**
* @function binarySearch
* @description Search the key element in the array from start position to end position.
* @param {Array} array Array of numbers.
* @param {Number} key Value to be searched
* @param {Number} start start index position of array
* @param {Number} end end index position of array
* @return {Number} Position of the key element
*/
const findPosbinarySearch = (array: number[], key: number, start: number, end: number): number => {
if (start === end) {
if (array[start] > key) {
return start;
} else {
return start + 1;
}
}

if (start > end) {
return start;
}

const mid = Math.floor((start + end) / 2);

if (array[mid] < key) {
return findPosbinarySearch(array, key, mid + 1, end);
} else if (array[mid] > key) {
return findPosbinarySearch(array, key, start, mid - 1);
} else {
return mid;
}
}

/**
* @function Binary Insertion Sort
* @param {Array} list List to be sorted.
* @return {Array} The sorted list.
*/
export const binaryInsertionSort = (array: number[]): number[] => {
const totalLength = array.length;
for (let itr = 1; itr < totalLength; itr += 1) {
const key = array[itr];
const indexPosition = findPosbinarySearch(array, key, 0, itr - 1);
array.splice(itr, 1);
array.splice(indexPosition, 0, key);
}
return array;
}

console.log(binaryInsertionSort([5,4, 3, 1, 2]))
8 changes: 8 additions & 0 deletions sorts/test/binaryinsertionsort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { binaryInsertionSort } from "../BinaryInsertionSort";

describe("BinaryInsertionSort", () => {
it("should sort arrays correctly", () => {
expect(binaryInsertionSort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5]);
expect(binaryInsertionSort([7, 9, 4, 3, 5])).toEqual([3, 4, 5, 7, 9]);
});
});