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

@ -0,0 +1,25 @@
// version 1.1.0
fun <T: Comparable<T>> gnomeSort(a: Array<T>, ascending: Boolean = true) {
var i = 1
var j = 2
while (i < a.size)
if (ascending && (a[i - 1] <= a[i]) ||
!ascending && (a[i - 1] >= a[i]))
i = j++
else {
val temp = a[i - 1]
a[i - 1] = a[i]
a[i--] = temp
if (i == 0) i = j++
}
}
fun main(args: Array<String>) {
val array = arrayOf(100, 2, 56, 200, -52, 3, 99, 33, 177, -199)
println("Original : ${array.asList()}")
gnomeSort(array)
println("Sorted (asc) : ${array.asList()}")
gnomeSort(array, false)
println("Sorted (desc) : ${array.asList()}")
}

View file

@ -0,0 +1,95 @@
/* Rexx */
call demo
return
exit
-- -----------------------------------------------------------------------------
-- Gnome sort implementation
-- -----------------------------------------------------------------------------
::routine gnomeSort
use arg ra, sAsc = ''
if sAsc = '' then sAsc = isTrue()
sDsc = \sAsc -- Ascending/descending switches
i_ = 1
j_ = 2
loop label i_ while i_ < ra~items()
ctx = (ra~get(i_ - 1))~compareTo(ra~get(i_))
if (sAsc & ctx <= 0) | (sDsc & ctx >= 0) then do
i_ = j_
j_ = j_ + 1
end
else do
swap = ra~get(i_)
ra~set(i_, ra~get(i_ - 1))
ra~set(i_ - 1, swap)
i_ = i_ - 1
if i_ = 0 then do
i_ = j_
j_ = j_ + 1
end
end
end i_
return ra
-- -----------------------------------------------------------------------------
-- Demonstrate the implementation
-- -----------------------------------------------------------------------------
::routine demo
placesList = .nlist~of( -
"UK London", "US New York", "US Boston", "US Washington" -
, "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" -
)
lists = .nlist~of( -
placesList -
, gnomeSort(placesList~copy()) -
)
loop ln = 0 to lists~items() - 1
cl = lists[ln]
loop ct = 0 to cl~items() - 1
say right(ct + 1, 3)':' cl[ct]
end ct
say
end ln
i_.0 = 3
i_.1 = .nlist~of(3, 3, 1, 2, 4, 3, 5, 6)
i_.2 = gnomeSort(i_.1~copy(), isTrue())
i_.3 = gnomeSort(i_.1~copy(), isFalse())
loop s_ = 1 to i_.0
ss = ''
loop x_ = 0 to i_.s_~items() - 1
ss ||= right(i_.s_~get(x_), 3)' '
end x_
say strip(ss, 'T')
end s_
return
-- -----------------------------------------------------------------------------
::routine isTrue
return 1 == 1
-- -----------------------------------------------------------------------------
::routine isFalse
return \isTrue()
-- -----------------------------------------------------------------------------
-- Helper class. Map get and set methods for easier conversion from java.util.List
-- -----------------------------------------------------------------------------
::class NList mixinclass List public
-- Map get() to at()
::method get
use arg ix
return self~at(ix)
-- Map set() to put()
::method set
use arg ix, item
self~put(item, ix)
return

View file

@ -0,0 +1,40 @@
/* REXX ---------------------------------------------------------------
* 28.06.2014 Walter Pachl
* 30.06.2014 corrected in sync with REXX version 2
*--------------------------------------------------------------------*/
Call gen /* generate the array elements. */
Call show 'before sort' /* show "before" array elements.*/
Call gnomeSort x /* invoke the infamous gnome sort.*/
Call show ' after sort' /* show "after" array elements.*/
Exit /* done. */
gnomesort: Procedure
Use Arg x
n=x~items
k=2
Do j=3 While k<=n
km=k-1
If x[km]<=x[k] Then
k=j
Else Do
t=x[km]; x[km]=x[k]; x[k]=t /* swap two entries in the array. */
k=k-1
If k==1 Then
k=j
Else
j=j-1
End
End
Return
gen:
x=.array~of('---the seven virtues---','=======================',,
'Faith','Hope','Charity [Love]','Fortitude',' Justice',,
'Prudence','Temperance')
Return
show:
Say arg(1)
Do i=1 To x~items
Say 'element' right(i,2) x[i]
End
Return

View file

@ -6,6 +6,11 @@ call gnomeSort # /*invoke the well─known gnom
call show ' after sort' /*display the after array elements.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1='---the seven virtues---'; @.4="Hope" ; @.7='Justice'
@.2='======================='; @.5="Charity [LOVE]"; @.8='Prudence'
@.3='Faith' ; @.6="Fortitude" ; @.9='Temperance'
do #=1 while @.#\==''; end; #=#-1; w=length(#); return /*find # items.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2 /*N: is number items. */
do j=3 while k<=n; p=k-1 /*P: is previous item.*/
if @.p<<=@.k then do; k=j; iterate; end /*order is OK so far. */
@ -14,12 +19,4 @@ gnomeSort: procedure expose @.; parse arg n; k=2 /*N: is number
end /*j*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
gen: @.=; @.1 = '---the seven virtues---' ; @.5 = "Charity [Love]"
@.2 = '=======================' ; @.6 = "Fortitude"
@.3 = 'Faith' ; @.7 = "Justice"
@.4 = 'Hope' ; @.8 = "Prudence"
@.9 = "Temperance"
do #=1 while @.#\==''; end; #=#-1; w=length(#) /*find # of items.*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return
show: do j=1 for #; say ' element' right(j,w) arg(1)":" @.j; end; return

View file

@ -0,0 +1,13 @@
fcn gnomeSort(a){
i,j,size := 1,2,a.len();
while(i < size){
if(a[i-1] > a[i]){ // for descending sort, use < for comparison
a.swap(i-1,i);
i = i - 1;
if(i) continue;
}
i = j;
j = j + 1;
}//while
a
}

View file

@ -0,0 +1 @@
gnomeSort("This is a test".split("")).println();