Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,41 @@
' version 23-10-2016
' compile with: fbc -s console
Sub stoogesort(s() As Long, l As Long, r As Long)
If s(r) < s(l) Then
Swap s(r), s(l)
End If
If r - l > 1 Then
Var t = (r - l +1) \ 3
stoogesort(s(), l , r - t)
stoogesort(s(), l + t, r )
stoogesort(s(), l , r - t)
End If
End Sub
' ------=< MAIN >=------
Dim As Long i, array(-7 To 7)
Dim As Long a = LBound(array), b = UBound(array)
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
Print "unsorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
stoogesort(array(), LBound(array), UBound(array))
Print " sorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End

View file

@ -0,0 +1,11 @@
proc stoogeSort[T](a: var openarray[T], i, j: int) =
if a[j] < a[i]: swap a[i], a[j]
if j - i > 1:
let t = (j - i + 1) div 3
stoogeSort(a, i, j - t)
stoogeSort(a, i + t, j)
stoogeSort(a, i, j - t)
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
stoogeSort a, 0, a.high
echo a

View file

@ -0,0 +1,13 @@
function stoogesort(sequence s, integer i=1, integer j=length(s))
integer t
if s[j]<s[i] then
{s[i],s[j]} = {s[j],s[i]}
end if
if j-i>1 then
t = floor((j-i+1)/3)
s = stoogesort(s,i, j-t)
s = stoogesort(s,i+t,j )
s = stoogesort(s,i, j-t)
end if
return s
end function

View file

@ -0,0 +1,18 @@
test = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]
stoogeSort(test, 1, len(test))
for i = 1 to 10
see "" + test[i] + " "
next
see nl
func stoogeSort list, i, j
if list[j] < list[i]
temp = list[i]
list[i] = list[j]
list[j] = temp ok
if j - i > 1
t = (j - i + 1)/3
stoogeSort(list, i, j-t)
stoogeSort(list, i+t, j)
stoogeSort(list, i, j-t) ok
return list

View file

@ -0,0 +1,18 @@
func stooge(x, i, j) {
if (x[j] < x[i]) {
x.swap(i, j)
}
if (j-i > 1) {
var t = ((j - i + 1) / 3)
stooge(x, i, j - t)
stooge(x, i + t, j )
stooge(x, i, j - t)
}
}
var a = 10.of { 100.irand }
say "Before #{a}"
stooge(a, 0, a.end)
say "After #{a}"

View file

@ -0,0 +1,22 @@
func stoogeSort(inout arr:[Int], _ i:Int = 0, var _ j:Int = -1) {
if j == -1 {
j = arr.count - 1
}
if arr[i] > arr[j] {
swap(&arr[i], &arr[j])
}
if j - i > 1 {
let t = (j - i + 1) / 3
stoogeSort(&arr, i, j - t)
stoogeSort(&arr, i + t, j)
stoogeSort(&arr, i, j - t)
}
}
var a = [-4, 2, 5, 2, 3, -2, 1, 100, 20]
stoogeSort(&a)
println(a)

View file

@ -0,0 +1,17 @@
def stoogesort:
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
# for efficiency, define an auxiliary function
# that takes as input [L, i, j]
def ss: .[1] as $i | .[2] as $j
| .[0]
| if .[$j] < .[$i] then swap($i;$j) else . end
| if $j - $i > 1 then
(($j - $i + 1) / 3 | floor) as $t
| [., $i, $j-$t] | ss
| [., $i+$t, $j] | ss
| [., $i, $j-$t] | ss
else .
end;
[., 0, length-1] | ss;

View file

@ -0,0 +1,6 @@
([],
[1],
[1,2],
[1,3,2,4],
[1,4,5,3,-6,3,7,10,-2,-5]
) | stoogesort

View file

@ -0,0 +1,6 @@
$ jq -c -n -f stooge_sort.jq
[]
[1]
[1,2]
[1,2,3,4]
[-6,-5,-2,1,3,3,4,5,7,10]