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
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Gathros
Jeremie Gillet (- Jie -)
Salim Khatib
Hitesh C
Maxime Dherbécourt
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 @@ -56,4 +58,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("\n");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need both println and "\n". println already automatically adds a "\n" at the end of the string. Using System.out.println("\n") will actually print two line breaks.

Go for either System.out.print("\n") or System.out.println(). I prefer the latter, but it's up to you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really know if it's better with one or two spaces ...

}
}
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 @@ -50,4 +52,3 @@ $$
\newcommand{\bfomega}{\boldsymbol{\omega}}
\newcommand{\bftau}{\boldsymbol{\tau}}
$$

31 changes: 31 additions & 0 deletions chapters/sorting_searching/bubble/code/java/bubble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
public class Bubble {
static void bubbleSort(int[] arr) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This entire file is still indented with tabs. Again, not inherently bad, but you should replace these with 4 spaces each for consistency.

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] + " ");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add an empty System.out.println() here to fix the output, just as in bogo.java.

}
}