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,87 @@
' version 21-10-2016
' compile with: fbc -s console
' for boundary checks on array's compile with: fbc -s console -exx
Sub compsort(bs() As Long)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(bs)
Dim As Long ub = UBound(bs)
Dim As Long gap = ub - lb
Dim As Long done, i
Do
gap = Int (gap / 1.3)
If gap < 1 Then gap = 1
done = 0
For i = lb To ub - gap
If bs(i) > bs(i + gap) Then
Swap bs(i), bs(i + gap)
done = 1
End If
Next
Loop Until ((gap = 1) And (done = 0))
End Sub
Sub comp11sort(bs() As Long)
' sort from lower bound to the higher bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(bs)
Dim As Long ub = UBound(bs)
Dim As Long gap = ub - lb
Dim As Long done, i
Do
gap = Int(gap / 1.24733)
If gap = 9 Or gap = 10 Then
gap = 11
ElseIf gap < 1 Then
gap = 1
End If
done = 0
For i = lb To ub - gap
If bs(i) > bs(i + gap) Then
Swap bs(i), bs(i + gap)
done = 1
End If
Next
Loop Until ((gap = 1) And (done = 0))
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 "normal comb sort"
Print "unsorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
compsort(array()) ' sort the array
Print " sorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
Print
Print "comb11 sort"
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
comp11sort(array()) ' sort the 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,18 @@
proc combSort[T](a: var openarray[T]) =
var gap = a.len
var swapped = true
while gap > 1 or swapped:
gap = gap * 10 div 13
if gap == 9 or gap == 10: gap = 11
if gap < 1: gap = 1
swapped = false
var i = 0
for j in gap .. <a.len:
if a[i] > a[j]:
swap a[i], a[j]
swapped = true
inc i
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
combSort a
echo a

View file

@ -0,0 +1,17 @@
function comb_sort(sequence s)
integer gap = length(s)-1
while 1 do
gap = max(floor(gap/1.3),1)
integer swapped = 0
for i=1 to length(s)-gap do
object si = s[i]
if si>s[i+gap] then
s[i] = s[i+gap]
s[i+gap] = si
swapped = 1
end if
end for
if gap=1 and swapped=0 then exit end if
end while
return s
end function

View file

@ -0,0 +1,20 @@
aList = [3,5,1,2,7,4,8,3,6,4,1]
see combsort(aList)
func combsort t
gapd = 1.2473
gap = len(t)
swaps = 0
while gap + swaps > 1
k = 0
swaps = 0
if gap > 1 gap = floor(gap / gapd) ok
for k = 1 to len(t) - gap
if t[k] > t[k + gap]
temp = t[k]
t[k] = t[k + gap]
t[k + gap] = temp
swaps = swaps + 1 ok
next
end
return t

View file

@ -0,0 +1,15 @@
func comb_sort(arr) {
var gap = arr.len;
var swaps = true;
while (gap > 1 || swaps) {
gap.div!(1.25).int! if (gap > 1);
swaps = false;
for i in ^(arr.len - gap) {
if (arr[i] > arr[i+gap]) {
arr[i, i+gap] = arr[i+gap, i];
swaps = true;
}
}
}
return arr;
}

View file

@ -0,0 +1,23 @@
func combSort(inout list:[Int]) {
var swapped = true
var gap = list.count
while gap > 1 || swapped {
gap = gap * 10 / 13
if gap == 9 || gap == 10 {
gap = 11
} else if gap < 1 {
gap = 1
}
swapped = false
for var i = 0, j = gap; j < list.count; i++, j++ {
if list[i] > list[j] {
(list[i], list[j]) = (list[j], list[i])
swapped = true
}
}
}
}

View file

@ -0,0 +1,34 @@
# Input should be the array to be sorted.
def combsort:
# As soon as "condition" is true, emit . and stop:
def do_until(condition; next):
def u: if condition then . else (next|u) end;
u;
def swap(i;j):
if i==j then . else .[i] as $tmp | .[i] = .[j] | .[j] = $tmp end;
. as $in
| length as $length
# state: [gap, swaps, array] where:
# gap is the gap size;
# swaps is a boolean flag indicating a swap has occurred,
# implying that the array might not be sorted;
# array is the current state of the array being sorted
| [ $length, false, $in ]
| do_until( .[0] == 1 and .[1] == false;
# update the gap value for the next "comb":
([1, ((.[0] / 1.25) | floor)] | max) as $gap # minimum gap is 1
# state: [i, swaps, array]
| [0, false, .[2]]
# a single "comb" over the input list:
| do_until( (.[0] + $gap) >= $length;
.[0] as $i
| if .[2][$i] > .[2][$i+$gap] then
[$i+1, true, (.[2]|swap($i; $i+$gap))]
else .[0] += 1
end)
| .[0] = $gap )
| .[2] ;