Skip to content

Commit

Permalink
refactor: Add generic types naming convention (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
ariskemper authored Nov 23, 2023
1 parent 2b793ba commit 94c728c
Show file tree
Hide file tree
Showing 20 changed files with 64 additions and 42 deletions.
8 changes: 8 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ module.exports = {
'prettier/prettier': 'warn',
'semi': ['error', 'never'],
'quotes': [2, 'single'],
'@typescript-eslint/naming-convention': [
'error',
{
selector: 'typeParameter',
format: ['PascalCase'],
custom: { regex: '^T[A-Z]', match: true },
},
],

// ES6+
'prefer-const': 'error', // Suggest using const
Expand Down
13 changes: 9 additions & 4 deletions src/sort/bitonic/bitonic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { compare, swap } from '../../utils'
* @param ascending direction of sort
* @returns sorted array
*/
export function bitonicSort<T extends string | number>(arr: T[], ascending: boolean = true): T[] {
export function bitonicSort<TElement extends string | number>(arr: TElement[], ascending: boolean = true): TElement[] {
const n = arr.length
if (n <= 1) return arr

Expand All @@ -43,14 +43,14 @@ export function bitonicSort<T extends string | number>(arr: T[], ascending: bool
return arr
}

function compareAndSwap<T extends string | number>(arr: T[], i: number, j: number, ascending: boolean) {
function compareAndSwap<TElement extends string | number>(arr: TElement[], i: number, j: number, ascending: boolean) {
if (ascending ? compare(arr[i], arr[j]) > 0 : compare(arr[i], arr[j]) < 0) {
arr = swap(arr, i, j)
}
return arr
}

function bitonicMerge<T extends string | number>(arr: T[], low: number, cnt: number, ascending: boolean) {
function bitonicMerge<TElement extends string | number>(arr: TElement[], low: number, cnt: number, ascending: boolean) {
if (cnt > 1) {
const k = Math.floor(cnt / 2)
for (let i = low; i < low + k; i++) {
Expand All @@ -61,7 +61,12 @@ function bitonicMerge<T extends string | number>(arr: T[], low: number, cnt: num
}
}

function bitonicSortRec<T extends string | number>(arr: T[], low: number, cnt: number, ascending: boolean) {
function bitonicSortRec<TElement extends string | number>(
arr: TElement[],
low: number,
cnt: number,
ascending: boolean
) {
if (cnt > 1) {
const k = Math.floor(cnt / 2)
bitonicSortRec(arr, low, k, !ascending)
Expand Down
6 changes: 3 additions & 3 deletions src/sort/bogo/bogo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ import { swap } from '../../utils/swap'
* @param arr unsorted array
* @returns sorted array
*/
export function bogoSort<T extends string | number>(arr: T[]): T[] {
export function bogoSort<TElement extends string | number>(arr: TElement[]): TElement[] {
while (isNotSorted(arr)) {
shuffle(arr)
}
return arr
}

function shuffle<T extends string | number>(arr: T[]): void {
function shuffle<TElement extends string | number>(arr: TElement[]): void {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
swap(arr, i, j)
}
}

function isNotSorted<T extends string | number>(arr: T[]): boolean {
function isNotSorted<TElement extends string | number>(arr: TElement[]): boolean {
for (let i = 1; i < arr.length; i++) {
if (compare(arr[i - 1], arr[i]) > 0) {
return true
Expand Down
2 changes: 1 addition & 1 deletion src/sort/bubble/bubble.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { compare, swap } from '../../utils'
* @param arr array which should be sorted
* @returns sorted array
*/
export function bubbleSort<T extends string | number>(arr: T[]): T[] {
export function bubbleSort<TElement extends string | number>(arr: TElement[]): TElement[] {
let len = arr.length
let swapped: boolean

Expand Down
9 changes: 7 additions & 2 deletions src/sort/cocktail-shaker/cocktail-shaker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { compare, swap } from '../../utils'
* @param arr unsorted array
* @returns sorted array
*/
export function cocktailShakerSort<T extends string | number>(arr: T[]): T[] {
export function cocktailShakerSort<TElement extends string | number>(arr: TElement[]): TElement[] {
let start = 0
let end = arr.length - 1

Expand All @@ -38,7 +38,12 @@ export function cocktailShakerSort<T extends string | number>(arr: T[]): T[] {
return arr
}

function traverse<T extends string | number>(arr: T[], start: number, end: number, direction: number): T[] {
function traverse<TElement extends string | number>(
arr: TElement[],
start: number,
end: number,
direction: number
): TElement[] {
if (direction === 1) {
for (let i = start; i < end; i++) {
if (compare(arr[i], arr[i + 1]) > 0) {
Expand Down
2 changes: 1 addition & 1 deletion src/sort/combo/combo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { compare, swap } from '../../utils'
* @param arr unsorted array of strings or numbers
* @returns sorted array
*/
export function comboSort<T extends string | number>(arr: T[]): T[] {
export function comboSort<TElement extends string | number>(arr: TElement[]): TElement[] {
let gap = arr.length
if (gap <= 1) return arr
let swapped = false
Expand Down
2 changes: 1 addition & 1 deletion src/sort/cycle/cycle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import { compare } from '../../utils'
* @param arr unsorted array
* @returns sorted array
*/
export function cycleSort<T extends number | string>(arr: T[]): T[] {
export function cycleSort<TElement extends number | string>(arr: TElement[]): TElement[] {
const len = arr.length

if (len === 0) return arr
Expand Down
2 changes: 1 addition & 1 deletion src/sort/gnome/gnome.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { swap } from '../../utils/swap'
* @param arr passed array of string or numbers
* @returns return sorted array
*/
export function gnomeSort<T extends number | string>(arr: T[]): T[] {
export function gnomeSort<TElement extends number | string>(arr: TElement[]): TElement[] {
const l = arr.length
let index = 0

Expand Down
8 changes: 6 additions & 2 deletions src/sort/heap/heap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ import { compare, swap } from '../../utils'
* @param n number of elements in heap
* @returns sorted array
*/
export function heapSort<T extends number | string>(arr: T[], low: number = 0, n: number = arr.length): T[] {
export function heapSort<TElement extends number | string>(
arr: TElement[],
low: number = 0,
n: number = arr.length
): TElement[] {
// Build heap (rearrange array)
for (let i = Math.floor(n / 2) - 1; i >= low; i--) {
heapify(arr, n, i)
Expand All @@ -43,7 +47,7 @@ export function heapSort<T extends number | string>(arr: T[], low: number = 0, n
return arr
}

function heapify<T extends number | string>(arr: T[], n: number, i: number) {
function heapify<TElement extends number | string>(arr: TElement[], n: number, i: number) {
let largest = i // Initialize largest as root
const l = 2 * i + 1 // left = 2*i + 1
const r = 2 * i + 2 // right = 2*i + 2
Expand Down
6 changes: 3 additions & 3 deletions src/sort/insertion/insertion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import { compare, swap } from '../../utils'
* @param high ending index
* @returns sorted array
*/
export function insertionSort<T extends number | string>(
arr: T[],
export function insertionSort<TElement extends number | string>(
arr: TElement[],
low: number = 0,
high: number = arr.length - 1
): T[] {
): TElement[] {
for (let i = low + 1; i <= high; i++) {
const key = arr[i]
let j = i - 1
Expand Down
6 changes: 3 additions & 3 deletions src/sort/intro/intro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ import { insertionSort } from '../insertion/insertion'
* @param arr unsorted array
* @returns sorted array
*/
export function introSort<T extends string | number>(arr: T[]): T[] {
export function introSort<TElement extends string | number>(arr: TElement[]): TElement[] {
const copyArr = [...arr]
sort(copyArr)
return copyArr
}

function sort<T extends string | number>(
arr: T[],
function sort<TElement extends string | number>(
arr: TElement[],
low: number = 0,
high: number = arr.length - 1,
depthLimit = 2 * Math.floor(Math.log2(arr.length))
Expand Down
6 changes: 3 additions & 3 deletions src/sort/merge/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { compare } from '../../utils'
* @param arr unsorted array
* @returns sorted array
*/
export function mergeSort<T extends number | string>(arr: T[]): T[] {
export function mergeSort<TElement extends number | string>(arr: TElement[]): TElement[] {
if (arr.length <= 1) {
return arr
}
Expand All @@ -35,8 +35,8 @@ export function mergeSort<T extends number | string>(arr: T[]): T[] {
return merge(mergeSort(left), mergeSort(right))
}

function merge<T extends number | string>(left: T[], right: T[]): T[] {
const resultArray: T[] = []
function merge<TElement extends number | string>(left: TElement[], right: TElement[]): TElement[] {
const resultArray: TElement[] = []
let i = 0
let j = 0

Expand Down
8 changes: 4 additions & 4 deletions src/sort/random-quick/random-quick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ import { compare, swap } from '../../utils'
* @param right
* @returns sorted array of elements (may be strings or numbers)
*/
export function randomQuickSort<T extends number | string>(
arr: T[],
export function randomQuickSort<TElement extends number | string>(
arr: TElement[],
left: number = 0,
right: number = arr.length - 1
): T[] {
): TElement[] {
if (left < right) {
const pivotIndex = partition(arr, left, right)
randomQuickSort(arr, left, pivotIndex - 1)
Expand All @@ -40,7 +40,7 @@ export function randomQuickSort<T extends number | string>(
return arr
}

function partition<T extends number | string>(arr: T[], left: number, right: number): number {
function partition<TElement extends number | string>(arr: TElement[], left: number, right: number): number {
const pivotIndex = Math.floor(Math.random() * (right - left + 1)) + left
const pivot = arr[pivotIndex]
swap(arr, right, pivotIndex)
Expand Down
2 changes: 1 addition & 1 deletion src/sort/selection/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { compare, swap } from '../../utils'
* @param arr array which should be sorted
* @returns sorted array
*/
export function selectionSort<T extends number | string>(arr: T[]): T[] {
export function selectionSort<TElement extends number | string>(arr: TElement[]): TElement[] {
const len = arr.length

for (let i = 0; i < len - 1; i++) {
Expand Down
2 changes: 1 addition & 1 deletion src/sort/shell/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { compare, swap } from '../../utils'
* @param arr unsorted array
* @returns sorted array
*/
export function shellSort<T extends number | string>(arr: T[]): T[] {
export function shellSort<TElement extends number | string>(arr: TElement[]): TElement[] {
const size = arr.length

for (let gap = Math.floor(size / 2); gap > 0; gap = Math.floor(gap / 2)) {
Expand Down
14 changes: 7 additions & 7 deletions src/sort/tree/tree-node.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
export class TreeNode<T extends number | string> {
value: T
left: TreeNode<T> | null = null
right: TreeNode<T> | null = null
export class TreeNode<TElement extends number | string> {
value: TElement
left: TreeNode<TElement> | null = null
right: TreeNode<TElement> | null = null

constructor(value: T) {
constructor(value: TElement) {
this.value = value
}

insert(value: T, cmpFunc: (a: T, b: T) => number) {
insert(value: TElement, cmpFunc: (a: TElement, b: TElement) => number) {
if (cmpFunc(value, this.value) < 0) {
if (this.left === null) {
this.left = new TreeNode(value)
Expand All @@ -23,7 +23,7 @@ export class TreeNode<T extends number | string> {
}
}

inOrderTraversal(arr: T[]) {
inOrderTraversal(arr: TElement[]) {
if (this.left !== null) {
this.left.inOrderTraversal(arr)
}
Expand Down
4 changes: 2 additions & 2 deletions src/sort/tree/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import { TreeNode } from './tree-node'
* @param arr unsorted array
* @returns sorted array
*/
export function treeSort<T extends number | string>(arr: T[]): T[] {
export function treeSort<TElement extends number | string>(arr: TElement[]): TElement[] {
if (arr.length === 0) return []

const root = new TreeNode(arr[0])
Expand All @@ -38,7 +38,7 @@ export function treeSort<T extends number | string>(arr: T[]): T[] {
root.insert(arr[i], compare)
}

const sortedArray: T[] = []
const sortedArray: TElement[] = []
root.inOrderTraversal(sortedArray)

return sortedArray
Expand Down
2 changes: 1 addition & 1 deletion src/utils/compare.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function compare<SortT>(a: SortT, b: SortT): number {
export function compare<TSort>(a: TSort, b: TSort): number {
if (typeof a === 'string' && typeof b === 'string') {
return a.localeCompare(b)
} else if (typeof a === 'number' && typeof b === 'number') {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/partition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { swap } from './swap'

export function partition<T>(arr: T[], low: number, high: number): number {
export function partition<TElement>(arr: TElement[], low: number, high: number): number {
const pivot = arr[high]
let i = low

Expand Down
2 changes: 1 addition & 1 deletion src/utils/swap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export function swap<T>(arr: T[], i: number, j: number): T[] {
export function swap<TElement>(arr: TElement[], i: number, j: number): TElement[] {
;[arr[i], arr[j]] = [arr[j], arr[i]]
return arr
}

0 comments on commit 94c728c

Please sign in to comment.