September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -0,0 +1,9 @@
|
|||
(defun stooge-sort (vector &optional (i 0) (j (1- (length vector))))
|
||||
(when (> (aref vector i) (aref vector j))
|
||||
(rotatef (aref vector i) (aref vector j)))
|
||||
(when (> (- j i) 1)
|
||||
(let ((third (floor (1+ (- j i)) 3)))
|
||||
(stooge-sort vector i (- j third))
|
||||
(stooge-sort vector (+ i third) j)
|
||||
(stooge-sort vector i (- j third))))
|
||||
vector)
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
function stoogesort!(a::Array, i::Int=1, j::Int=length(a))
|
||||
if a[j] < a[i]
|
||||
a[[i, j]] = a[[j, i]];
|
||||
end
|
||||
|
||||
if (j - i) > 1
|
||||
t = round(Int, (j - i + 1) / 3)
|
||||
a = stoogesort!(a, i, j - t)
|
||||
a = stoogesort!(a, i + t, j)
|
||||
a = stoogesort!(a, i, j - t)
|
||||
end
|
||||
|
||||
return a
|
||||
end
|
||||
|
||||
x = randn(10)
|
||||
@show x stoogesort!(x)
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
// version 1.1.0
|
||||
|
||||
fun stoogeSort(a: IntArray, i: Int, j: Int) {
|
||||
if (a[j] < a[i]) {
|
||||
val temp = a[j]
|
||||
a[j] = a[i]
|
||||
a[i] = temp
|
||||
}
|
||||
if (j - i > 1) {
|
||||
val t = (j - i + 1) / 3
|
||||
stoogeSort(a, i, j - t)
|
||||
stoogeSort(a, i + t, j)
|
||||
stoogeSort(a, i, j - t)
|
||||
}
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val a = intArrayOf(100, 2, 56, 200, -52, 3, 99, 33, 177, -199)
|
||||
println("Original : ${a.asList()}")
|
||||
stoogeSort(a, 0, a.size - 1)
|
||||
println("Sorted : ${a.asList()}")
|
||||
}
|
||||
|
|
@ -0,0 +1,80 @@
|
|||
/* Rexx */
|
||||
|
||||
call demo
|
||||
return
|
||||
exit
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Stooge sort implementation
|
||||
-- -----------------------------------------------------------------------------
|
||||
::routine stoogeSort
|
||||
use arg rL_, i_ = 0, j_ = .nil
|
||||
if j_ = .nil then j_ = rL_~items() - 1
|
||||
|
||||
if rL_~get(j_) < rL_~get(i_) then do
|
||||
Lt = rL_~get(i_)
|
||||
rL_~set(i_, rL_~get(j_))
|
||||
rL_~set(j_, Lt)
|
||||
end
|
||||
if j_ - i_ > 1 then do
|
||||
t_ = (j_ - i_ + 1) % 3
|
||||
rL_ = stoogeSort(rL_, i_, j_ - t_)
|
||||
rL_ = stoogeSort(rL_, i_ + t_, j_)
|
||||
rL_ = stoogeSort(rL_, i_, j_ - t_)
|
||||
end
|
||||
|
||||
return rL_
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- Demonstrate the implementation
|
||||
-- -----------------------------------------------------------------------------
|
||||
::routine demo
|
||||
|
||||
iList = .nlist~of(1, 4, 5, 3, -6, 3, 7, 10, -2, -5, 7, 5, 9, -3, 7)
|
||||
sList = iList~copy()
|
||||
|
||||
placesList = .nlist~of( -
|
||||
"UK London", "US New York", "US Boston", "US Washington" -
|
||||
, "UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" -
|
||||
)
|
||||
|
||||
sList = stoogeSort(sList)
|
||||
sortedList = stoogeSort(placesList~copy())
|
||||
|
||||
iLists = .list~of(iList, sList)
|
||||
loop ln = 0 to iLists~items() - 1
|
||||
icl = iLists[ln]
|
||||
rpt = ''
|
||||
loop ct = 0 to icl~items() - 1
|
||||
rpt = rpt icl[ct]
|
||||
end ct
|
||||
say '['rpt~strip()~changestr(' ', ',')']'
|
||||
end ln
|
||||
|
||||
say
|
||||
sLists = .list~of(placesList, sortedList)
|
||||
loop ln = 0 to sLists~items() - 1
|
||||
scl = sLists[ln]
|
||||
loop ct = 0 to scl~items() - 1
|
||||
say right(ct + 1, 3)':' scl[ct]
|
||||
end ct
|
||||
say
|
||||
end ln
|
||||
|
||||
return
|
||||
|
||||
-- -----------------------------------------------------------------------------
|
||||
-- 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
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
fcn stoogeSort(list,i=0,j=Void){ if(Void==j) j=list.len() - 1; // default parameters set before call
|
||||
if(list[j]<list[i]) list.swap(i,j);
|
||||
if(j - i >1){
|
||||
t:=(j - i + 1)/3;
|
||||
stoogeSort(list,i , j-t);
|
||||
stoogeSort(list,i+t, j );
|
||||
stoogeSort(list,i , j-t);
|
||||
}
|
||||
list
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
stoogeSort(List(67,-201,0,9,9,231,4)).println();
|
||||
Loading…
Add table
Add a link
Reference in a new issue