Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,43 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Bubblesort
|
||||
{
|
||||
Bubblesort[T] (x : list[T]) : list[T]
|
||||
where T : IComparable
|
||||
{
|
||||
def isSorted(y)
|
||||
{
|
||||
|[_] => true
|
||||
|y1::y2::ys => (y1.CompareTo(y2) < 0) && isSorted(y2::ys)
|
||||
}
|
||||
|
||||
def sort(y)
|
||||
{
|
||||
|[y] => [y]
|
||||
|y1::y2::ys => if (y1.CompareTo(y2) < 0) y1::sort(y2::ys)
|
||||
else y2::sort(y1::ys)
|
||||
}
|
||||
|
||||
def loop(y)
|
||||
{
|
||||
if (isSorted(y)) y else {def z = sort(y); loop(z)}
|
||||
}
|
||||
|
||||
match(x)
|
||||
{
|
||||
|[] => []
|
||||
|_ => loop(x)
|
||||
}
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def empty = [];
|
||||
def single = [2];
|
||||
def several = [2, 6, 1, 7, 3, 9, 4];
|
||||
WriteLine(Bubblesort(empty));
|
||||
WriteLine(Bubblesort(single));
|
||||
WriteLine(Bubblesort(several));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
using System;
|
||||
using System.Console;
|
||||
|
||||
module Bubblesort
|
||||
{
|
||||
public static Bubblesort[T](this x : array[T]) : void
|
||||
where T : IComparable
|
||||
{
|
||||
mutable changed = false;
|
||||
def ln = x.Length;
|
||||
|
||||
do
|
||||
{
|
||||
changed = false;
|
||||
foreach (i in [0 .. (ln - 2)])
|
||||
{
|
||||
when (x[i].CompareTo(x[i + 1]) > 0)
|
||||
{
|
||||
x[i] <-> x[i + 1];
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
} while (changed);
|
||||
}
|
||||
|
||||
Main() : void
|
||||
{
|
||||
def several = array[2, 6, 1, 7, 3, 9, 4];
|
||||
several.Bubblesort();
|
||||
foreach (i in several)
|
||||
Write($"$i ");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue