Created
October 9, 2017 02:04
-
-
Save qzm/308d9261b6bc47e0d501cb5bbcbdcf1e to your computer and use it in GitHub Desktop.
Bubble Sort
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
function bubbleSort(arr) { | |
// copy | |
var newArr = []; | |
for (var index = 0; index < arr.length; index++) { | |
newArr[index] = arr[index]; | |
} | |
// sort | |
var temp; | |
for (var i = 0; i < newArr.length; i++) { | |
for (var j = 0; j < (newArr.length - i); j++) { | |
if (newArr[j] > newArr[j + 1]) { | |
temp = newArr[j]; | |
newArr[j] = newArr[j + 1]; | |
newArr[j + 1] = temp; | |
} | |
} | |
} | |
return newArr; | |
} | |
var sortList = bubbleSort([1, 1, 1, 1, 1, 0, 4, 78, 2, 789, 32, 7890, 32, 4]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment