Skip to content

Commit 62d9256

Browse files
committed
jisan_bubble_sort
Completed Bubble Sort Algorithm
1 parent 43f00d2 commit 62d9256

File tree

10 files changed

+223
-0
lines changed

10 files changed

+223
-0
lines changed

.idea/.gitignore

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/DSA-Java.iml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/Project.xml

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/codeStyles/codeStyleConfig.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/Project_Default.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lessons/bubblesort.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,126 @@ order: "5C"
55
section: "Searching & Sorting"
66
description: "learn Searching n sorting algorithms"
77
---
8+
9+
# Sorting
10+
11+
Sorting is an everyday tool for programmers.We need to sort many things for many reasons.Like sorting to age,price etc.So
12+
there are many sorting algorithms like Merge Sort , Quick Sort, Bubble sort etc.
13+
#Stable and Unstable Sorting
14+
If Two elements in array has same value, After sorting two things can happen.
15+
1. They exchange their relative position.
16+
2. They don't exchange their relative position.
17+
![Stable Sorting](./images/stableSort.jpeg)
18+
19+
From this example we can see that two '26' doesn't replace their relative positions.Hence it is
20+
stable sorting algorithms.
21+
Merge Sort ,Bubble Sort, insertion sort are actually stable sorting algorithm.
22+
On the other hand,Heap Sort, Quick Sort etc are unstable sorting algorithms.
23+
24+
#Bubble Sort
25+
Bubble Sort is the simplest algorithm. This is named Bubble Sort because its element just swap
26+
one by one.
27+
It doesn't have any special use in sorting.It is actually used for educational purposes to introduces
28+
sorting algorithms. I'm actually sorry if I disappointed you. Sort your emotions then.
29+
30+
31+
<ul>
32+
Suppose we have an array of 3 numbers [3,6,2] which should be sorted in incrementing order. We can use Bubble Sort
33+
Algorithms to do so.
34+
<li>
35+
At first, we have to take the first element of the array and compare it to the rest of the
36+
elements.If we get any elements lower than it, we will swap the position.
37+
Comparison 1: 3 > 6 ->NO
38+
</li>
39+
40+
<li>
41+
Comparison 2: 3 > 2
42+
->YES swap the positions Then the array will be [2,6,3]
43+
44+
</li>
45+
<li>
46+
Comparison 3: 6>3 -> YES swap the positions Then the array will be [2,3,6]
47+
48+
</li>
49+
</ul>
50+
Boom, We have sorted the array. Now
51+
52+
Now let's try to do this in Java.
53+
54+
#Bubble Sort Function
55+
```
56+
void bubbleSort(int array[], int length){
57+
58+
for(int i = 0; i < length-1; i++){
59+
for(int j = i+1; j < length-1; j++){
60+
//first loop is used to check to the (length-1)th index of the aray
61+
//second loop is used to check the following items of the arrays
62+
int temp;
63+
if(array[i]<array[j]){
64+
temp = array[i];
65+
array[i]=array[j];
66+
array[j]=temp;
67+
}
68+
}
69+
}
70+
71+
```
72+
73+
#Complete Program in Java
74+
```
75+
76+
77+
public class Main {
78+
static int array[] = {2, 1, 6, 5, 4};
79+
80+
81+
public static void main(String[] args) {
82+
83+
int length = array.length;
84+
System.out.println("Unsorted Array");
85+
86+
for (int i = 0; i < length; i++) {
87+
System.out.println(array[i]);
88+
}
89+
90+
System.out.println("Unsorted Array");
91+
bubbleSort(array, length);
92+
93+
94+
95+
96+
97+
98+
}
99+
100+
public static void bubbleSort(int array[], int length) {
101+
102+
for (int i = 0; i < length - 1; i++) {
103+
for (int j = i + 1; j < length ; j++) {
104+
//first loop is used to check to the (length-1)th index of the aray
105+
//second loop is used to check the following items of the arrays
106+
int temp;
107+
if (array[i] > array[j]) {
108+
temp = array[i];
109+
array[i] = array[j];
110+
array[j] = temp;
111+
}
112+
}
113+
}
114+
//for printing
115+
116+
for (int i = 0; i < length; i++) {
117+
System.out.println(array[i]);
118+
}
119+
}
120+
}
121+
122+
```
123+
#Time Complexity:
124+
Now talk about the time complexity of this algorithm.From the coding algorithm that is clear that we have to use a loop and
125+
a nested loop. So the complexity becomes O(n*n).
126+
127+
#Space Complexity:
128+
Here Space complexity is O(1). We just have to make a temp variable which is updated throughout the loop and doesn't need to
129+
create anything new.
130+

lessons/images/bubble.gif

333 KB
Loading

lessons/images/stableSort.jpeg

8 KB
Loading

0 commit comments

Comments
 (0)