Created
October 15, 2012 20:31
-
-
Save johl/3895222 to your computer and use it in GitHub Desktop.
BogoSort
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
import java.util.Arrays; | |
public class BogoSort { | |
public static int[] array = {2,5,6,3,1,73,46}; | |
public static int[] shuffle(int[] a) { | |
int n = a.length; | |
Random random = new Random(); | |
random.nextInt(); | |
for (int i = 0; i < n; i++) { | |
int change = i + random.nextInt(n - i); | |
swap(a, i, change); | |
} | |
return a; | |
} | |
private static void swap(int[] a, int i, int change) { | |
int helper = a[i]; | |
a[i] = a[change]; | |
a[change] = helper; | |
} | |
public static Boolean isUnsorted(int[] a) { | |
Boolean unsorted = false; | |
for (int i = 0; i < a.length-1; i++) { | |
if (a[i] > a[i+1]) { | |
unsorted = true; | |
break; | |
} | |
} | |
return unsorted; | |
} | |
public static void main(String[] args) { | |
while (isUnsorted(array)) { | |
System.out.println("Array ist nun: " + Arrays.toString(array)); | |
array = shuffle(array); | |
} | |
System.out.println("Array ist nun: " + Arrays.toString(array)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment