Skip to content

Commit 2603b22

Browse files
authored
feat: update merge_sort.ts (TheAlgorithms#126)
* Update merge_sort.ts - use JavaScript ES6 syntax - use `let`, `const` instead of `var` - make it more "clean", I supposed * fix: merge_sort.ts and mroe - fix merge_sort with return copy of array - rename merge_sort.ts `MergeSrot` => `mergeSort` - rename quick_sort.ts `QuickSrot` => `quickSort` - rename merge_sort.test.ts `MergeSrot` => `mergeSort` - rename quick_sort.test.ts `QuickSrot` => `quickSort` * fix: update merge_sort.ts * fix: update merge_sort.ts * fix: convert naming of quick_sort.ts * fix: convert naming of quick_sort.ts
1 parent 3787b38 commit 2603b22

File tree

3 files changed

+44
-53
lines changed

3 files changed

+44
-53
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,3 @@
1919
"author": "TheAlgorithms",
2020
"license": "MIT"
2121
}
22-

sorts/merge_sort.ts

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,52 +5,45 @@
55
* @example MergeSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8]
66
* @Complexity_Analysis
77
* Space complexity - O(n)
8-
* Time complexity
8+
* Time complexity
99
* Best case - O(nlogn)
1010
* Worst case - O(nlogn)
1111
* Average case - O(nlogn)
12-
*
13-
* Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation.
14-
* T(n) = 2T(n/2) + O(n)
12+
*
13+
* Merge Sort is a recursive algorithm and time complexity can be expressed as following recurrence relation.
14+
* T(n) = 2T(n/2) + O(n)
1515
* The solution of the above recurrence is O(nLogn).
1616
*/
1717

18-
export const MergeSort = (items: number[]): number[] => {
19-
var halfLength = Math.ceil(items.length / 2);
20-
var low = items.slice(0, halfLength);
21-
var high = items.slice(halfLength);
22-
if (halfLength > 1) {
23-
low = MergeSort(low);
24-
high = MergeSort(high);
25-
}
26-
return merge(low, high);
27-
};
18+
export function mergeSort(array: number[]): number[] {
19+
if (array.length <= 1) return array.slice()
2820

29-
export const merge = (low: number[], high: number[]): number[] => {
30-
let indexLow = 0;
31-
let indexHigh = 0;
32-
let curIndex = 0;
33-
let merged = Array<number>(low.length + high.length);
21+
const midIndex = Math.floor(array.length / 2)
22+
const left = array.slice(0, midIndex)
23+
const right = array.slice(midIndex, array.length)
3424

35-
while (indexLow < low.length && indexHigh < high.length) {
25+
return merge(mergeSort(left), mergeSort(right))
26+
}
3627

37-
if (low[indexLow] <= high[indexHigh]) {
38-
merged[curIndex++] = low[indexLow];
39-
indexLow++;
40-
} else {
41-
merged[curIndex++] = high[indexHigh];
42-
indexHigh++;
43-
}
44-
}
28+
function merge(left: number[], right: number[]): number[] {
29+
const result = Array<number>(left.length + right.length)
30+
let curIndex = 0
31+
let leftIndex = 0
32+
let rightIndex = 0
4533

46-
while (indexLow < low.length) {
47-
merged[curIndex++] = low[indexLow];
48-
indexLow++;
34+
while (leftIndex < left.length && rightIndex < right.length) {
35+
if (left[leftIndex] < right[rightIndex]) {
36+
result[curIndex++] = left[leftIndex++]
37+
} else {
38+
result[curIndex++] = right[rightIndex++]
4939
}
40+
}
41+
while (leftIndex < left.length) {
42+
result[curIndex++] = left[leftIndex++]
43+
}
44+
while (rightIndex < right.length) {
45+
result[curIndex++] = right[rightIndex++]
46+
}
5047

51-
while (indexHigh < high.length) {
52-
merged[curIndex++] = high[indexHigh];
53-
indexHigh++;
54-
}
55-
return merged;
56-
};
48+
return result
49+
}

sorts/test/merge_sort.test.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import { MergeSort } from "../merge_sort";
1+
import { mergeSort } from "../merge_sort"
22

33
describe("Merge Sort", () => {
4-
it("generating array with variable length and comparing with sorted array", () => {
5-
let arrLenArr = [10, 200, 40000];
4+
it("generating array with variable length and comparing with sorted array", () => {
5+
let arrLenArr = [10, 200, 40000]
66

7-
arrLenArr.forEach((arrLen: number) => {
8-
9-
let inBuiltSortArr = Array<number>(arrLen)
10-
for (let i = 0; i < arrLen; i++) { inBuiltSortArr[i] = Math.random() * 10000 }
11-
let mergeSortArray = inBuiltSortArr.slice();
12-
13-
inBuiltSortArr.sort((a, b) => a - b);
14-
expect(MergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr);
15-
16-
})
17-
});
18-
});
7+
arrLenArr.forEach((arrLen: number) => {
8+
let inBuiltSortArr = Array<number>(arrLen)
9+
for (let i = 0; i < arrLen; i++) {
10+
inBuiltSortArr[i] = Math.random() * 10000
11+
}
12+
let mergeSortArray = inBuiltSortArr.slice()
1913

14+
inBuiltSortArr.sort((a, b) => a - b)
15+
expect(mergeSort(mergeSortArray)).toStrictEqual(inBuiltSortArr)
16+
})
17+
})
18+
})

0 commit comments

Comments
 (0)