-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-an-array.js
46 lines (43 loc) · 1.14 KB
/
sort-an-array.js
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
42
43
44
45
46
/**
* @param {number[]} nums
* @return {number[]}
*/
var sortArray = function(nums) {
if (nums === null || nums.length === 0)
return;
let temp = [];
mergeSort(nums, 0, nums.length - 1, temp);
return nums;
};
function mergeSort(nums, start, end, temp) {
if (start >= end)
return;
// deal with first half
mergeSort(nums, start, Math.floor((start + end) / 2), temp);
// deal with second half
mergeSort(nums, Math.floor((start + end) / 2) + 1, end, temp);
// merge
merge(nums, start, end, temp);
}
function merge(nums, start, end, temp) {
let mid = Math.floor((start + end) / 2);
let l_index = start;
let r_index = mid + 1;
let index = start;
while (l_index <= mid && r_index <= end) {
if (nums[l_index] < nums[r_index]) {
temp[index++] = nums[l_index++];
} else {
temp[index++] = nums[r_index++];
}
}
while (l_index <= mid) {
temp[index++] = nums[l_index++];
}
while (r_index <= end) {
temp[index++] = nums[r_index++];
}
for (let i = start; i <= end; i++) {
nums[i] = temp[i];
}
};