<h1>Bubble sort algorithm in JavaScript</h1>
<p>Please check javascript code.</p>
/***************************************
* sort
***************************************/
var bubbleSort = function(list){
var k = 0,
l = list.length,
i,w;
while(k < l){
i = list.length;
while(i > k){
if(list[i-1] > list[i]){
w = list[i-1];
list[i-1] = list[i];
list[i] = w;
}
--i;
}
++k;
}
return list;
};
// 2つ取り出して比較する
// 隣と取り替える
// 取り替えたインデックスとそのとなりを比較する
/***************************************
* main
***************************************/
var before = [0,9,3,4,6,7,8,2,1,5];
console.log('before : ' + before);
var after = bubbleSort(before);
console.log('after : ' + after);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.