Skip to content

Instantly share code, notes, and snippets.

@imshubhamsingh
Created March 31, 2018 03:44
Show Gist options
  • Save imshubhamsingh/44422d532f3111ba253c55dfd462dde8 to your computer and use it in GitHub Desktop.
Save imshubhamsingh/44422d532f3111ba253c55dfd462dde8 to your computer and use it in GitHub Desktop.
Shuffle Song List in Javascript in O(n)
const shuffleSongs = (songList) => {
let index = Date.now() % songList.length
let list = []
for (var i = 0; i < songList.length/2; i++) {
if (index + i < songList.length) {
list.push(songList[index + i])
} else {
break;
}
if (i === 0) continue;
if (index - i >= 0) {
list.push(songList[index - i])
} else {
break;
}
}
for (var j = index + i + 1; j < songList.length; j++) {
list.push(songList[j])
}
for (var j = index - i; j >= 0; j--) {
list.push(songList[j])
}
return list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment