File tree Expand file tree Collapse file tree 1 file changed +16
-5
lines changed
algorithms/sorting/bubble-sort Expand file tree Collapse file tree 1 file changed +16
-5
lines changed Original file line number Diff line number Diff line change 8
8
* A bubble sort implementation in JavaScript. The array
9
9
* is sorted in-place.
10
10
* @param {Array } items An array of items to sort.
11
+ * @return {void }
11
12
*/
12
-
13
- function sort ( items ) {
13
+ function bubbleSort ( items ) {
14
14
for ( var i = items . length - 1 ; i >= 0 ; i -- ) {
15
15
for ( var j = i ; j >= 0 ; j -- ) {
16
16
if ( items [ j ] < items [ j - 1 ] ) {
17
- var temp = items [ j ] ;
18
- items [ j ] = items [ j - 1 ] ;
19
- items [ j - 1 ] = temp ;
17
+ swap ( items , j , j - 1 ) ;
20
18
}
21
19
}
22
20
}
21
+ }
22
+
23
+ /**
24
+ * Swaps two values in an array.
25
+ * @param {Array } items The array containing the items.
26
+ * @param {int } firstIndex Index of first item to swap.
27
+ * @param {int } secondIndex Index of second item to swap.
28
+ * @return {void }
29
+ */
30
+ function swap ( items , firstIndex , secondIndex ) {
31
+ var temp = items [ firstIndex ] ;
32
+ items [ firstIndex ] = items [ secondIndex ] ;
33
+ items [ secondIndex ] = temp ;
23
34
}
You can’t perform that action at this time.
0 commit comments