Problem
Given a list of numbers, sort it using the Bubble Sort algorithm.
Example
{
"arr": [5, 8, 3, 9, 4, 1, 7]
}
Output:
[1, 3, 4, 5, 7, 8, 9]
Solution
function swapArr(arr, i, j) {
var tempArr = arr[i];
arr[i] = arr[j];
arr[j] = tempArr;
}
function bubble_sort(arr) {
// Write your code here.
for(var i=0; i< arr.length; i+= 1){
for(var j=1; j < arr.length; j+=1) {
if(arr[j-1] > arr[j]) {
swapArr(arr, j-1, j);
}
}
}
return arr;
}