September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,82 +0,0 @@
::Pancake Sort
::Batch File Implementation
::
::Using the "Classic XOR trick" to swap integer values of two variables...
::...IF the variable names are different...
@echo off
setlocal enabledelayedexpansion
::Initial Values and Variables
set "range=0"
set "output="
set "list=-2 0 -1 5 2 7 4 3 6 -1 7 2 1 8"
::Put the sequence of integers (ONLY) on the list variable.
::Please do not "play" with the list variable.
::or else the code will not work or crash.
for %%l in (!list!) do (
set num!range!=%%l
set /a range+=1
)
set /a range-=1
::Scramble
for /l %%l in (%range%,-1,1) do (
set /a n=^(%random% %% %%l^)
set /a num%%l^^^^=num!n!
set /a num!n!^^^^=num%%l
set /a num%%l^^^^=num!n!
)
::/Scramble (Remove this if you do not want to scramble the integers)
::Display initial condition
for /l %%l in (0,1,%range%) do set "output=!output! !num%%l!"
echo.Initial Sequence:
echo.
echo. ^>^> !output!
echo.
echo Sorting:
echo.
::Sort begins!
for /l %%m in (%range%,-1,1) do (
set n=0
for /l %%l in (1,1,%%m) do (
set /a tmp_var1=num!n!
if !tmp_var1! lss !num%%l! set n=%%l
)
if !n! lss %%m (
if !n! gtr 0 (
set /a tmp_var1=!n!/2
for /l %%l in (0,1,!tmp_var1!) do (
set /a tmp_var2=!n!-%%l
if !tmp_var2! neq %%l (
set /a num!tmp_var2!^^^^=num%%l
set /a num%%l^^^^=num!tmp_var2!
set /a num!tmp_var2!^^^^=num%%l
)
)
set output=
for /l %%x in (0,1,%range%) do set "output=!output! !num%%x!"
echo. ^>^> !output!
)
set /a tmp_var3=%%m/2
for /l %%l in (0,1,!tmp_var3!) do (
set /a tmp_var4=%%m-%%l
if !tmp_var4! neq %%l (
set /a num!tmp_var4!^^^^=num%%l
set /a num%%l^^^^=num!tmp_var4!
set /a num!tmp_var4!^^^^=num%%l
)
)
set output=
for /l %%x in (0,1,%range%) do set "output=!output! !num%%x!"
echo. ^>^> !output!
)
)
)
::We are done.
echo DONE^^!
exit /b 0

View file

@ -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

View file

@ -0,0 +1,43 @@
// version 1.1.2
class PancakeSort(private val a: IntArray) {
init {
for (n in a.size downTo 2) { // successively reduce size of array by 1
val index = indexOfMax(n) // find index of largest
if (index != n - 1) { // if it's not already at the end
if (index > 0) { // if it's not already at the beginning
flip(index) // move largest to beginning
println("${a.contentToString()} after flipping first ${index + 1}")
}
flip(n - 1) // move largest to end
println("${a.contentToString()} after flipping first $n")
}
}
}
private fun indexOfMax(n: Int): Int {
var index = 0
for (i in 1 until n) {
if (a[i] > a[index]) index = i
}
return index
}
private fun flip(index: Int) {
var i = index
var j = 0
while (j < i) {
val temp = a[j]
a[j] = a[i]
a[i] = temp
j++
i--
}
}
}
fun main(args: Array<String>) {
val a = intArrayOf(7, 6, 9, 2, 4, 8, 1, 3, 5)
println("${a.contentToString()} initially")
PancakeSort(a)
}

View file

@ -6,17 +6,18 @@ call pancakeSort # /*invoke the pancake sort.
call show ' after sort' /*display the AFTER array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
flip: parse arg y; do i=1 for (y+1)%2; yyy=y-i+1; _=@.i; @.i=@.yyy; @.yyy=_; end
return /*swap: ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑ */
flip: parse arg y; do i=1 for (y+1)%2; yyy=y-i+1; _=@.i; @.i=@.yyy; @.yyy=_; end; return
show: do k=1 for #; say @element right(k,length(#)) arg(1)':' right(@.k,9); end; return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: fibs= '-55 -21 -1 -8 -8 -21 -55 0 0' /*some non─positive Fibonacci numbers, */
@element=right('element',21) /* most of which are repeated.
some paired bread primes which are of the form: (p-3)÷2 and 2p+3
where p is a prime. Bread primes are related to sandwich & meat primes.
*/
@element= right('element', 21) /* most Fibs of which are repeated.*/
/* ┌◄─┬──◄─ some paired bread primes which are of the form: (P-3)÷2 and 2∙P+3 */
/* │ │ where P is a prime. Bread primes are related to sandwich & meat primes*/
/* ↓ ↓ ──── ──── ───── ────── ────── ────── ────── ─────── ─────── ─────── ──────*/
bp=2 17 5 29 7 37 13 61 43 181 47 197 67 277 97 397 113 461 137 557 167 677 173 701,
797 1117 307 1237 1597 463 1861 467
$=bp fibs; #=words($) /*combine the two lists; get # of items*/
$=bp fibs; #=words($) /*combine the two lists; get # of items*/
do j=1 for #; @.j=word($,j); end /*◄─── obtain a number from the $ list.*/
return /* [↑] populate the @. array with #s*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
@ -28,5 +29,3 @@ pancakeSort: procedure expose @.; parse arg N
call flip ?; call flip N
end /*N*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do k=1 for #; say @element right(k,length(#)) arg(1)':' right(@.k,9); end; return

View file

@ -0,0 +1,7 @@
fcn pancakeSort(a){
foreach i in ([a.len()-1..1,-1]){
j := a.index((0).max(a[0,i+1])); // min for decending sort
if(i != j){ a.swap(0,j); a.swap(0,i); }
}
a
}

View file

@ -0,0 +1 @@
j := (i+1).reduce('wrap(x,y){ if(a[x]>a[y]) x else y });

View file

@ -0,0 +1 @@
pancakeSort(List(7,6,9,2,4,8,1,3,5)).println();