-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-sort.js
43 lines (35 loc) · 1.27 KB
/
merge-sort.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
function mergeSort(arr) {
if (arr.length <= 1) {
return arr; // O array já está ordenado se tiver 0 ou 1 elemento
}
// Divide o array ao meio
const middle = Math.floor(arr.length / 2);
const leftHalf = arr.slice(0, middle);
const rightHalf = arr.slice(middle);
// Recursivamente ordena as duas metades
const leftSorted = mergeSort(leftHalf);
const rightSorted = mergeSort(rightHalf);
// Mescla as duas metades ordenadas
return merge(leftSorted, rightSorted);
}
function merge(left, right) {
let result = [];
let leftIndex = 0;
let rightIndex = 0;
// Comparando e mesclando as duas metades
while (leftIndex < left.length && rightIndex < right.length) {
if (left[leftIndex] < right[rightIndex]) {
result.push(left[leftIndex]);
leftIndex++;
} else {
result.push(right[rightIndex]);
rightIndex++;
}
}
// Adiciona os elementos restantes (se houver) de ambas as metades
return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
}
const unsortedArray = [38, 27, 43, 3, 9, 82, 10];
const sortedArray = mergeSort(unsortedArray);
console.log("Array não ordenado:", unsortedArray);
console.log("Array ordenado:", sortedArray);