Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Sort-an-integer-array/Neko/sort-an-integer-array.neko
Normal file
40
Task/Sort-an-integer-array/Neko/sort-an-integer-array.neko
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/**
|
||||
<doc><h2>Sort integer array, in Neko</h2>
|
||||
<p>Array sort function modified from Haxe codegen with -D neko-source</p>
|
||||
<p>The Neko target emits support code for Haxe basics, sort is included</p>
|
||||
<p>Tectonics:<br />prompt$ nekoc sort.neko<br />prompt$ neko sort</p>
|
||||
</doc>
|
||||
**/
|
||||
|
||||
var sort = function(a) {
|
||||
var i = 0;
|
||||
var len = $asize(a);
|
||||
while ( i < len ) {
|
||||
var swap = false;
|
||||
var j = 0;
|
||||
var max = (len - i) - 1;
|
||||
while ( j < max ) {
|
||||
if ( (a[j] - a[j + 1]) > 0 ) {
|
||||
var tmp = a[j + 1];
|
||||
a[j + 1] = a[j];
|
||||
a[j] = tmp;
|
||||
swap = true;
|
||||
}
|
||||
j += 1;
|
||||
}
|
||||
if ( $not(swap) )
|
||||
break;;
|
||||
i += 1;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
var arr = $array(5,3,2,1,4)
|
||||
$print(arr, "\n")
|
||||
|
||||
/* Sorts in place */
|
||||
sort(arr)
|
||||
$print(arr, "\n")
|
||||
|
||||
/* Also returns the sorted array for chaining */
|
||||
$print(sort($array(3,1,4,1,5,9,2,6,5,3,5,8)), "\n")
|
||||
Loading…
Add table
Add a link
Reference in a new issue