Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,60 @@
@ECHO OFF
SETLOCAL EnableExtensions EnableDelayedExpansion
:: GnomeSort.cmd in WinNT Batch using pseudo array.
:: Set the number of random elements to sort.
SET numElements=100
:: Decrement numElements for use in zero-based loops as in (0, 1, %numElements% - 1).
SET /A tmpElements=%numElements% - 1
:: Create array of random numbers and output to file.
ECHO GnomeSort Random Input 0 to %tmpElements%:>%~n0.txt
FOR /L %%X IN (0, 1, %tmpElements%) DO (
SET array[%%X]=!RANDOM!
ECHO !array[%%X]!>>%~n0.txt
)
:GnomeSort
:: Initialize the pointers i-1, i, and j.
SET gs1=0
SET gs2=1
SET gs3=2
:GS_Loop
:: Implementing a WHILE loop in WinNT batch using GOTO. It only executes
:: if the condition is true and continues until the condition is false.
:: First, display [i-1][j - 2] to the Title Bar.
SET /A gsTmp=%gs3% - 2
TITLE GnomeSort:[%gs1%][%gsTmp%] of %tmpElements%
:: ...then start Main Loop. It's a direct implementation of the
:: pseudo code supplied by Rosetta Code. I had to add an additional
:: pointer to represent i-1, because of limitations in WinNT Batch.
IF %gs2% LSS %numElements% (
REM if i-1 <= i advance pointers to next unchecked element, then loop.
IF !array[%gs1%]! LEQ !array[%gs2%]! (
SET /A gs1=%gs3% - 1
SET /A gs2=%gs3%
SET /A gs3=%gs3% + 1
) ELSE (
REM ... else swap i-1 and i, decrement pointers to check previous element, then loop.
SET gsTmp=!array[%gs1%]!
SET array[%gs1%]=!array[%gs2%]!
SET array[%gs2%]=!gsTmp!
SET /A gs1-=1
SET /A gs2-=1
REM if first element has been reached, set pointers to next unchecked element.
IF !gs2! EQU 0 (
SET /A gs1=%gs3% - 1
SET /A gs2=%gs3%
SET /A gs3=%gs3% + 1
)
)
GOTO :GS_Loop
)
TITLE GnomeSort:[%gs1%][%gsTmp%] - Done!
:: write sorted elements out to file
ECHO.>>%~n0.txt
ECHO GnomeSort Sorted Output 0 to %tmpElements%:>>%~n0.txt
FOR /L %%X IN (0, 1, %tmpElements%) DO ECHO !array[%%X]!>>%~n0.txt
ENDLOCAL
EXIT /B 0

View file

@ -1,22 +1,30 @@
#include <algorithm>
#include <iterator>
#include <iostream>
template<typename RandomAccessIterator>
void gnomeSort(RandomAccessIterator begin, RandomAccessIterator end) {
RandomAccessIterator i = begin + 1;
RandomAccessIterator j = begin + 2;
void gnome_sort(RandomAccessIterator begin, RandomAccessIterator end) {
auto i = begin + 1;
auto j = begin + 2;
while(i < end) {
if(*(i - 1) <= *i) {
while (i < end) {
if (!(*i < *(i - 1))) {
i = j;
++j;
} else {
std::iter_swap(i - 1, i);
--i;
if(i == begin) {
if (i == begin) {
i = j;
++j;
}
}
}
}
int main() {
int a[] = {100, 2, 56, 200, -52, 3, 99, 33, 177, -199};
gnome_sort(std::begin(a), std::end(a));
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}

View file

@ -0,0 +1,38 @@
class
GNOME_SORT
feature
sort(ar: ARRAY[INTEGER]): ARRAY[INTEGER]
-- sort array ar with gnome sort
require
array_not_void: ar/= VOID
local
i,j, ith: INTEGER
do
from
i:= 2
j:= 3
until
i>ar.count
loop
if ar[i-1] <= ar[i] then
i:= j
j:= j+1
else
ith := ar[i-1]
ar[i-1] := ar[i]
ar[i] := ith
i:= i-1
if i=1 then
i:=j
j:= j+1
end
end
end
Result := ar
ensure
same_length: ar.count = Result.count
same_items: Result.same_items (ar)
end
end

View file

@ -0,0 +1,21 @@
class
APPLICATION
inherit
ARGUMENTS
create
make
feature
make
do
test:= <<7, 99, -7, 1, 0, 25, -10>>
create gnome
test:= gnome.sort (test)
across test as ar loop io.put_string( ar.item.out + "%T") end
end
test: ARRAY[INTEGER]
gnome: GNOME_SORT[INTEGER]
end

View file

@ -0,0 +1,24 @@
fn gnomeSort arr =
(
local i = 2
local j = 3
while i <= arr.count do
(
if arr[i-1] <= arr[i] then
(
i = j
j += 1
)
else
(
swap arr[i-1] arr[i]
i -= 1
if i == 1 then
(
i = j
j += 1
)
)
)
return arr
)

View file

@ -0,0 +1,4 @@
a = for i in 1 to 10 collect random 1 20
#(20, 10, 16, 2, 19, 12, 11, 3, 5, 16)
gnomesort a
#(2, 3, 5, 10, 11, 12, 16, 16, 19, 20)

View file

@ -0,0 +1,31 @@
/*REXX program sorts a stemmed array using the gnome-sort algorithm.*/
call gen@ /*generate the @. array elements.*/
call show@ 'before sort' /*show "before" array elements.*/
call gnomeSort # /*invoke the infamous gnome sort.*/
call show@ ' after sort' /*show "after" array elements.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────GNOMESORT subroutine────────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2 /*n=num items.*/
do j=3 while k<=n; km=k-1 /*KM=prev item*/
if @.km<<=@.k then do; k=j; iterate; end /*OK so far···*/
_=@.km; @.km=@.k; @.k=_ /*swap 2 entries in the @. array.*/
k=k-1; if k==1 then k=j; else j=j-1 /*test index 1*/
end /*j*/ /* [↑] perform gnome sort on @.*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: !=... 'deadbeef'x ...; @.=! /*default none-value; allows null*/
@.1 = '---the seven virtues---' /* [↓] indent the seven virtues.*/
@.2 = '=======================' ; @.6 = 'Fortitude'
@.3 = 'Faith' ; @.7 = 'Justice'
@.4 = 'Hope' ; @.8 = 'Prudence'
@.5 = 'Charity [Love]' ; @.9 = 'Temperance'
do #=1 while @.#\==!; end /*find the # of items in @ array.*/
#=#-1 /*adjust the numer of items by 1.*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: do j=1 for # /* [↓] display all items for @. */
say ' element' right(j,length(#)) arg(1)":" @.j
end /*j*/ /* [↑] right justify the J num.*/
say copies('',60) /*show a separator line that fits*/
return

View file

@ -0,0 +1,46 @@
/* REXX ---------------------------------------------------------------
* 28.06.2014 Walter Pachl cf ooRexx version 2
* only style changes (reformatting) and adapt for ooRexx compatibility
* NOTE that leading blanks are ignored in the comparison (' Justice')
* unless strict comparison is used (i.e., 'If x.km<<=x.k Then')
* 30.06.2014 WP added the missing else clause
*--------------------------------------------------------------------*/
/* generate the array elements. */
Call gen '---the seven virtues---','=======================',,
'Faith','Hope','Charity [Love]','Fortitude',' Justice',,
'Prudence','Temperance'
Call show 'before sort' /* show "before" array elements.*/
Call gnomeSort /* invoke the infamous gnome sort.*/
Call show ' after sort' /* show "after" array elements.*/
Exit /* done. */
gnomesort: Procedure Expose x.
k=2
Do j=3 While k<=x.0
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: Procedure Expose x.
Do i=1 To arg()
x.i=arg(i)
End
x.0=arg()
Return
show: Procedure Expose x.
Say arg(1)
Do i=1 To x.0
Say 'element' right(i,2) x.i
End
Return

View file

@ -1,41 +0,0 @@
/*REXX program sorts an array using the gnome-sort method. */
call gen@ /*generate the array elements. */
call show@ 'before sort' /*show "before" array elements.*/
call gnomeSort highItem /*invoke the infamous gnome sort.*/
call show@ ' after sort' /*show "after" array elements.*/
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────────gnomeSORT subroutine─────────────*/
gnomeSort: procedure expose @.; parse arg n; k=2
do j=3 while k<=n; km=k-1
if @.km<=@.k then k=j
else do
_=@.km /*swap two entries in the array. */
@.km=@.k
@.k=_
k=k-1; if k==1 then k=j
end
end /*j*/
return
/*─────────────────────────────────────GEN@ subroutine──────────────────*/
gen@: @.= /*assign a default value (null). */
@.1='---the seven virtues---'
@.2='======================='
@.3='Faith'
@.4='Hope'
@.5='Charity [Love]'
@.6='Fortitude'
@.7='Justice'
@.8='Prudence'
@.9='Temperance'
do highItem=1 while @.highItem\=='' /*find how many entries in array.*/
end /*not much of a DO loop, eh? */
highItem=highItem-1 /*because of DO, adjust highItem.*/
return
/*─────────────────────────────────────SHOW@ subroutine─────────────────*/
show@: widthH=length(highItem) /*the maximum width of any line. */
do j=1 for highItem /*show and tell time for array. */
say 'element' right(j,widthH) arg(1)":" @.j /*make it pretty.*/
end /*j*/
say copies('',60) /*show a separator line that fits*/
return