-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgnome.ts
41 lines (37 loc) · 1.14 KB
/
gnome.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import { compare } from '../../utils/compare'
import { swap } from '../../utils/swap'
/**
* Gnome Sort, also known as Stupid Sort, is a sorting algorithm that is
* conceptually simple but inefficient for large datasets.
*
* Time Complexity
* - Best Case: O(n) - If the list is already sorted, because it only needs
* to traverse the list once.
*
* - Average Case: O(n^2)
*
* - Worst Case: O(n^2) - It happens when list is reversed.
*
* Space complexity: O(1) - it is in place sorting algorithm, which means
* it requires only constact amount of extra space.
*
* It is mainly used for conceptual or educational purposes, due to inneficient
* performance with large datasets.
*
* @param arr passed array of string or numbers
* @returns return sorted array
*/
export function gnomeSort<TElement extends number | string>(arr: TElement[]): TElement[] {
const l = arr.length
let index = 0
while (index < l) {
if (index === 0 || compare(arr[index], arr[index - 1]) >= 0) {
index = index + 1
} else {
swap(arr, index, index - 1)
index = index - 1
}
}
return arr
}
export type GnomeSortFn = typeof gnomeSort