September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,104 @@
|
|||
' version 11-04-2017
|
||||
' compile with: fbc -s console
|
||||
' for boundry checks on array's compile with: fbc -s console -exx
|
||||
|
||||
' direction = 1, (default) sort ascending
|
||||
' direction <> 1 sort descending
|
||||
' show = 0, (default) do not show sorting
|
||||
' show <> 0, show sorting
|
||||
Sub pancake_sort(a() As Long,direction As Long = 1, show As Long = 0)
|
||||
' array's can have subscript range from -2147483648 to +2147483647
|
||||
Dim As Long i, j, n
|
||||
Dim As Long lb = LBound(a)
|
||||
Dim As Long ub = UBound(a)
|
||||
|
||||
If show <> 0 Then ' show initial state
|
||||
For j = lb To ub
|
||||
Print Using "####"; a(j);
|
||||
Next
|
||||
Print
|
||||
End If
|
||||
|
||||
For i = ub To lb +1 Step -1
|
||||
|
||||
n = lb
|
||||
For j = lb +1 To i
|
||||
If direction = 1 Then
|
||||
If a(n) < a(j) Then n = j
|
||||
Else
|
||||
If a(n) > a(j) Then n = j
|
||||
End If
|
||||
Next
|
||||
|
||||
If n < i Then
|
||||
If n > lb Then
|
||||
For j = lb To lb + ((n - lb) \ 2)
|
||||
Swap a(j), a(lb + n - j)
|
||||
Next
|
||||
|
||||
If show <> 0 Then
|
||||
For j = lb To ub
|
||||
Print Using "####"; a(j);
|
||||
Next
|
||||
Print
|
||||
End If
|
||||
|
||||
End If
|
||||
|
||||
For j = lb To lb + ((i - lb) \ 2)
|
||||
Swap a(j), a(lb + i - j)
|
||||
Next
|
||||
|
||||
If show <> 0 Then
|
||||
For j = lb To ub
|
||||
Print Using "####"; a(j);
|
||||
Next
|
||||
Print
|
||||
End If
|
||||
|
||||
End If
|
||||
Next
|
||||
|
||||
End Sub
|
||||
|
||||
' ------=< MAIN >=------
|
||||
|
||||
Dim As Long i, array(-7 To 7)
|
||||
Dim As Long lb = LBound(array)
|
||||
Dim As Long ub = UBound(array)
|
||||
|
||||
Randomize Timer
|
||||
For i = lb To ub : array(i) = i : Next
|
||||
For i = lb To ub ' little shuffle
|
||||
Swap array(i), array(Int(Rnd * (ub - lb +1) + lb))
|
||||
Next
|
||||
|
||||
Print "unsorted ";
|
||||
For i = lb To ub
|
||||
Print Using "####"; array(i);
|
||||
Next
|
||||
Print : Print
|
||||
|
||||
pancake_sort(array())
|
||||
|
||||
Print " sorted ";
|
||||
For i = lb To ub
|
||||
Print Using "####"; array(i);
|
||||
Next
|
||||
|
||||
Print : Print
|
||||
Dim As Long l(10 To ...) = {0, -30, 20, -10, 0, 10, -20}
|
||||
|
||||
pancake_sort(l(),0,1) ' sort array l, ascending and show process
|
||||
|
||||
Print : Print " sorted l()";
|
||||
For i = LBound(l) To UBound(l)
|
||||
Print Using "####"; l(i);
|
||||
Next
|
||||
Print
|
||||
|
||||
' empty keyboard buffer
|
||||
While Inkey <> "" : Wend
|
||||
Print : Print "hit any key to end program"
|
||||
Sleep
|
||||
End
|
||||
Loading…
Add table
Add a link
Reference in a new issue