W
Size: a a a
W
{
{
D
{
const shuffleArray = arr => {
const len = arr.length
for (let i = 0; i < len - 1; i += 1) {
let r = Math.floor(Math.random() * (len - i))
r && ([arr[i], arr[i + r]] = [arr[i + r], arr[i]])
}
return arr
}
const chaoticArray = shuffleArray(Array(20).fill(0).map((_, i) => i + 1))
const selectionSort = array => {
for (let i = 0; i < array.length - 1; i += 1) {
let min = i;
for (let j = i + 1; j < array.length; j += 1) {
if (array[min] > array[j]) min = j;
}
[array[i], array[min]] = [array[min], array[i]];
}
return array
}
const mergeSort = array => {
const pivot = (start, end) => Math.floor((start + end) / 2);
const len = array.length
let tempA = [...array]
let tempB = tempA.splice(pivot(0, len), len)
tempA = selectionSort(tempA)
tempB = selectionSort(tempB)
let i = 0
let j = 0
let k = 0
const result = []
while (k < len) {
if (tempA[i] < tempB[j]) {
result.push(tempA[i])
i += 1
} else {
result.push(tempB[j])
j += 1
}
k += 1
}
return result;
}
console.log(mergeSort(chaoticArray))
D
j
D
D
D
D
D
D
D
D
D
D
{
{
{