Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,15 @@
void insertionSort(T)(T[] data) pure nothrow @safe @nogc {
foreach (immutable i, value; data[1 .. $]) {
auto j = i + 1;
for ( ; j > 0 && value < data[j - 1]; j--)
data[j] = data[j - 1];
data[j] = value;
}
}
void main() {
import std.stdio;
auto items = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
items.insertionSort;
items.writeln;
}

View file

@ -0,0 +1,31 @@
import std.stdio, std.range, std.algorithm, std.traits;
void insertionSort(R)(R arr)
if (hasLength!R && isRandomAccessRange!R && hasSlicing!R) {
foreach (immutable i; 1 .. arr.length)
bringToFront(arr[0 .. i].assumeSorted.upperBound(arr[i]), arr[i .. i + 1]);
}
void main() {
import std.random, std.container;
auto arr1 = [28, 44, 46, 24, 19, 2, 17, 11, 25, 4];
arr1.insertionSort;
assert(arr1.isSorted);
writeln("arr1 sorted: ", arr1);
auto arr2 = Array!int([28, 44, 46, 24, 19, 2, 17, 11, 25, 4]);
arr2[].insertionSort;
assert(arr2[].isSorted);
writeln("arr2 sorted: ", arr2[]);
// Random data test.
int[10] buf;
foreach (immutable _; 0 .. 100_000) {
auto arr3 = buf[0 .. uniform(0, $)];
foreach (ref x; arr3)
x = uniform(-6, 6);
arr3.insertionSort;
assert(arr3.isSorted);
}
}