License Public Domain
Lines 20
Implements this Algorithm
Permissions
Viewable by Everyone
Editable by All Siafoo Users
Hide
Writing an article is easy - try our reStructured Text demo Join Siafoo Now or Learn More

Bubble Sort Atom Feed 0

In Brief Simple implementation of the Bubble Sort algorithm in Javascript.... more
# 's
 1function bubbleSort(array){
2
3 var swapped
4
5 // Keep making passes until nothing is swapped
6 do{
7 swapped = false
8
9 // Step over array
10 for(i = 0; i < array.length - 1; i++){
11 if (array[i] > array[i+1]){
12 //Maybe I'm dumb, is there a 'real' way to do this in js?
13 var tmp = array[i]
14 array[i] = array[i+1]
15 array[i+1] = tmp
16 swapped = true
17 }
18 }
19
20 } while(swapped)
21
22 return array
23}
24

Simple implementation of the Bubble Sort algorithm in Javascript.

Pass the function an array, it will modify it in place and return it to you.