September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,24 @@
|
|||
# As soon as "condition" is true, then emit . and stop:
|
||||
def do_until(condition; next):
|
||||
def u: if condition then . else (next|u) end;
|
||||
u;
|
||||
|
||||
# input: an array
|
||||
def gnomeSort:
|
||||
def swap(i;j): .[i] as $x | .[i]=.[j] | .[j]=$x;
|
||||
|
||||
length as $length
|
||||
# state: [i, j, ary]
|
||||
| [1, 2, .]
|
||||
| do_until( .[0] >= $length;
|
||||
.[0] as $i | .[1] as $j
|
||||
| .[2]
|
||||
# for descending sort, use >= for comparison
|
||||
| if .[$i-1] <= .[$i] then [$j, $j + 1, .]
|
||||
else swap( $i-1; $i)
|
||||
| ($i - 1) as $i
|
||||
| if $i == 0 then [$j, $j + 1, .]
|
||||
else [$i, $j, .]
|
||||
end
|
||||
end )
|
||||
| .[2];
|
||||
|
|
@ -0,0 +1 @@
|
|||
[(2|sqrt), [1], null, 1, 0.5, 2, 1, -3, {"a": "A"}] | gnomeSort
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
$ jq -M -n -f Gnome_sort.jq
|
||||
[
|
||||
null,
|
||||
-3,
|
||||
0.5,
|
||||
1,
|
||||
1,
|
||||
1.4142135623730951,
|
||||
2,
|
||||
[
|
||||
1
|
||||
],
|
||||
{
|
||||
"a": "A"
|
||||
}
|
||||
]
|
||||
Loading…
Add table
Add a link
Reference in a new issue