Skip to content

Commit b3ff43a

Browse files
committed
Bubble sort code comment and README file updated
1 parent 2af0799 commit b3ff43a

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
- [Sorting](./Sorting/)
3131
- Selection Sort
32-
- Bubble Sort
32+
- [Bubble Sort](./Sorting/Bubble%20Sort)
3333
- Insertion Sort
3434
- Merge Sort
3535
- Quick Sort

Sorting/Bubble Sort/Bubble.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
/* Bubble Sort implementation in Java */
12

2-
public class Bubble
3+
public class Bubble
34
{
5+
//Simple Bubble Sort implementation
46
//Following function will sort the array in Increasing (ascending) order
57
void sort(int arr[])
68
{
79
int n = arr.length;
8-
10+
911
for (int i = 0; i < n-1; i++)
1012
{
1113
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
@@ -20,28 +22,29 @@ void sort(int arr[])
2022
}
2123
}
2224
}
23-
25+
26+
//Following is a slightly modified bubble sort implementation, which tracks the list with a flag to check if it is already sorted
2427
void modified_sort(int arr[])
2528
{
2629
int n = arr.length;
27-
30+
2831
for (int i = 0; i < n-1; i++)
2932
{
3033
boolean flag = false; //Taking a flag variable
31-
32-
// Last i elements are already in place
34+
35+
// Last i elements are already in place, so the inner loops will run until it reaches the last i elements
3336
for (int j = 0; j < n-i-1; j++)
3437
{
3538
if (arr[j] > arr[j+1]) //To Sort in decreasing order, change the comparison operator to '<'
3639
{
3740
int temp = arr[j];
3841
arr[j] = arr[j+1];
3942
arr[j+1] = temp;
40-
43+
4144
flag = true; //Setting the flag, if swapping occurs
4245
}
4346
}
44-
47+
4548
if(!flag) //If not swapped, that means the list has already sorted
4649
{
4750
break;

Sorting/Bubble Sort/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ Starting from the beginning of the list, compare every adjacent pair, swap their
1515

1616
![Bubble sort gif](https://upload.wikimedia.org/wikipedia/commons/c/c8/Bubble-sort-example-300px.gif)
1717

18-
####Complexity Analysis
18+
#### Complexity Analysis
1919
- Worst Case - O(n<sup>2</sup>)
2020
- Average Case - O(n<sup>2</sup>)
2121
- Best Case - O(n)
22+
2223
### More on this topic
23-
- http://en.wikipedia.org/wiki/Bubble_sort
24-
- http://quiz.geeksforgeeks.org/bubble-sort/
25-
- https://www.topcoder.com/community/data-science/data-science-tutorials/sorting/
24+
- [Bubble Sort](http://en.wikipedia.org/wiki/Bubble_sort)
25+
- [Bubble Sort](http://quiz.geeksforgeeks.org/bubble-sort/)

0 commit comments

Comments
 (0)