Skip to content

Commit

Permalink
js bubble sort
Browse files Browse the repository at this point in the history
  • Loading branch information
lenmorld committed Oct 15, 2017
1 parent 2450354 commit 880ff77
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions bubble_sort/bubble_sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function bubble_sort(arr) {
console.log("before:", arr);
for (var i=arr.length-1; i>0; i-- ) {
for (var j=0; j<i;j++) {
if (arr[j] > arr[j+1]) {
swap(arr,j,j+1);
}
}
}
return arr;
}

function swap(arr, a, b) {
const temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}


array = [5,6,2,4,3,1,0,0]

console.log("sorted:", bubble_sort(array))

0 comments on commit 880ff77

Please sign in to comment.