-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
Changes from 6 commits
011fbf1
3e5fdff
883be3b
3113eb4
00ae3c3
e1f6d43
f58d817
4b965fd
cf065a8
308296c
f991753
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,4 @@ Gathros | |
Jeremie Gillet (- Jie -) | ||
Salim Khatib | ||
Hitesh C | ||
Maxime Dherbécourt |
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"); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
public class Bubble { | ||
static void bubbleSort(int[] arr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] + " "); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add an empty |
||
} | ||
} |
There was a problem hiding this comment.
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. UsingSystem.out.println("\n")
will actually print two line breaks.Go for either
System.out.print("\n")
orSystem.out.println()
. I prefer the latter, but it's up to you.There was a problem hiding this comment.
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 ...