Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -26,7 +26,7 @@ extension op
public program()
{
var list := new int[]{3, 9, 4, 6, 8, 1, 7, 2, 5};
var list := new int[]::(3, 9, 4, 6, 8, 1, 7, 2, 5);
console.printLine("before:", list.asEnumerable());
console.printLine("after :", list.insertionSort().asEnumerable());

View file

@ -6,7 +6,6 @@
X = A(I)
J = I
10 J = J - 1
Can't IF (J.EQ.0 .OR. A(J).LE.X) GO TO 20 in case both sides are ALWAYS evaluated.
IF (J.EQ.0) GO TO 20
IF (A(J).LE.X) GO TO 20
A(J + 1) = A(J)

View file

@ -0,0 +1,33 @@
class InsertionSort {
@:generic
public static function sort<T>(arr:Array<T>) {
for (i in 1...arr.length) {
var value = arr[i];
var j = i - 1;
while (j >= 0 && Reflect.compare(arr[j], value) > 0) {
arr[j + 1] = arr[j--];
}
arr[j + 1] = value;
}
}
}
class Main {
static function main() {
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
3.5, 0.0, -4.1, -9.5];
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
'be', 'self-evident', 'that', 'all',
'men', 'are', 'created', 'equal'];
Sys.println('Unsorted Integers: ' + integerArray);
InsertionSort.sort(integerArray);
Sys.println('Sorted Integers: ' + integerArray);
Sys.println('Unsorted Floats: ' + floatArray);
InsertionSort.sort(floatArray);
Sys.println('Sorted Floats: ' + floatArray);
Sys.println('Unsorted Strings: ' + stringArray);
InsertionSort.sort(stringArray);
Sys.println('Sorted Strings: ' + stringArray);
}
}

View file

@ -0,0 +1,6 @@
def insertSort[X](list: List[X])(implicit ord: Ordering[X]) = {
def insert(list: List[X], value: X) = list.span(x => ord.lt(x, value)) match {
case (lower, upper) => lower ::: value :: upper
}
list.foldLeft(List.empty[X])(insert)
}

View file

@ -0,0 +1,19 @@
void insertion_sort(int[] array) {
var count = 0;
for (int i = 1; i < array.length; i++) {
var val = array[i];
var j = i;
while (j > 0 && val < array[j - 1]) {
array[j] = array[j - 1];
j--;
}
array[j] = val;
}
}
void main() {
int[] array = {4, 65, 2, -31, 0, 99, 2, 83, 782};
insertion_sort(array);
foreach (int i in array)
print("%d ", i);
}