Family Day update
This commit is contained in:
parent
aac6731f2c
commit
9ad63ea473
2442 changed files with 39761 additions and 8255 deletions
|
|
@ -1,5 +1,9 @@
|
|||
{{Sorting Algorithm}}{{wikipedia|Cocktail sort}}
|
||||
{{Sorting Algorithm}}
|
||||
{{Wikipedia|Cocktail sort}}
|
||||
|
||||
|
||||
The cocktail shaker sort is an improvement on the [[Bubble Sort]].
|
||||
|
||||
The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from [[wp:Cocktail sort|wikipedia]]):
|
||||
'''function''' ''cocktailSort''( A : list of sortable items )
|
||||
'''do'''
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ extension op
|
|||
|
||||
public program()
|
||||
{
|
||||
var list := new int[]{3, 5, 1, 9, 7, 6, 8, 2, 4 };
|
||||
var list := new int[]::(3, 5, 1, 9, 7, 6, 8, 2, 4 );
|
||||
|
||||
console.printLine("before:", list.asEnumerable());
|
||||
console.printLine("after :", list.cocktailSort().asEnumerable())
|
||||
|
|
|
|||
|
|
@ -0,0 +1,18 @@
|
|||
USING: kernel locals math math.ranges sequences ;
|
||||
|
||||
:: cocktail-sort! ( seq -- seq' )
|
||||
f :> swapped! ! bind false to mutable lexical variable 'swapped'. This must be done outside both while quotations so it is in scope of both.
|
||||
[ swapped ] [ ! is swapped true? Then execute body quotation. 'do' executes body quotation before predicate on first pass.
|
||||
f swapped! ! set swapped to false
|
||||
seq length 2 - [| i | ! for each i in 0 to seq length - 2 do
|
||||
i i 1 + [ seq nth ] bi@ > ! is element at index i greater than element at index i + 1?
|
||||
[ i i 1 + seq exchange t swapped! ] when ! if so, swap them and set swapped to true
|
||||
] each-integer
|
||||
swapped [ ! skip to end of loop if swapped is false
|
||||
seq length 2 - 0 [a,b] [| i | ! for each i in seq length - 2 to 0 do
|
||||
i i 1 + [ seq nth ] bi@ > ! is element at index i greater than element at index i + 1?
|
||||
[ i i 1 + seq exchange t swapped! ] when ! if so, swap them and set swapped to true
|
||||
] each
|
||||
] when
|
||||
] do while
|
||||
seq ; ! return the sequence
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
USING: fry kernel math math.ranges namespaces sequences ;
|
||||
|
||||
SYMBOL: swapped?
|
||||
|
||||
: dupd+ ( m obj -- m n obj ) [ dup 1 + ] dip ;
|
||||
|
||||
: 2nth ( n seq -- elt1 elt2 ) dupd+ [ nth ] curry bi@ ;
|
||||
|
||||
: ?exchange ( n seq -- )
|
||||
2dup 2nth > [ dupd+ exchange swapped? on ] [ 2drop ] if ;
|
||||
|
||||
: cocktail-pass ( seq forward? -- )
|
||||
'[ length 2 - 0 _ [ swap ] when [a,b] ] [ ] bi
|
||||
[ ?exchange ] curry each ;
|
||||
|
||||
: (cocktail-sort!) ( seq -- seq' )
|
||||
swapped? off dup t cocktail-pass
|
||||
swapped? get [ dup f cocktail-pass ] when ;
|
||||
|
||||
: cocktail-sort! ( seq -- seq' )
|
||||
[ swapped? get ] [ (cocktail-sort!) ] do while ;
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
class CocktailSort {
|
||||
@:generic
|
||||
public static function sort<T>(arr:Array<T>) {
|
||||
var swapped = false;
|
||||
do {
|
||||
swapped = false;
|
||||
for (i in 0...(arr.length - 1)) {
|
||||
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
|
||||
var temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
}
|
||||
if (!swapped) break;
|
||||
swapped = false;
|
||||
var i = arr.length - 2;
|
||||
while (i >= 0) {
|
||||
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
|
||||
var temp = arr[i];
|
||||
arr[i] = arr[i + 1];
|
||||
arr[i + 1] = temp;
|
||||
swapped = true;
|
||||
}
|
||||
i--;
|
||||
}
|
||||
} while (swapped);
|
||||
}
|
||||
}
|
||||
|
||||
class Main {
|
||||
static function main() {
|
||||
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
|
||||
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
|
||||
3.5, 0.0, -4.1, -9.5];
|
||||
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
|
||||
'be', 'self-evident', 'that', 'all',
|
||||
'men', 'are', 'created', 'equal'];
|
||||
Sys.println('Unsorted Integers: ' + integerArray);
|
||||
CocktailSort.sort(integerArray);
|
||||
Sys.println('Sorted Integers: ' + integerArray);
|
||||
Sys.println('Unsorted Floats: ' + floatArray);
|
||||
CocktailSort.sort(floatArray);
|
||||
Sys.println('Sorted Floats: ' + floatArray);
|
||||
Sys.println('Unsorted Strings: ' + stringArray);
|
||||
CocktailSort.sort(stringArray);
|
||||
Sys.println('Sorted Strings: ' + stringArray);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,71 +1,58 @@
|
|||
/*REXX program sorts an array using the cocktail-sort method, a.k.a.: */
|
||||
/* bidirectional bubble sort, */
|
||||
/* cocktail shaker sort, */
|
||||
/* a selection sort variation,*/
|
||||
/* ripple sort, */
|
||||
/* shuffle sort, */
|
||||
/* shuttle sort, */
|
||||
/* happy hour sort, */
|
||||
/* a bubble sort variation. */
|
||||
/*REXX program sorts an array using the cocktail─sort method, A.K.A.: happy hour sort,*/
|
||||
/* bidirectional bubble sort, */
|
||||
/* cocktail shaker sort, ripple sort,*/
|
||||
/* a selection sort variation, */
|
||||
/* shuffle sort, shuttle sort, or */
|
||||
/* a bubble sort variation. */
|
||||
call gen@ /*generate some array elements. */
|
||||
call show@ 'before sort' /*show unsorted array elements. */
|
||||
say copies('█', 101) /*show a separator line (a fence). */
|
||||
call cocktailSort # /*invoke the cocktail sort subroutine. */
|
||||
call show@ ' after sort' /*show sorted array elements. */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
cocktailSort: procedure expose @.; parse arg N /*N: is number of items. */
|
||||
do until done; done= 1
|
||||
do j=1 for N-1; jp= j+1
|
||||
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end
|
||||
end /*j*/
|
||||
if done then leave /*No swaps done? Finished*/
|
||||
do k=N-1 for N-1 by -1; kp= k+1
|
||||
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end
|
||||
end /*k*/
|
||||
end /*until*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gen@: @.= /*assign a default value for the stem. */
|
||||
@.1 ='---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---'
|
||||
@.2 ='==========symbol====================pip======================================'
|
||||
@.3 ='the juggler ◄─── I'
|
||||
@.4 ='the high priestess [Popess] ◄─── II'
|
||||
@.5 ='the empress ◄─── III'
|
||||
@.6 ='the emperor ◄─── IV'
|
||||
@.7 ='the hierophant [Pope] ◄─── V'
|
||||
@.8 ='the lovers ◄─── VI'
|
||||
@.9 ='the chariot ◄─── VII'
|
||||
@.10='justice ◄─── VIII'
|
||||
@.11='the hermit ◄─── IX'
|
||||
@.12='fortune [the wheel of] ◄─── X'
|
||||
@.13='strength ◄─── XI'
|
||||
@.14='the hanging man ◄─── XII'
|
||||
@.15='death [often unlabeled] ◄─── XIII'
|
||||
@.16='temperance ◄─── XIV'
|
||||
@.17='the devil ◄─── XV'
|
||||
@.18='lightning [the tower] ◄─── XVI'
|
||||
@.18='the stars ◄─── XVII'
|
||||
@.20='the moon ◄─── XVIII'
|
||||
@.21='the sun ◄─── XIX'
|
||||
@.22='judgment ◄─── XX'
|
||||
@.23='the world ◄─── XXI'
|
||||
@.24='the fool [often unnumbered] ◄─── XXII'
|
||||
|
||||
call gen@ /*generate some array elements. */
|
||||
call show@ 'before sort' /*show unsorted array elements.*/
|
||||
call cocktailSort highItem /*invoke cocktail sort subroutine*/
|
||||
call show@ ' after sort' /*show sorted array elements.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────COCKTAILSORT subroutine─────────────*/
|
||||
cocktailSort: procedure expose @.; parse arg N /*N=# of items.*/
|
||||
|
||||
do until done; done=1
|
||||
|
||||
do j=1 for N-1; jp=j+1
|
||||
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end
|
||||
end /*j*/
|
||||
|
||||
if done then leave /*No swaps done? Then we're done*/
|
||||
|
||||
do k=N-1 for N-1 by -1; kp=k+1
|
||||
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end
|
||||
end /*k*/
|
||||
|
||||
end /*until*/
|
||||
return
|
||||
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
|
||||
gen@: @.='' /*assign default value for stem. */
|
||||
@.1 ='---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---'
|
||||
@.2 ='==========symbol====================pip======================================'
|
||||
@.3 ='the juggler ◄─── I'
|
||||
@.4 ='the high priestess [Popess] ◄─── II'
|
||||
@.5 ='the empress ◄─── III'
|
||||
@.6 ='the emperor ◄─── IV'
|
||||
@.7 ='the hierophant [Pope] ◄─── V'
|
||||
@.8 ='the lovers ◄─── VI'
|
||||
@.9 ='the chariot ◄─── VII'
|
||||
@.10='justice ◄─── VIII'
|
||||
@.11='the hermit ◄─── IX'
|
||||
@.12='fortune [the wheel of] ◄─── X'
|
||||
@.13='strength ◄─── XI'
|
||||
@.14='the hanging man ◄─── XII'
|
||||
@.15='death [often unlabeled] ◄─── XIII'
|
||||
@.16='temperance ◄─── XIV'
|
||||
@.17='the devil ◄─── XV'
|
||||
@.18='lightning [the tower] ◄─── XVI'
|
||||
@.18='the stars ◄─── XVII'
|
||||
@.20='the moon ◄─── XVIII'
|
||||
@.21='the sun ◄─── XIX'
|
||||
@.22='judgment ◄─── XX'
|
||||
@.23='the world ◄─── XXI'
|
||||
@.24='the fool [often unnumbered] ◄─── XXII'
|
||||
|
||||
do i=1 while @.i\==''; end /*find how many entries in array.*/
|
||||
|
||||
highItem=i-1 /*adjust for DO loop advancement.*/
|
||||
return
|
||||
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
|
||||
show@: widthH=length(highItem) /*the maximum width of any line. */
|
||||
|
||||
do j=1 for highItem
|
||||
say 'element' right(j,widthH) arg(1)":" @.j
|
||||
end
|
||||
say copies('─',79) /*show a separator line or fence.*/
|
||||
return
|
||||
do #=1 until @.#==''; end; #= #-1 /*find how many entries in the array. */
|
||||
return /* [↑] adjust for DO loop advancement.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
show@: w= length(#); do j=1 for # /*#: is the number of items in @. */
|
||||
say 'element' right(j, w) arg(1)":" @.j
|
||||
end /*j*/ /* ↑ */
|
||||
return /* └─────max width of any line.*/
|
||||
|
|
|
|||
|
|
@ -1,17 +1,12 @@
|
|||
/*──────────────────────────────────COCKTAILSORT2 subroutine────────────*/
|
||||
cocktailSort2: procedure expose @.; parse arg N /*N=# of items.*/
|
||||
/*array items may not contain blanks*/
|
||||
do until done; done=1
|
||||
|
||||
do j=1 for N-1; jp=j+1
|
||||
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j
|
||||
end /*j*/
|
||||
|
||||
if done then leave /*No swaps done? Then we're done*/
|
||||
|
||||
do k=N-1 for N-1 by -1; kp=k+1
|
||||
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k
|
||||
end /*k*/
|
||||
|
||||
end /*until*/
|
||||
return
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
cocktailSort2: procedure expose @.; parse arg N /*N: is the number of items in @. */
|
||||
do until done; done= 1 /*array items may not contain blanks*/
|
||||
do j=1 for N-1; jp= j+1
|
||||
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j
|
||||
end /*j*/
|
||||
if done then leave /*No swaps done? Then we're done. */
|
||||
do k=N-1 for N-1 by -1; kp= k+1
|
||||
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k
|
||||
end /*k*/
|
||||
end /*until*/
|
||||
return
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
void swap(int[] array, int i1, int i2) {
|
||||
if (array[i1] == array[i2])
|
||||
return;
|
||||
var tmp = array[i1];
|
||||
array[i1] = array[i2];
|
||||
array[i2] = tmp;
|
||||
}
|
||||
|
||||
void cocktail_sort(int[] array) {
|
||||
while(true) {
|
||||
bool flag = false;
|
||||
int[] start = {1, array.length - 1};
|
||||
int[] end = {array.length, 0};
|
||||
int[] inc = {1, -1};
|
||||
for (int it = 0; it < 2; ++it) {
|
||||
flag = true;
|
||||
for (int i = start[it]; i != end[it]; i += inc[it])
|
||||
if (array[i - 1] > array[i]) {
|
||||
swap(array, i - 1, i);
|
||||
flag = false;
|
||||
}
|
||||
}
|
||||
if (flag) return;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
int[] array = {5, -1, 101, -4, 0, 1, 8, 6, 2, 3};
|
||||
cocktail_sort(array);
|
||||
foreach (int i in array) {
|
||||
stdout.printf("%d ", i);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue