【1からのデータ構造とアルゴリズム】Cocktailソート

import random
from typing import List
def cocktail_sort(numbers:List[int]) -> List[int]:
  len_numbers = len(numbers)
  swapped = True
  start = 0
  end = len_numbers - 1
  while swapped:
    swapped = False

    for i in range(start, end):
      if numbers[i] > numbers[i+1]:
        numbers[i], numbers[i+1] = numbers[i+1], numbers[i]
        swapped = True
    
    if not swapped:
      break
    
    swapped = False
    end = end - 1

    for i in range(end-1, start-1, -1):
      print(i)
      if numbers[i] > numbers[i+1]:
        numbers[i], numbers[i+1] = numbers[i+1], numbers[i]
        swapped = True
    
    start = start + 1
  
  return numbers

if __name__ == '__main__':
  nums = [random.randint(0, 1000) for _ in range(10)]
  print(cocktail_sort(nums))

7

6

5

4

3

2

1

0

6

5

4

3

2

1

[52, 159, 199, 480, 480, 586, 664, 703, 765, 864]