Skip to content

Added the java version of the bubble algorithm #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 28, 2018
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ Gathros
Jeremie Gillet (- Jie -)
Salim Khatib
Hitesh C
Maxime Dherbécourt
Jess 3Jane
Pen Pal
Jess 3Jane
3 changes: 2 additions & 1 deletion chapters/sorting_searching/bogo/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ In code, it looks something like this:
[import:2-10, lang:"clojure"](code/clojure/bogo.clj)
{% sample lang="c" %}
[import:4-27, lang:"c_cpp"](code/c/bogo_sort.c)
{% sample lang="java" %}
[import:2-17, lang:"java"](code/java/bogo.java)
{% sample lang="js" %}
[import:1-16, lang:"javascript"](code/js/bogo.js)
{% sample lang="hs" %}
Expand Down Expand Up @@ -58,4 +60,3 @@ $$
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$

48 changes: 48 additions & 0 deletions chapters/sorting_searching/bogo/code/java/bogo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
public class Bogo {
// The shuffle() function can be found in code/java/bogo.java
static void bogoSort(int[] arr) {
while(!isSorted(arr)) {
shuffle(arr);
}
}

static boolean isSorted(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i + 1]) {
return false;
}
}

return true;
}

static void shuffle(int[] arr) {
for (int r = arr.length - 1; r > 0; r--) {
int i = (int) Math.floor(Math.random() * r);
int tmp = arr[i];
arr[i] = arr[r];
arr[r] = tmp;
}
}


// main function (for testing)
public static void main(String[] args) {
int[] test = new int[]{20, -3, 50, 1, -6, 59};

System.out.println("Unsorted array :");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + " ");
}


bogoSort(test);


System.out.println("\n\nSorted array :");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + " ");
}
System.out.println("");
}
}
3 changes: 2 additions & 1 deletion chapters/sorting_searching/bubble/bubble_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with
[import:9-27, lang:"csharp"](code/cs/BubbleSort.cs)
{% sample lang="c" %}
[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c)
{% sample lang="java" %}
[import:2-12, lang:"java"](code/java/bubble.java)
{% sample lang="js" %}
[import:1-11, lang:"javascript"](code/js/bubble.js)
{% sample lang="hs" %}
Expand Down Expand Up @@ -54,4 +56,3 @@ $$
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$

32 changes: 32 additions & 0 deletions chapters/sorting_searching/bubble/code/java/bubble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Bubble {
static void bubbleSort(int[] arr) {
for (int r = arr.length - 1; r > 0; r--) {
for (int i = 0; i < r; i++) {
if(arr[i] > arr[i + 1]) {
int tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
}
}
}
}


// main function (for testing)
public static void main(String[] args) {
int[] test = new int[]{20, -3, 50, 1, -6, 59};

System.out.println("Unsorted array :");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + " ");
}

bubbleSort(test);

System.out.println("\n\nSorted array :");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + " ");
}
System.out.println("");
}
}