aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/algorithms/impl/bubble_sort.py
blob: e6aa6458df9b7b203bb4bcbfee7cd48e3604fb3a (plain) (blame)
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
39
40
41
42
43
44
45
46
47
# Copyright 2015 Egor Tensin <Egor.Tensin@gmail.com>
# This file is licensed under the terms of the MIT License.
# See LICENSE.txt for details.

import sys

from ..algorithm import SortingAlgorithm

def bubble_sort(xs):
    while True:
        swapped = False
        for i in range(1, len(xs)):
            if xs[i - 1] > xs[i]:
                xs[i], xs[i - 1] = xs[i - 1], xs[i]
                swapped = True
        if not swapped:
            break
    return xs

def bubble_sort_optimized(xs):
    n = len(xs)
    while True:
        new_n = 0
        for i in range(1, n):
            if xs[i - 1] > xs[i]:
                xs[i], xs[i - 1] = xs[i - 1], xs[i]
                new_n = i
        n = new_n
        if not n:
            break
    return xs

_ALGORITHMS = [
    SortingAlgorithm('bubble_sort', 'Bubble sort', bubble_sort),
    SortingAlgorithm('bubble_sort_optimized', 'Bubble sort (optimized)', bubble_sort_optimized),
]

def _parse_args(args=sys.argv):
    return list(map(int, args[1:]))

def main(args=sys.argv):
    xs = _parse_args(args)
    print(bubble_sort(list(xs)))
    print(bubble_sort_optimized(list(xs)))

if __name__ == '__main__':
    main()