Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -0,0 +1,13 @@
|
|||
def selectionSort(array : Array)
|
||||
(0...array.size-1).each do |i|
|
||||
nextMinIndex = i
|
||||
(i+1...array.size).each do |j|
|
||||
if array[j] < array[nextMinIndex]
|
||||
nextMinIndex = j
|
||||
end
|
||||
end
|
||||
if i != nextMinIndex
|
||||
array.swap(i, nextMinIndex)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
@ -26,7 +26,7 @@ extension op
|
|||
|
||||
public program()
|
||||
{
|
||||
var list := new string[]{"this", "is", "a", "test", "of", "generic", "selection", "sort"};
|
||||
var list := new string[]::("this", "is", "a", "test", "of", "generic", "selection", "sort");
|
||||
|
||||
console.printLine("before:",list.asEnumerable());
|
||||
console.printLine("after:",list.selectionSort().asEnumerable())
|
||||
|
|
|
|||
|
|
@ -0,0 +1,8 @@
|
|||
USING: kernel math sequences sequences.extras ;
|
||||
|
||||
: select ( m n seq -- )
|
||||
[ dup ] 2dip [ <slice> [ ] infimum-by* drop over + ]
|
||||
[ exchange ] bi ;
|
||||
|
||||
: selection-sort! ( seq -- seq' )
|
||||
[ ] [ length dup ] [ ] tri [ select ] 2curry each-integer ;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
IN: scratchpad { 5 -6 3 9 -2 4 -1 -6 5 -5 } selection-sort!
|
||||
|
||||
--- Data stack:
|
||||
{ -6 -6 -5 -2 -1 3 4 5 5 9 }
|
||||
|
|
@ -1,18 +1,38 @@
|
|||
static function selectionSort(arr:Array<Int>) {
|
||||
var len = arr.length;
|
||||
for (index in 0...len)
|
||||
{
|
||||
var minIndex = index;
|
||||
for (remainingIndex in (index+1)...len)
|
||||
{
|
||||
if (arr[minIndex] > arr[remainingIndex]) {
|
||||
minIndex = remainingIndex;
|
||||
}
|
||||
}
|
||||
if (index != minIndex) {
|
||||
var temp = arr[index];
|
||||
arr[index] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
class SelectionSort {
|
||||
@:generic
|
||||
public static function sort<T>(arr:Array<T>) {
|
||||
var len = arr.length;
|
||||
for (index in 0...len) {
|
||||
var minIndex = index;
|
||||
for (remainingIndex in (index+1)...len) {
|
||||
if (Reflect.compare(arr[minIndex], arr[remainingIndex]) > 0)
|
||||
minIndex = remainingIndex;
|
||||
}
|
||||
if (index != minIndex) {
|
||||
var temp = arr[index];
|
||||
arr[index] = arr[minIndex];
|
||||
arr[minIndex] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
SelectionSort.sort(integerArray);
|
||||
Sys.println('Sorted Integers: ' + integerArray);
|
||||
Sys.println('Unsorted Floats: ' + floatArray);
|
||||
SelectionSort.sort(floatArray);
|
||||
Sys.println('Sorted Floats: ' + floatArray);
|
||||
Sys.println('Unsorted Strings: ' + stringArray);
|
||||
SelectionSort.sort(stringArray);
|
||||
Sys.println('Sorted Strings: ' + stringArray);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue