
Sorting Algorithm

This is a sorting algorithm.   It may be applied to a set of data in order to sort it.    
For comparing various sorts, see compare sorts.  
For other sorting algorithms,   see sorting algorithms,   or:

O(n logn) sorts
Heap sort |
Merge sort |
Patience sort |
Quick sort
O(n log2n) sorts
Shell Sort
O(n2) sorts
Bubble sort |
Cocktail sort |
Cocktail sort with shifting bounds |
Comb sort |
Cycle sort |
Gnome sort |
Insertion sort |
Selection sort |
Strand sort
other sorts
Bead sort |
Bogo sort |
Common sorted list |
Composite structures sort |
Custom comparator sort |
Counting sort |
Disjoint sublist sort |
External sort |
Jort sort |
Lexicographical sort |
Natural sorting |
Order by pair comparisons |
Order disjoint list items |
Order two numerical lists |
Object identifier (OID) sort |
Pancake sort |
Quickselect |
Permutation sort |
Radix sort |
Ranking methods |
Remove duplicate elements |
Sleep sort |
Stooge sort |
[Sort letters of a string] |
Three variable sort |
Topological sort |
Tree sort




 This page uses content from Wikipedia. The original article was at Cocktail sort. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance)



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 wikipedia):

function cocktailSort( A : list of sortable items )
 do
   swapped := false
   for each i in 0 to length( A ) - 2 do
     if A[ i ] > A[ i+1 ] then // test whether the two 
                               // elements are in the wrong 
                               // order
       swap( A[ i ], A[ i+1 ] ) // let the two elements
                                // change places
       swapped := true;
   if swapped = false then
     // we can exit the outer loop here if no swaps occurred.
     break do-while loop;
   swapped := false
   for each i in length( A ) - 2 down to 0 do
     if A[ i ] > A[ i+1 ] then
       swap( A[ i ], A[ i+1 ] )
       swapped := true;
 while swapped; // if no elements have been swapped, 
                // then the list is sorted

Related task

   cocktail sort with shifting bounds


define z_HL $00
define z_L  $00
define z_H  $01
define temp $02
define temp2 $03
 
define yINC $04
define yDEC $05
 
set_table:
dex
txa
sta $1200,y
iny
bne set_table	;stores $ff at $1200, $fe at $1201, etc.
 
lda #$12
sta z_H
lda #$00
sta z_L         ;get the base address of the data table
 
 
lda #0
tax
tay		;clear regs
sty yINC        ;yINC = 0
dey             ;LDY #255
sty yDEC        ;yDEC = 255
iny             ;LDY #0
 
JSR COCKTAILSORT
BRK
 
COCKTAILSORT:
;yINC starts at the beginning and goes forward, yDEC starts at the end and goes back.
LDY yINC
LDA (z_HL),y      ;get item Y
STA temp
INY
LDA (z_HL),y      ;get item Y+1
DEY
STA temp2
CMP temp          
bcs doNothing_Up  ;if Y<=Y+1, do nothing. Otherwise swap them.
 
    ;we had to re-arrange an item.
    lda temp
    iny
    sta (z_HL),y   ;store the higher value at base+y+1
    inx ;sort count. If zero at the end, we're done.
    dey
    lda temp2
    sta (z_HL),y   ;store the lower value at base+y 
 
doNothing_Up:
LDY yDEC
LDA (z_HL),y
STA temp
DEY
LDA (z_HL),y
INY
STA temp2
CMP temp
bcc doNothing_Down ;if Y<=Y+1, do nothing. Otherwise swap them.
 
    ;we had to re-arrange an item.
    lda temp
    dey
    sta (z_HL),y   ;store the higher value at base+y+1
    inx ;sort count. If zero at the end, we're done.
    iny
    lda temp2
    sta (z_HL),y   ;store the lower value at base+y 
 
doNothing_Down:
INC yINC
DEC yDEC
LDA yINC
BPL COCKTAILSORT
 
CPX #0
BEQ doneSorting
LDX #0          ;reset the counter
LDY #0
STY yINC
DEY             ;LDY #$FF
STY yDEC
INY		;LDY #0
JMP COCKTAILSORT
doneSorting:
RTS