Completed
Push — master ( 03eb77...9029ba )
by Xianshun
01:25
created

String3WayQuickSort   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 38
rs 10
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sort() 0 3 1
A char_at() 0 5 2
B _sort() 0 27 6
1
from pyalgs.algorithms.commons import util
2
3
4
class LSD(object):
5
    R = 256
6
7
    @staticmethod
8
    def sort(a):
9
        W = len(a[0])
10
        aux = [None] * len(a)
11
12
        for d in range(W - 1, -1, -1):
13
            count = [0] * (LSD.R + 1)
14
15
            for i in range(len(a)):
16
                count[ord(a[i][d]) + 1] += 1
17
18
            for r in range(0, LSD.R):
19
                count[r + 1] += count[r]
20
21
            for i in range(len(a)):
22
                aux[count[ord(a[i][d])]] = a[i]
23
                count[ord(a[i][d])] += 1
24
25
            for i in range(len(a)):
26
                a[i] = aux[i]
27
28
29
class MSD(object):
30
    R = 256
31
32
    @staticmethod
33
    def sort(a):
34
        MSD._sort(a, 0, len(a) - 1, 0)
35
36
    @staticmethod
37
    def _sort(a, lo, hi, d):
38
        if hi - lo <= 0:
39
            return
40
41
        count = [0] * (MSD.R + 2)
42
        aux = [None] * (hi - lo + 1)
43
44
        for i in range(lo, hi + 1):
45
            count[MSD.char_at(a[i], d) + 2] += 1
46
47
        for r in range(0, MSD.R):
48
            count[r + 1] += count[r]
49
50
        for i in range(lo, hi + 1):
51
            aux[count[MSD.char_at(a[i], d) + 1]] = a[i]
52
            count[MSD.char_at(a[i], d) + 1] += 1
53
54
        for i in range(lo, hi + 1):
55
            a[i] = aux[i - lo]
56
57
        for r in range(MSD.R):
58
            MSD._sort(a, lo + count[r], lo + count[r + 1] - 1, d + 1)
59
60
    @staticmethod
61
    def char_at(text, d):
62
        if len(text) <= d + 1:
63
            return -1
64
        return ord(text[d])
65
66
67
class String3WayQuickSort(object):
68
    @staticmethod
69
    def sort(a):
70
        String3WayQuickSort._sort(a, 0, len(a) - 1, 0)
71
72
    @staticmethod
73
    def _sort(a, lo, hi, d):
74
        if lo >= hi:
75
            return
76
77
        lt = lo
78
        i = lo
79
        gt = hi
80
81
        c = String3WayQuickSort.char_at(a[lo], d)
82
83
        while i < gt:
84
            cmp = c - String3WayQuickSort.char_at(a[i], d)
85
            if cmp > 0:
86
                util.exchange(a, i, lt)
87
                i += 1
88
                lt += 1
89
            elif cmp < 0:
90
                util.exchange(a, i, gt)
91
                gt -= 1
92
            else:
93
                i += 1
94
95
        String3WayQuickSort._sort(a, lo, lt-1, d)
96
        if c >= 0:
97
            String3WayQuickSort._sort(a, lt, gt, d+1)
98
        String3WayQuickSort._sort(a, gt+1, hi, d)
99
100
    @staticmethod
101
    def char_at(text, d):
102
        if len(text) <= d + 1:
103
            return -1
104
        return ord(text[d])
105