2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,13 +1,23 @@
{{Sorting Algorithm}}
;Task:
[[wp:Bogosort|Bogosort]] a list of numbers.
Bogosort simply shuffles a collection randomly until it is sorted.
"Bogosort" is a perversely inefficient algorithm only used as an in-joke.
Its average run-time is O(n!) because the chance that any given shuffle of a set will end up in sorted order is about one in ''n'' factorial, and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence. Its best case is O(n) since a single pass through the elements may suffice to order them.
Its average run-time is   O(n!)   because the chance that any given shuffle of a set will end up in sorted order is about one in   ''n''   factorial,   and the worst case is infinite since there's no guarantee that a random shuffling will ever produce a sorted sequence.
Its best case is   O(n)   since a single pass through the elements may suffice to order them.
Pseudocode:
'''while not''' InOrder(list) '''do'''
Shuffle(list)
'''done'''
<br>
The [[Knuth shuffle]] may be used to implement the shuffle part of this algorithm.
<br><br>

View file

@ -0,0 +1,65 @@
identification division.
program-id. bogo-sort-program.
data division.
working-storage section.
01 array-to-sort.
05 item-table.
10 item pic 999
occurs 10 times.
01 randomization.
05 random-seed pic 9(8).
05 random-index pic 9.
01 flags-counters-etc.
05 array-index pic 99.
05 adjusted-index pic 99.
05 temporary-storage pic 999.
05 shuffles pic 9(8)
value zero.
05 sorted pic 9.
01 numbers-without-leading-zeros.
05 item-no-zeros pic z(4).
05 shuffles-no-zeros pic z(8).
procedure division.
control-paragraph.
accept random-seed from time.
move function random(random-seed) to item(1).
perform random-item-paragraph varying array-index from 2 by 1
until array-index is greater than 10.
display 'BEFORE SORT:' with no advancing.
perform show-array-paragraph varying array-index from 1 by 1
until array-index is greater than 10.
display ''.
perform shuffle-paragraph through is-it-sorted-paragraph
until sorted is equal to 1.
display 'AFTER SORT: ' with no advancing.
perform show-array-paragraph varying array-index from 1 by 1
until array-index is greater than 10.
display ''.
move shuffles to shuffles-no-zeros.
display shuffles-no-zeros ' SHUFFLES PERFORMED.'
stop run.
random-item-paragraph.
move function random to item(array-index).
show-array-paragraph.
move item(array-index) to item-no-zeros.
display item-no-zeros with no advancing.
shuffle-paragraph.
perform shuffle-items-paragraph,
varying array-index from 1 by 1
until array-index is greater than 10.
add 1 to shuffles.
is-it-sorted-paragraph.
move 1 to sorted.
perform item-in-order-paragraph varying array-index from 1 by 1,
until sorted is equal to zero
or array-index is equal to 10.
shuffle-items-paragraph.
move function random to random-index.
add 1 to random-index giving adjusted-index.
move item(array-index) to temporary-storage.
move item(adjusted-index) to item(array-index).
move temporary-storage to item(adjusted-index).
item-in-order-paragraph.
add 1 to array-index giving adjusted-index.
if item(array-index) is greater than item(adjusted-index)
then move zero to sorted.

View file

@ -1,9 +1,7 @@
bogosort <- function(x)
{
is.sorted <- function(x) all(diff(x) >= 0)
while(!is.sorted(x)) x <- sample(x)
bogosort <- function(x) {
while(is.unsorted(x)) x <- sample(x)
x
}
n <- c(1, 10, 9, 7, 3, 0)
print(bogosort(n))
bogosort(n)

View file

@ -0,0 +1,24 @@
extern crate rand;
use rand::{thread_rng, Rng};
use std::vec::Vec;
fn sorted(curlist: &Vec<u64>) -> bool {
let mut sortedlist = curlist.clone();
sortedlist.sort();
curlist.iter().eq(sortedlist.iter())
}
fn sort(curlist: &Vec<u64>) -> Vec<u64> {
let mut result = curlist.clone();
while !sorted(&result) {
let mut rng = thread_rng();
rng.shuffle(&mut result);
}
result
}
fn main() {
let mut testlist = vec![1,55,88,24,990876,312,67,0,854,13,4,7];
println!("{:?}", sort(&mut testlist))
}