Skip to content

Commit 9f67819

Browse files
committed
Bubble sort code added
1 parent 48669df commit 9f67819

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

Sorting/Bubble Sort/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Bubble Sort
2+
3+
**Bubble Sort** is an algorithm which is used to sort **N** elements that are given in a memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one and sort them based on their values.
4+
5+
It is called Bubble sort, because *with each iteration the smaller element in the list bubbles up towards the first place*, just like a water bubble rises up to the water surface.
6+
7+
Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order.
8+
![bubble sort demonstration](bubble-sort.png)
9+
10+
11+
---
12+
**A visualization on the algorithm:**
13+
14+
Starting from the beginning of the list, compare every adjacent pair, swap their position if they are not in the right order (the latter one is smaller than the former one). After each iteration, one less element (the last one) is needed to be compared until there are no more elements left to be compared.
15+
16+
![Bubble sort gif](https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif)
17+
18+
#### Complexity Analysis
19+
- Worst Case - O(n<sup>2</sup>)
20+
- Average Case - O(n<sup>2</sup>)
21+
- Best Case - O(n)
22+
23+
### More on this topic
24+
- http://en.wikipedia.org/wiki/Bubble_sort
25+
- http://quiz.geeksforgeeks.org/bubble-sort/
26+
- https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/

Sorting/Bubble Sort/bubble-sort.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//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;
18+
}
19+
20+
//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+
24+
for(var i=0; i<len-1; i++){
25+
var flag = false; //Taking a flag variable
26+
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;
34+
35+
flag = true; //Setting the flag, if swapping occurs
36+
}
37+
}
38+
39+
//If not swapped, that means the list has already sorted
40+
if(!flag) break;
41+
}
42+
43+
return arr;
44+
}
45+
46+
47+
/** Testing Bubble sort algorithm **/
48+
/**
49+
* Returns a random integer between min (inclusive) and max (inclusive)
50+
* Using Math.round() will give you a non-uniform distribution!
51+
*/
52+
function getRandomInt(min, max) {
53+
return Math.floor(Math.random() * (max - min + 1)) + min;
54+
}
55+
56+
var arr = [];
57+
58+
for(var i=0;i<10;i++){
59+
arr.push(getRandomInt(1, 100));
60+
}
61+
console.log("Unsorted array: ");
62+
console.log(arr); //printing unsorted array
63+
64+
arr = bubbleSort(arr);
65+
// arr = modifiedBubbleSort(arr);
66+
console.log("Sorted array: ");
67+
console.log(arr); //printing sorted array

Sorting/Bubble Sort/bubble-sort.png

8.5 KB
Loading

0 commit comments

Comments
 (0)