all tasks
This commit is contained in:
parent
b83f433714
commit
68f8f3e56b
14735 changed files with 178959 additions and 0 deletions
|
|
@ -0,0 +1,24 @@
|
|||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class Bogosort {
|
||||
private static <T extends Comparable<? super T>> boolean isSorted(List<T> list) {
|
||||
if (list.isEmpty())
|
||||
return true;
|
||||
Iterator<T> it = list.iterator();
|
||||
T last = it.next();
|
||||
while (it.hasNext()) {
|
||||
T current = it.next();
|
||||
if (last.compareTo(current) > 0)
|
||||
return false;
|
||||
last = current;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static <T extends Comparable<? super T>> void bogoSort(List<T> list) {
|
||||
while (!isSorted(list))
|
||||
Collections.shuffle(list);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue