Skip to content

Commit 64a9d0f

Browse files
xam4lorButt4cak3
authored andcommitted
Added the java version of the bubble algorithm (#131)
1 parent 71ff692 commit 64a9d0f

File tree

5 files changed

+86
-3
lines changed

5 files changed

+86
-3
lines changed

CONTRIBUTORS.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ Gathros
55
Jeremie Gillet (- Jie -)
66
Salim Khatib
77
Hitesh C
8+
Maxime Dherbécourt
9+
Jess 3Jane
810
Pen Pal
9-
Jess 3Jane

chapters/sorting_searching/bogo/bogo_sort.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ In code, it looks something like this:
2121
[import:2-10, lang:"clojure"](code/clojure/bogo.clj)
2222
{% sample lang="c" %}
2323
[import:4-27, lang:"c_cpp"](code/c/bogo_sort.c)
24+
{% sample lang="java" %}
25+
[import:2-17, lang:"java"](code/java/bogo.java)
2426
{% sample lang="js" %}
2527
[import:1-16, lang:"javascript"](code/js/bogo.js)
2628
{% sample lang="hs" %}
@@ -58,4 +60,3 @@ $$
5860
\newcommand{\bfomega}{\boldsymbol{\omega}}
5961
\newcommand{\bftau}{\boldsymbol{\tau}}
6062
$$
61-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
public class Bogo {
2+
// The shuffle() function can be found in code/java/bogo.java
3+
static void bogoSort(int[] arr) {
4+
while(!isSorted(arr)) {
5+
shuffle(arr);
6+
}
7+
}
8+
9+
static boolean isSorted(int[] arr) {
10+
for (int i = 0; i < arr.length - 1; i++) {
11+
if(arr[i] > arr[i + 1]) {
12+
return false;
13+
}
14+
}
15+
16+
return true;
17+
}
18+
19+
static void shuffle(int[] arr) {
20+
for (int r = arr.length - 1; r > 0; r--) {
21+
int i = (int) Math.floor(Math.random() * r);
22+
int tmp = arr[i];
23+
arr[i] = arr[r];
24+
arr[r] = tmp;
25+
}
26+
}
27+
28+
29+
// main function (for testing)
30+
public static void main(String[] args) {
31+
int[] test = new int[]{20, -3, 50, 1, -6, 59};
32+
33+
System.out.println("Unsorted array :");
34+
for (int i = 0; i < test.length; i++) {
35+
System.out.print(test[i] + " ");
36+
}
37+
38+
39+
bogoSort(test);
40+
41+
42+
System.out.println("\n\nSorted array :");
43+
for (int i = 0; i < test.length; i++) {
44+
System.out.print(test[i] + " ");
45+
}
46+
System.out.println("");
47+
}
48+
}

chapters/sorting_searching/bubble/bubble_sort.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
1414
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
1515
{% sample lang="c" %}
1616
[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c)
17+
{% sample lang="java" %}
18+
[import:2-12, lang:"java"](code/java/bubble.java)
1719
{% sample lang="js" %}
1820
[import:1-11, lang:"javascript"](code/js/bubble.js)
1921
{% sample lang="hs" %}
@@ -54,4 +56,3 @@ $$
5456
\newcommand{\bfomega}{\boldsymbol{\omega}}
5557
\newcommand{\bftau}{\boldsymbol{\tau}}
5658
$$
57-
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
public class Bubble {
2+
static void bubbleSort(int[] arr) {
3+
for (int r = arr.length - 1; r > 0; r--) {
4+
for (int i = 0; i < r; i++) {
5+
if(arr[i] > arr[i + 1]) {
6+
int tmp = arr[i];
7+
arr[i] = arr[i + 1];
8+
arr[i + 1] = tmp;
9+
}
10+
}
11+
}
12+
}
13+
14+
15+
// main function (for testing)
16+
public static void main(String[] args) {
17+
int[] test = new int[]{20, -3, 50, 1, -6, 59};
18+
19+
System.out.println("Unsorted array :");
20+
for (int i = 0; i < test.length; i++) {
21+
System.out.print(test[i] + " ");
22+
}
23+
24+
bubbleSort(test);
25+
26+
System.out.println("\n\nSorted array :");
27+
for (int i = 0; i < test.length; i++) {
28+
System.out.print(test[i] + " ");
29+
}
30+
System.out.println("");
31+
}
32+
}

0 commit comments

Comments
 (0)