Skip to content

Instantly share code, notes, and snippets.

@kaskichandrakant
Created June 18, 2018 10:50
Show Gist options
  • Save kaskichandrakant/bece5625a00766fba293fbbc01f91020 to your computer and use it in GitHub Desktop.
Save kaskichandrakant/bece5625a00766fba293fbbc01f91020 to your computer and use it in GitHub Desktop.
function sort(list) {
if (list.length == 1) {
return list;
}
var listA = sort(list.slice(0, list.length / 2))
var listB = sort(list.slice(list.length / 2))
var indexA = 0
var indexB = 0
var newList = []
while (indexA < listA.length || indexB < listB.length) {
if (indexA !== listA.length && (listA[indexA] < listB[indexB] || indexB === listB.length)) {
newList.push(listA[indexA]);
indexA++
} else {
newList.push(listB[indexB]);
indexB++
}
}
return newList;
}
var list = []
for (var i = 0; i < 1000; i++) {
let random = Math.floor(Math.random() * 1000)
if(!list.includes(random)){
list.push(random)
}
}
console.log(sort(list));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment