Skip to content

Commit 67269fd

Browse files
author
Nicholas C. Zakas
committed
Bubble sort implementation
1 parent 92a85af commit 67269fd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Bubble sort implementation in JavaScript
3+
* Copyright (c) 2009 Nicholas C. Zakas
4+
* MIT Licensed - see LICENSE for details on license.
5+
*/
6+
7+
/**
8+
* A bubble sort implementation in JavaScript. The array
9+
* is sorted in-place.
10+
* @param {Array} items An array of items to sort.
11+
*/
12+
13+
function sort(items){
14+
for (var i=items.length-1; i >= 0; i--){
15+
for (var j=i; j >= 0; j--){
16+
if (items[j] < items[j-1]){
17+
var temp = items[j];
18+
items[j] = items[j-1];
19+
items[j-1] = temp;
20+
}
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)