Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java bogosort

import java.math.*;
import java.util.Arrays;

public class Test {
	
	public static void shuffle(int[] arr) {
		for(int i = 0; i < arr.length; i++) {
	        int index = (int) (Math.random() * arr.length);
	        int temp = arr[i];
	        arr[i] = arr[index];
	        arr[index] = temp;
		}
	}
	
	public static boolean isSorted(int[] arr) {
		for(int i = 1; i < arr.length; i++) {
			if(arr[i] < arr[i-1]) {
				return false;
			}
		}
		return true;
	}
	
	public static void bogoSort(int[] arr) {
		while(!isSorted(arr)) {
			shuffle(arr);
		}
	}
	
	public static void main(String[] args) {
		int[] arr = {3, 31, 16, 8};
		bogoSort(arr);
	    System.out.println(Arrays.toString(arr));
	}
}
Comment

PREVIOUS NEXT
Code Example
Java :: compress a directory in java 
Java :: comparestring java 
Java :: https://www.baeldung.com/hibernate-inheritance 
Java :: android java how to stop activity from opening twice programatically 
Java :: arraylist<hashmap<string, string arraylist = new arraylist<() 
Java :: java list remove 
Java :: java class array of objects 
Java :: for loop condition java 
Java :: javafx get button id 
Java :: check if two lists are equal java 
Java :: JFrame change border 
Java :: if not java 
Java :: how to sort arraylist in java 
Java :: change brightness of image in java 
Java :: expression régulière téléphone java 
Java :: get width and height of screen libgdx 
Java :: instance of 
Java :: java background color 
Java :: object null java 
Java :: fix nullpointerexception 
Java :: Create Java Strings using the new keyword 
Java :: java break string at comma 
Java :: garbage collection in java 
Java :: convert string to boolean java 
Java :: java insertion sort 
Java :: search in rotated sorted array leetcode 
Java :: list remove duplicates java 
Java :: each loop in java 
Java :: example of a for loop 
Java :: runtime exception in java 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =