Skip to content

Commit f4d5536

Browse files
committed
Bubble sort code reformatted
1 parent 2b7a531 commit f4d5536

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Learn Data Structure and Algorithms by Javascript
1+
# Learn Data Structure and Algorithms by JavaScript
22

33
> You need to have basic understanding of the JavaScript programming language to proceed with the codes from this repository.
44

Sorting/Bubble Sort/bubble-sort.js

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,48 @@
1+
/* Bubble Sort implementation in JavaScript */
2+
13
//Simple Bubble Sort implementation
2-
function bubbleSort(arr){
3-
var len = arr.length;
4-
5-
for(var i=0; i<len-1; i++){
6-
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
7-
for(var j=0; j<len-i-1;j++){
8-
//To Sort in decreasing order, change the comparison operator to '<'
9-
if(arr[j] > arr[j+1]){
10-
var tmp = arr[j];
11-
arr[j] = arr[j+1];
12-
arr[j+1] = tmp;
13-
}
14-
}
15-
}
16-
17-
return arr;
4+
function bubbleSort(arr) {
5+
var len = arr.length;
6+
7+
for (var i = 0; i < len - 1; i++) {
8+
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
9+
for (var j = 0; j < len - i - 1; j++) {
10+
//To Sort in decreasing order, change the comparison operator to '<'
11+
if (arr[j] > arr[j + 1]) {
12+
var tmp = arr[j];
13+
arr[j] = arr[j + 1];
14+
arr[j + 1] = tmp;
15+
}
16+
}
17+
}
18+
19+
return arr;
1820
}
1921

2022
//Following is a slightly modified bubble sort implementation, which tracks the list with a flag to check if it is already sorted
21-
function modifiedBubbleSort(arr){
22-
var len = arr.length;
23+
function modifiedBubbleSort(arr) {
24+
var len = arr.length;
2325

24-
for(var i=0; i<len-1; i++){
25-
var flag = false; //Taking a flag variable
26+
for (var i = 0; i < len - 1; i++) {
27+
var flag = false; //Taking a flag variable
2628

27-
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
28-
for(var j=0; j<len-i-1;j++){
29-
//To Sort in decreasing order, change the comparison operator to '<'
30-
if(arr[j] > arr[j+1]){
31-
var tmp = arr[j];
32-
arr[j] = arr[j+1];
33-
arr[j+1] = tmp;
29+
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
30+
for (var j = 0; j < len - i - 1; j++) {
31+
//To Sort in decreasing order, change the comparison operator to '<'
32+
if (arr[j] > arr[j + 1]) {
33+
var tmp = arr[j];
34+
arr[j] = arr[j + 1];
35+
arr[j + 1] = tmp;
3436

35-
flag = true; //Setting the flag, if swapping occurs
36-
}
37-
}
37+
flag = true; //Setting the flag, if swapping occurs
38+
}
39+
}
3840

39-
//If not swapped, that means the list has already sorted
40-
if(!flag) break;
41-
}
41+
//If not swapped, that means the list has already sorted
42+
if (!flag) break;
43+
}
4244

43-
return arr;
45+
return arr;
4446
}
4547

4648

@@ -51,13 +53,13 @@ function modifiedBubbleSort(arr){
5153
* Using Math.round() will give you a non-uniform distribution!
5254
*/
5355
function getRandomInt(min, max) {
54-
return Math.floor(Math.random() * (max - min + 1)) + min;
56+
return Math.floor(Math.random() * (max - min + 1)) + min;
5557
}
5658

5759
var arr = [];
5860

59-
for(var i=0;i<10;i++){//initialize a random integer unsorted array
60-
arr.push(getRandomInt(1, 100));
61+
for (var i = 0; i < 10; i++) { //initialize a random integer unsorted array
62+
arr.push(getRandomInt(1, 100));
6163
}
6264

6365
console.log("Unsorted array: ");

0 commit comments

Comments
 (0)