diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 0a5442fe0..57640d2d6 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -5,5 +5,6 @@ Gathros Jeremie Gillet (- Jie -) Salim Khatib Hitesh C +Maxime Dherbécourt +Jess 3Jane Pen Pal -Jess 3Jane \ No newline at end of file diff --git a/chapters/sorting_searching/bogo/bogo_sort.md b/chapters/sorting_searching/bogo/bogo_sort.md index 36f7da1f7..e8c91fec2 100644 --- a/chapters/sorting_searching/bogo/bogo_sort.md +++ b/chapters/sorting_searching/bogo/bogo_sort.md @@ -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" %} @@ -58,4 +60,3 @@ $$ \newcommand{\bfomega}{\boldsymbol{\omega}} \newcommand{\bftau}{\boldsymbol{\tau}} $$ - diff --git a/chapters/sorting_searching/bogo/code/java/bogo.java b/chapters/sorting_searching/bogo/code/java/bogo.java new file mode 100644 index 000000000..d44c3b422 --- /dev/null +++ b/chapters/sorting_searching/bogo/code/java/bogo.java @@ -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(""); + } +} diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index 5d207ffe7..b71e50ed3 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -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" %} @@ -54,4 +56,3 @@ $$ \newcommand{\bfomega}{\boldsymbol{\omega}} \newcommand{\bftau}{\boldsymbol{\tau}} $$ - diff --git a/chapters/sorting_searching/bubble/code/java/bubble.java b/chapters/sorting_searching/bubble/code/java/bubble.java new file mode 100644 index 000000000..fa7dd9971 --- /dev/null +++ b/chapters/sorting_searching/bubble/code/java/bubble.java @@ -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(""); + } +}