Cocktail Sort

Python

Public Domain

Cocktail Sort in Ruby

Download (right click, save as, rename as appropriate)

Embed

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import random
import sys

foo = []

for b in range(1,330):
  foo.append(random.randint(0,99))

#print foo
  

#print cocktail_sort(foo)


for k in range(len(foo)-1, 0, -1):
    swapped = False
    for i in range(k, 0, -1):
        if foo[i]<foo[i-1]:
            a = foo[i]
            b = foo[i-1]
            foo[i] = b
            foo[i-1] = a
            swapped = True

    for b in range(len(foo)):
      sys.stdout.write(str(foo[b]))
    sys.stdout.write("\n")

    for i in range(k):
        if foo[i] > foo[i+1]:
            a = foo[i]
            b = foo[i+1]
            foo[i] = b
            foo[i+1] = a
            swapped = True
    for b in range(len(foo)):
      sys.stdout.write(str(foo[b]))
    sys.stdout.write("\n")