Skip to content

Commit 63c7649

Browse files
bubble
1 parent 5ef8629 commit 63c7649

File tree

9 files changed

+85
-0
lines changed

9 files changed

+85
-0
lines changed

lectures/11-sorting/code/.idea/.gitignore

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

lectures/11-sorting/code/.idea/description.html

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

lectures/11-sorting/code/.idea/encodings.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.

lectures/11-sorting/code/.idea/misc.xml

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

lectures/11-sorting/code/.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.

lectures/11-sorting/code/.idea/project-template.xml

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

lectures/11-sorting/code/.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.

lectures/11-sorting/code/code.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.kunal;
2+
3+
import java.util.Arrays;
4+
5+
public class Main {
6+
7+
public static void main(String[] args) {
8+
int[] arr = {3, 1, 5, 4, 2};
9+
bubble(arr);
10+
System.out.println(Arrays.toString(arr));
11+
}
12+
13+
static void bubble(int[] arr) {
14+
boolean swapped;
15+
// run the steps n-1 times
16+
for (int i = 0; i < arr.length; i++) {
17+
swapped = false;
18+
// for each step, max item will come at the last respective index
19+
for (int j = 1; j < arr.length - i; j++) {
20+
// swap if the item is smaller than the previous item
21+
if (arr[j] < arr[j-1]) {
22+
// swap
23+
int temp = arr[j];
24+
arr[j] = arr[j-1];
25+
arr[j-1] = temp;
26+
swapped = true;
27+
}
28+
}
29+
// if you did not swap for a particular value of i, it means the array is sorted hence stop the program
30+
if (!swapped) { // !false = true
31+
break;
32+
}
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)