Created
February 19, 2019 14:24
-
-
Save alaingoldman/958b7285315061a6f9b0472e97119cdd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var a = []; | |
function generateRandomData(limit) { | |
const alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]; | |
for (let i = 0; i < limit; i++) { | |
let item = alphabet[Math.floor(Math.random() * alphabet.length)]; | |
a.push(item); | |
} | |
} | |
generateRandomData(10000); | |
function split() { | |
for (let increment = 1; increment <= a.length; increment *= 2) { | |
for (let initial = 0; initial < a.length; initial += increment * 2) { | |
let left = a.slice(initial, (initial + increment)); | |
let right = a.slice((initial + increment), (initial + increment + increment)) | |
compare(left, right, initial); | |
} | |
} | |
console.log(a); | |
} | |
function compare(left, right, pos) { | |
let leftIndex = 0; | |
let rightIndex = 0; | |
let result = []; | |
while (result.length < (left.length + right.length)) { | |
if (left[leftIndex] <= right[rightIndex] || right[rightIndex] === undefined) { | |
result.push(left[leftIndex]); | |
leftIndex++; | |
} else { | |
result.push(right[rightIndex]); | |
rightIndex++; | |
} | |
} | |
let newLeft = a.slice(0, pos); | |
let newRight = a.slice((result.length + pos), a.length); | |
a = newLeft.concat(result, newRight); | |
} | |
split(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment