Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Cocktail shaker sort
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Pseudocode== The simplest form goes through the whole list each time: '''procedure''' cocktailShakerSort(A ''':''' list of sortable items) '''is''' '''do''' swapped := false '''for each''' i '''in''' 0 '''to''' length(A) β 1 '''do:''' '''if''' A[i] > A[i + 1] '''then''' <span style="color:green">// test whether the two elements are in the wrong order</span> swap(A[i], A[i + 1]) <span style="color:green">// let the two elements change places</span> swapped := true '''end if''' '''end for''' '''if not''' swapped '''then''' <span style="color:green">// we can exit the outer loop here if no swaps occurred.</span> '''break do-while loop''' '''end if''' swapped := false '''for each''' i '''in''' length(A) β 1 '''to''' 0 '''do:''' '''if''' A[i] > A[i + 1] '''then''' swap(A[i], A[i + 1]) swapped := true '''end if''' '''end for''' '''while''' swapped <span style="color:green">// if no elements have been swapped, then the list is sorted</span> '''end procedure''' The first rightward pass will shift the largest element to its correct place at the end, and the following leftward pass will shift the smallest element to its correct place at the beginning. The second complete pass will shift the second largest and second smallest elements to their correct places, and so on. After ''i'' passes, the first ''i'' and the last ''i'' elements in the list are in their correct positions, and do not need to be checked. By shortening the part of the list that is sorted each time, the number of operations can be halved (see [[Bubble_sort#Alternative_implementations|bubble sort]]). This is an example of the algorithm in MATLAB/OCTAVE with the optimization of remembering the last swap index and updating the bounds. <syntaxhighlight lang="matlab"> function A = cocktailShakerSort(A) % `beginIdx` and `endIdx` marks the first and last index to check beginIdx = 1; endIdx = length(A) - 1; while beginIdx <= endIdx newBeginIdx = endIdx; newEndIdx = beginIdx; for ii = beginIdx:endIdx if A(ii) > A(ii + 1) [A(ii+1), A(ii)] = deal(A(ii), A(ii+1)); newEndIdx = ii; end end % decreases `endIdx` because the elements after `newEndIdx` are in correct order endIdx = newEndIdx - 1; for ii = endIdx:-1:beginIdx if A(ii) > A(ii + 1) [A(ii+1), A(ii)] = deal(A(ii), A(ii+1)); newBeginIdx = ii; end end % increases `beginIdx` because the elements before `newBeginIdx` are in correct order beginIdx = newBeginIdx + 1; end end </syntaxhighlight>
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Cocktail shaker sort
(section)
Add topic