Last active
December 12, 2021 17:30
-
-
Save sunmeat/82aa50b8efdff4484bc1 to your computer and use it in GitHub Desktop.
bogosort code example for android 8
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
package yourPackage; | |
import java.util.Arrays; | |
import java.util.Random; | |
public class YourClassName { | |
public static void main(String[] args) { | |
Random random = new Random(); | |
int[] data = new int[]{1, 9, 2, 8, 3, 7, 4, 5}; | |
boolean inOrder = false; | |
int total_try = 0; | |
sorting: | |
while (!inOrder) { | |
// everyday i'm shuffling | |
for (int i = 0; i < data.length - 1; i++) { | |
int swapPosition = random.nextInt(data.length); | |
int temp = data[i]; | |
data[i] = data[swapPosition]; | |
data[swapPosition] = temp; | |
} | |
// current state | |
System.out.println(++total_try + ": " + Arrays.toString(data)); | |
for (int i = 0; i < data.length - 1; i++) { | |
if (data[i] > data[i + 1]) { | |
continue sorting; | |
} | |
} | |
inOrder = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment