Sorting algorithms/Cocktail sort with shifting bounds: Difference between revisions

Python version
m (Added Perl example)
(Python version)
Line 1,057:
{{12,2,1,9,4,10,8,5,3,11,6,7},{1,2,3,4,5,6,7,8,9,10,11,12}}
{{"one","two","three","four"},{"four","one","three","two"}}
</pre>
=={{header|Python}}==
<lang python>
"""
 
Python example of
 
http://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort_with_shifting_bounds
 
based on
 
http://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort#Python
 
"""
def cocktailshiftingbounds(A):
beginIdx = 0
endIdx = len(A) - 1
while beginIdx <= endIdx:
newBeginIdx = endIdx
newEndIdx = beginIdx
for ii in range(beginIdx,endIdx):
if A[ii] > A[ii + 1]:
A[ii+1], A[ii] = A[ii], A[ii+1]
newEndIdx = ii
endIdx = newEndIdx
for ii in range(endIdx,beginIdx-1,-1):
if A[ii] > A[ii + 1]:
A[ii+1], A[ii] = A[ii], A[ii+1]
newBeginIdx = ii
beginIdx = newBeginIdx + 1
test1 = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
cocktailshiftingbounds(test1)
print(test1)
test2=list('big fjords vex quick waltz nymph')
cocktailshiftingbounds(test2)
print(''.join(test2))
</lang>
 
{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
abcdefghiijklmnopqrstuvwxyz
</pre>