Examples in Gambas (#5) by @vdelacruzjardon
This commit is contained in:
committed by
Aditya Bhargava
parent
e2a4153e80
commit
0aec97dbf2
Binary file not shown.
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=01_binary_search
|
||||||
|
Startup=binary_search
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function binary_search(list As Float[], item As Float) As Variant
|
||||||
|
Dim low, high, Midd, guess As Float
|
||||||
|
low = 0
|
||||||
|
high = list.Length - 1
|
||||||
|
While low <= high
|
||||||
|
Midd = (low + high)
|
||||||
|
guess = list[Midd]
|
||||||
|
If guess = item Then
|
||||||
|
Return Midd
|
||||||
|
Else If guess > item Then
|
||||||
|
high = Midd - 1
|
||||||
|
Else
|
||||||
|
low = Midd + 1
|
||||||
|
Endif
|
||||||
|
Wend
|
||||||
|
Return Null
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
|
||||||
|
Dim my_list As New Float[]
|
||||||
|
my_list = [1, 3, 5, 7, 9]
|
||||||
|
Print binary_search(my_list, 3) '1
|
||||||
|
Print binary_search(my_list, -1) 'null
|
||||||
|
End
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
binary_search
|
||||||
|
01_binary_search
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
02_selection_sort/gambas/01_selection_sort/.gambas/MMAIN
Normal file
BIN
02_selection_sort/gambas/01_selection_sort/.gambas/MMAIN
Normal file
Binary file not shown.
4
02_selection_sort/gambas/01_selection_sort/.project
Normal file
4
02_selection_sort/gambas/01_selection_sort/.project
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
Title=01_selection_sort
|
||||||
|
Startup=MMain
|
||||||
|
|
||||||
40
02_selection_sort/gambas/01_selection_sort/.src/MMain.module
Normal file
40
02_selection_sort/gambas/01_selection_sort/.src/MMain.module
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function findSmallest(arr As Float[]) As Integer
|
||||||
|
Dim smallest As Float
|
||||||
|
Dim i, smallest_index As Integer
|
||||||
|
smallest_index = 0
|
||||||
|
smallest = arr[0]
|
||||||
|
For i = 1 To arr.Length - 1
|
||||||
|
If arr[i] < smallest Then
|
||||||
|
smallest = arr[i]
|
||||||
|
smallest_index = i
|
||||||
|
Endif
|
||||||
|
Next
|
||||||
|
Return smallest_index
|
||||||
|
|
||||||
|
End
|
||||||
|
Function selectionSort(arr As Float[]) As Float[]
|
||||||
|
Dim newArr As New Float[]
|
||||||
|
Dim smallest, i As Integer
|
||||||
|
Dim element As Float
|
||||||
|
For i = 0 To arr.Length - 1
|
||||||
|
smallest = findSmallest(arr)
|
||||||
|
newArr.Push(arr[smallest])
|
||||||
|
arr.Remove(smallest)
|
||||||
|
Next
|
||||||
|
|
||||||
|
Return newArr
|
||||||
|
|
||||||
|
End
|
||||||
|
Public Sub Main()
|
||||||
|
Dim r As New Float[]
|
||||||
|
Dim element As Float
|
||||||
|
Dim i As Integer
|
||||||
|
r = [5, 3, 6, 2, 10]
|
||||||
|
r = selectionSort(r)
|
||||||
|
|
||||||
|
For Each element In r
|
||||||
|
Print element
|
||||||
|
Next
|
||||||
|
End
|
||||||
7
02_selection_sort/gambas/01_selection_sort/.startup
Normal file
7
02_selection_sort/gambas/01_selection_sort/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
MMain
|
||||||
|
01_selection_sort
|
||||||
|
0
|
||||||
|
0
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
BIN
03_recursion/gambas/01_countdown/.gambas/COUNTDOWN
Normal file
BIN
03_recursion/gambas/01_countdown/.gambas/COUNTDOWN
Normal file
Binary file not shown.
7
03_recursion/gambas/01_countdown/.project
Normal file
7
03_recursion/gambas/01_countdown/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=01_countdown
|
||||||
|
Startup=countdown
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
15
03_recursion/gambas/01_countdown/.src/countdown.module
Normal file
15
03_recursion/gambas/01_countdown/.src/countdown.module
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function countdown(i As Integer) As Integer
|
||||||
|
Print i
|
||||||
|
If i <= 0 Then
|
||||||
|
Return
|
||||||
|
Else
|
||||||
|
countdown(i - 1)
|
||||||
|
Endif
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
countdown(5)
|
||||||
|
End
|
||||||
7
03_recursion/gambas/01_countdown/.startup
Normal file
7
03_recursion/gambas/01_countdown/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
countdown
|
||||||
|
01_countdown
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
03_recursion/gambas/02_greet/.gambas/GREET
Normal file
BIN
03_recursion/gambas/02_greet/.gambas/GREET
Normal file
Binary file not shown.
7
03_recursion/gambas/02_greet/.project
Normal file
7
03_recursion/gambas/02_greet/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=02_greet
|
||||||
|
Startup=greet
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
17
03_recursion/gambas/02_greet/.src/greet.module
Normal file
17
03_recursion/gambas/02_greet/.src/greet.module
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function greet2(name As String)
|
||||||
|
Print "how are you, "; name; "?"
|
||||||
|
End
|
||||||
|
Function bye()
|
||||||
|
Print "ok bye!"
|
||||||
|
End
|
||||||
|
Function greet(name As String)
|
||||||
|
Print "hello, "; name; "!"
|
||||||
|
greet2(name)
|
||||||
|
Print "getting ready to say bye..."
|
||||||
|
bye()
|
||||||
|
End
|
||||||
|
Public Sub Main()
|
||||||
|
greet("adit")
|
||||||
|
End
|
||||||
7
03_recursion/gambas/02_greet/.startup
Normal file
7
03_recursion/gambas/02_greet/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
greet
|
||||||
|
02_greet
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
03_recursion/gambas/03_factorial/.gambas/FACTORIAL
Normal file
BIN
03_recursion/gambas/03_factorial/.gambas/FACTORIAL
Normal file
Binary file not shown.
7
03_recursion/gambas/03_factorial/.project
Normal file
7
03_recursion/gambas/03_factorial/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=03_factorial
|
||||||
|
Startup=factorial
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
14
03_recursion/gambas/03_factorial/.src/factorial.module
Normal file
14
03_recursion/gambas/03_factorial/.src/factorial.module
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function fact(x As Float) As Float
|
||||||
|
If x = 1 Then
|
||||||
|
Return 1
|
||||||
|
Else
|
||||||
|
Return x * fact(x - 1)
|
||||||
|
Endif
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
Print fact(5)
|
||||||
|
End
|
||||||
7
03_recursion/gambas/03_factorial/.startup
Normal file
7
03_recursion/gambas/03_factorial/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
factorial
|
||||||
|
03_factorial
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
04_quicksort/gambas/01_loop_sum/.gambas/LOOP_SUM
Normal file
BIN
04_quicksort/gambas/01_loop_sum/.gambas/LOOP_SUM
Normal file
Binary file not shown.
7
04_quicksort/gambas/01_loop_sum/.project
Normal file
7
04_quicksort/gambas/01_loop_sum/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=01_loop_sum
|
||||||
|
Startup=loop_sum
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
15
04_quicksort/gambas/01_loop_sum/.src/loop_sum.module
Normal file
15
04_quicksort/gambas/01_loop_sum/.src/loop_sum.module
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function sum(arr As Integer[]) As Integer
|
||||||
|
Dim total, x As Integer
|
||||||
|
total = 0
|
||||||
|
For x = 0 To arr.Length - 1
|
||||||
|
total += arr[x]
|
||||||
|
Next
|
||||||
|
Return total
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
Print sum([1, 2, 3, 4, 5])
|
||||||
|
End
|
||||||
7
04_quicksort/gambas/01_loop_sum/.startup
Normal file
7
04_quicksort/gambas/01_loop_sum/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
loop_sum
|
||||||
|
01_loop_sum
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
04_quicksort/gambas/02_recursive_count/.gambas/RECURSIVE_COUNT
Normal file
BIN
04_quicksort/gambas/02_recursive_count/.gambas/RECURSIVE_COUNT
Normal file
Binary file not shown.
7
04_quicksort/gambas/02_recursive_count/.project
Normal file
7
04_quicksort/gambas/02_recursive_count/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=02_recursive_count
|
||||||
|
Startup=recursive_count
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function count(list As Float[]) As Float
|
||||||
|
If list.Length = 0 Then
|
||||||
|
Return 0
|
||||||
|
Endif
|
||||||
|
Return 1 + count(list.Copy(1, list.Length - 1))
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
Print count([0, 1, 2, 3, 4, 5]) '6
|
||||||
|
End
|
||||||
7
04_quicksort/gambas/02_recursive_count/.startup
Normal file
7
04_quicksort/gambas/02_recursive_count/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
recursive_count
|
||||||
|
02_recursive_count
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
04_quicksort/gambas/02_recursive_sum/.gambas/RECURSIVE_SUM
Normal file
BIN
04_quicksort/gambas/02_recursive_sum/.gambas/RECURSIVE_SUM
Normal file
Binary file not shown.
7
04_quicksort/gambas/02_recursive_sum/.project
Normal file
7
04_quicksort/gambas/02_recursive_sum/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=02_recursive_sum
|
||||||
|
Startup=recursive_sum
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function sum(list As Float[]) As Float
|
||||||
|
If list.Length = 0 Then
|
||||||
|
Return 0
|
||||||
|
Endif
|
||||||
|
Return list[0] + sum(list.Copy(1, list.Length - 1))
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
Print sum([1, 2, 3, 4])
|
||||||
|
End
|
||||||
7
04_quicksort/gambas/02_recursive_sum/.startup
Normal file
7
04_quicksort/gambas/02_recursive_sum/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
recursive_sum
|
||||||
|
02_recursive_sum
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
04_quicksort/gambas/04_recursive_max/.gambas/RECURSIVE_MAX
Normal file
BIN
04_quicksort/gambas/04_recursive_max/.gambas/RECURSIVE_MAX
Normal file
Binary file not shown.
7
04_quicksort/gambas/04_recursive_max/.project
Normal file
7
04_quicksort/gambas/04_recursive_max/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=04_recursive_max
|
||||||
|
Startup=recursive_max
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function mymax(list As Float[]) As Float
|
||||||
|
Dim sub_max As Float
|
||||||
|
|
||||||
|
If list.Length = 2 Then
|
||||||
|
If list[0] > list[1] Then
|
||||||
|
Return list[0]
|
||||||
|
Else
|
||||||
|
Return list[1]
|
||||||
|
Endif
|
||||||
|
Endif
|
||||||
|
sub_max = mymax(list.Copy(1, list.Length - 1))
|
||||||
|
If list[0] > sub_max Then
|
||||||
|
Return list[0]
|
||||||
|
Else
|
||||||
|
Return sub_max
|
||||||
|
Endif
|
||||||
|
|
||||||
|
End
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
Print mymax([1, 5, 10, 25, 16, 1]) '25
|
||||||
|
|
||||||
|
End
|
||||||
7
04_quicksort/gambas/04_recursive_max/.startup
Normal file
7
04_quicksort/gambas/04_recursive_max/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
recursive_max
|
||||||
|
04_recursive_max
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
BIN
04_quicksort/gambas/05_quicksort/.gambas/QUICKSORT
Normal file
BIN
04_quicksort/gambas/05_quicksort/.gambas/QUICKSORT
Normal file
Binary file not shown.
0
04_quicksort/gambas/05_quicksort/.lock
Normal file
0
04_quicksort/gambas/05_quicksort/.lock
Normal file
7
04_quicksort/gambas/05_quicksort/.project
Normal file
7
04_quicksort/gambas/05_quicksort/.project
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# Gambas Project File 3.0
|
||||||
|
# Compiled with Gambas 3.5.4
|
||||||
|
Title=05_quicksort
|
||||||
|
Startup=quicksort
|
||||||
|
Version=0.0.1
|
||||||
|
TabSize=2
|
||||||
|
Packager=1
|
||||||
39
04_quicksort/gambas/05_quicksort/.src/quicksort.module
Normal file
39
04_quicksort/gambas/05_quicksort/.src/quicksort.module
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
' Gambas module file
|
||||||
|
|
||||||
|
Function quicksort(myarray As Float[]) As Float[]
|
||||||
|
Dim pivot As Float
|
||||||
|
Dim i As Integer
|
||||||
|
Dim less, greater, r As New Float[]
|
||||||
|
If myarray.Length < 2 Then
|
||||||
|
Return myarray
|
||||||
|
Else
|
||||||
|
pivot = myarray[0]
|
||||||
|
For i = 1 To myarray.Length - 1
|
||||||
|
If myarray[i] <= pivot Then
|
||||||
|
less.add(myarray[i])
|
||||||
|
Else
|
||||||
|
greater.add(myarray[i])
|
||||||
|
End If
|
||||||
|
Next
|
||||||
|
|
||||||
|
Endif
|
||||||
|
r.Insert(quicksort(less))
|
||||||
|
r.Add(pivot)
|
||||||
|
r.Insert(quicksort(greater))
|
||||||
|
Return r
|
||||||
|
End
|
||||||
|
|
||||||
|
|
||||||
|
Public Sub Main()
|
||||||
|
Dim myarray As Float[]
|
||||||
|
Dim element As Float
|
||||||
|
Dim x As Integer
|
||||||
|
|
||||||
|
myarray = [10, 5, 2, 3]
|
||||||
|
myarray = quicksort(myarray)
|
||||||
|
|
||||||
|
For Each element In myarray
|
||||||
|
Print element
|
||||||
|
Next
|
||||||
|
|
||||||
|
End
|
||||||
7
04_quicksort/gambas/05_quicksort/.startup
Normal file
7
04_quicksort/gambas/05_quicksort/.startup
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
quicksort
|
||||||
|
05_quicksort
|
||||||
|
0
|
||||||
|
0
|
||||||
|
0.0.1
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user