Skip to content

Commit 7e0aff1

Browse files
DarkWarrior703Solot Paulitsvinayak
authored
Added JSDoc documentation to some sorting functions and added TimoSort (#406)
* Added TimSort Co-authored-by: Solot Paul <[email protected]> Co-authored-by: vinayak <[email protected]>
1 parent abce4b7 commit 7e0aff1

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

Sorts/TimSort.js

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* @function Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort,
3+
* designed to perform well on many kinds of real-world data.
4+
* It was implemented by Tim Peters in 2002 for use in the Python programming language.
5+
* It is also used to sort arrays of non-primitive type in Java SE 7,
6+
* on the Android platform, in GNU Octave, on V8, Swift and Rust.
7+
* 1) It sorts small partitions using Insertion Sort.
8+
* 2) Merges the partition using Merge Sort.
9+
* @see [Timsort](https://en.wikipedia.org/wiki/Timsort)
10+
* @param {Array} array
11+
*/
12+
13+
const Timsort = (array) => {
14+
// Default size of a partition
15+
const RUN = 32
16+
const n = array.length
17+
// Sorting the partitions using Insertion Sort
18+
for (let i = 0; i < n; i += RUN) {
19+
InsertionSort(array, i, Math.min(i + RUN - 1, n - 1))
20+
}
21+
for (let size = RUN; size < n; size *= 2) {
22+
for (let left = 0; left < n; left += 2 * size) {
23+
const mid = left + size - 1
24+
const right = Math.min(left + 2 * size - 1, n - 1)
25+
Merge(array, left, mid, right)
26+
}
27+
}
28+
}
29+
30+
/**
31+
* @function performs insertion sort on the partition
32+
* @param {Array} array array to be sorted
33+
* @param {Number} left left index of partiton
34+
* @param {Number} right right index of partition
35+
*/
36+
37+
const InsertionSort = (array, left, right) => {
38+
for (let i = left + 1; i <= right; i++) {
39+
const key = array[i]
40+
let j = i - 1
41+
while (j >= left && array[j] > key) {
42+
array[j + 1] = array[j]
43+
j--
44+
}
45+
array[j + 1] = key
46+
}
47+
}
48+
49+
/**
50+
* @function merges two sorted partitions
51+
* @param {Array} array array to be sorted
52+
* @param {Number} left left index of partition
53+
* @param {Number} mid mid index of partition
54+
* @param {Number} right right index of partition
55+
*/
56+
57+
const Merge = (array, left, mid, right) => {
58+
if (mid >= right) return
59+
const len1 = mid - left + 1
60+
const len2 = right - mid
61+
const larr = Array(len1)
62+
const rarr = Array(len2)
63+
for (let i = 0; i < len1; i++) {
64+
larr[i] = array[left + i]
65+
}
66+
for (let i = 0; i < len2; i++) {
67+
rarr[i] = array[mid + 1 + i]
68+
}
69+
let i = 0; let j = 0; let k = left
70+
while (i < larr.length && j < rarr.length) {
71+
if (larr[i] < rarr[j]) {
72+
array[k++] = larr[i++]
73+
} else {
74+
array[k++] = rarr[j++]
75+
}
76+
}
77+
while (i < larr.length) {
78+
array[k++] = larr[i++]
79+
}
80+
while (j < rarr.length) {
81+
array[k++] = rarr[j++]
82+
}
83+
}
84+
85+
/**
86+
* @example Test of Timsort functions.
87+
* Data is randomly generated.
88+
* Prints "RIGHT" if it works as expected,
89+
* otherwise "FAULTY"
90+
*/
91+
(() => {
92+
const size = 1000000
93+
const data = Array(size)
94+
for (let i = 0; i < size; i++) {
95+
data[i] = Math.random() * Number.MAX_SAFE_INTEGER
96+
}
97+
const isSorted = function (array) {
98+
const n = array.length
99+
for (let i = 0; i < n - 1; i++) {
100+
if (array[i] > array[i + 1]) return false
101+
}
102+
return true
103+
}
104+
Timsort(data)
105+
if (isSorted(data)) {
106+
console.log('RIGHT')
107+
} else {
108+
console.log('FAULTY')
109+
}
110+
})()

0 commit comments

Comments
 (0)