mk-mode BLOG

このブログは自作の自宅サーバに構築した Debian GNU/Linux で運用しています。
PC・サーバ構築等の話題を中心に公開しております。(クローンサイト: GitHub Pages

ブログ開設日2009-01-05
サーバ連続稼働時間
Reading...
Page View 合計
Reading...
今日
Reading...
昨日
Reading...

Python - ソート処理各種テスト!

[ プログラミング, 数学 ] [ Python ]

こんばんは。

今回は、各種ソート処理のアルゴリズムを Python3 で実装してみました。

0. 前提条件

  • LMDE 2 (Linux Mint Debian Edition 2; 64bit) での作業を想定。
  • Python 3.6.4 での作業を想定。
  • 当方は他のバージョンとの共存環境であり、 python3.6, pip3.6 で 3.6 系を使用するようにしている。(適宜、置き換えて考えること)

1. アルゴリズムについて

当ブログ過去記事を参照。

2. Python スクリプトの作成

  • 敢えてオブジェクト指向で作成している。
  • Shebang ストリング(1行目)では、フルパスでコマンド指定している。(当方の慣習
  • 数値計算ライブラリ NumPy は使用しない。(この程度の行列計算は List で充分)
  • 必要であれば、スクリプト内の定数を変更する。
sort_test.py
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#! /usr/local/bin/python3.6
"""
Test of sorting algorithms
"""
import copy
import random
import sys
import time
import traceback


class SortTest:
    N = 100    # Number of data
    M = 10000  # Maximum value ( < M )
    L = 10000  # Count of sort trying

    def __init__(self):
        self.base = [random.randrange(self.N) + 1 for _ in range(self.N)]
        print("#### Base list")
        self.__display_list(self.base)

    def exec(self):
        """ Execution of test """
        try:
            self.__sort_bubble()
            self.__sort_selection()
            self.__sort_insertion()
            self.__sort_quick()
            self.__sort_heap_up()
            self.__sort_heap_down()
            self.__sort_shell()
        except Exception as e:
            raise

    def __sort_bubble(self):
        """ Basic exchange method (Bubble sort) """
        print("1  : Bubble Sort      ", end="")
        try:
            t1 = time.time()
            for l in range(self.L):
                self.a = copy.deepcopy(self.base)
                for i in range(self.N - 1):
                    for j in reversed(range(i + 1, self.N)):
                        if self.a[j] < self.a[j - 1]:
                            self.a[j - 1], self.a[j] \
                                    = self.a[j], self.a[j - 1]
            t2 = time.time()
            self.__display(self.a, t2 - t1)
        except Exception as e:
            raise

    def __sort_selection(self):
        """ Basic exchange method (Selection sort) """
        print("2  : Selection Sort   ", end="")
        try:
            t1 = time.time()
            for l in range(self.L):
                self.a = copy.deepcopy(self.base)
                for i in range(self.N - 1):
                    min, s = self.a[i], i
                    for j in range(i + 1, self.N):
                        if self.a[j] < min:
                            min, s = self.a[j], j
                    self.a[i], self.a[s] = self.a[s], self.a[i]
            t2 = time.time()
            self.__display(self.a, t2 - t1)
        except Exception as e:
            raise

    def __sort_insertion(self):
        """ Insertion sort """
        print("3  : Insertion Sort   ", end="")
        try:
            t1 = time.time()
            for l in range(self.L):
                self.a = copy.deepcopy(self.base)
                for i in range(1, self.N):
                    for j in reversed(range(i)):
                        if self.a[j] > self.a[j + 1]:
                            self.a[j], self.a[j + 1] \
                                = self.a[j + 1], self.a[j]
                        else:
                            break
            t2 = time.time()
            self.__display(self.a, t2 - t1)
        except Exception as e:
            raise

    def __sort_quick(self):
        """ Improved exchange method (Quick sort) """
        print("4  : Quick Sort       ", end="")
        try:
            t1 = time.time()
            for l in range(self.L):
                self.a = copy.deepcopy(self.base)
                self.__quick(0, self.N - 1)
            t2 = time.time()
            self.__display(self.a, t2 - t1)
        except Exception as e:
            raise

    def __sort_heap_up(self):
        """ Improved exchange method (Heap sort with upward method) """
        print("5-1: Heap Sort(Up)    ", end="")
        try:
            t1 = time.time()
            self.h = [0 for _ in range(self.N + 1)]
            for l in range(self.L):
                self.__generate_heap_up()
                n, m = self.N, self.N
                while n > 1:
                    self.h[1], self.h[n] = self.h[n], self.h[1]
                    n -= 1
                    p = 1
                    s = 2 * p
                    while s <= n:
                        if s < n and self.h[s + 1] > self.h[s]:
                            s += 1
                        if self.h[p] >= self.h[s]:
                            break
                        self.h[p], self.h[s] = self.h[s], self.h[p]
                        p = s
                        s = 2 * p
            del(self.h[0])
            t2 = time.time()
            self.__display(self.h, t2 - t1)
        except Exception as e:
            raise

    def __sort_heap_down(self):
        """ Improved exchange method (Heap sort with downward method) """
        print("5-2: Heap Sort(Down)  ", end="")
        try:
            t1 = time.time()
            self.h = [0 for _ in range(self.N + 1)]
            for l in range(self.L):
                for i in range(1, self.N + 1):
                    self.h[i] = self.base[i - 1]
                self.__generate_heap_down()
                n, m = self.N, self.N  # データ個数, n の保存
                while n > 1:
                    self.h[1], self.h[n] = self.h[n], self.h[1]
                    n -= 1
                    p = 1
                    s = 2 * p
                    while s <= n:
                        if s < n and self.h[s + 1] > self.h[s]:
                            s += 1
                        if self.h[p] >= self.h[s]:
                            break
                        self.h[p], self.h[s] = self.h[s], self.h[p]
                        p = s
                        s = 2 * p
            del(self.h[0])
            t2 = time.time()
            self.__display(self.h, t2 - t1)
        except Exception as e:
            raise

    def __sort_shell(self):
        """ Improved insertion method (Shell sort) """
        print("6  : Shell Sort       ", end="")
        try:
            t1 = time.time()
            for l in range(self.L):
              self.a = copy.deepcopy(self.base)
              #gap = int(self.N / 2)
              gap = self.N // 2
              while gap > 0:
                  for k in range(gap):
                      i = k + gap
                      while i < self.N:
                          j = i - gap
                          while j >= k:
                              if self.a[j] > self.a[j + gap]:
                                  self.a[j], self.a[j + gap] \
                                      = self.a[j + gap], self.a[j]
                              else:
                                  break
                              j -= gap
                          i += gap
                  #gap = int(gap / 2)
                  gap = gap // 2
            t2 = time.time()
            self.__display(self.a, t2 - t1)
        except Exception as e:
            raise

    def __quick(self, left, right):
        """ Recursive function for quick sort

        :param int  left:  index of left-side list
        :param int right: index of right-side list
        """
        try:
            if left >= right:
                return
            s, i, j = self.a[left], left, right + 1
            while True:
                i += 1
                while i < self.N and self.a[i] < s:
                    i += 1
                j -= 1
                while j < self.N and self.a[j] > s:
                    j -= 1
                if i >= j:
                    break
                self.a[i], self.a[j] = self.a[j], self.a[i]
            self.a[left], self.a[j] = self.a[j], s
            self.__quick(left, j - 1)
            self.__quick(j + 1, right)
        except Exception as e:
            raise

    def __generate_heap_up(self):
        """ Function for heap(upward) generation """
        try:
            for i in range(1, self.N + 1):
                self.h[i] = self.base[i - 1]
                s = i
                #p = int(s / 2)
                p = s // 2
                while s >= 2 and self.h[p] < self.h[s]:
                    self.h[p], self.h[s] = self.h[s], self.h[p]
                    s = p
                    #p = int(s / 2)
                    p = s // 2
        except Exception as e:
            raise

    def __generate_heap_down(self):
        """ Function for heap(downward) generation """
        try:
            n = self.N
            for i in reversed(range(1, n // 2 + 1)):
                p = i
                s = 2 * p
                while s <= n:
                    if s < n and self.h[s + 1] > self.h[s]:
                        s += 1
                    if self.h[p] >= self.h[s]:
                        break
                    self.h[p], self.h[s] = self.h[s], self.h[p]
                    p = s
                    s = 2 * p
        except Exception as e:
            raise

    def __display_list(self, l):
        """ Display of list

        :param list l: target list for display
        """
        try:
            for i in range(self.N):
                print("{:5d} ".format(l[i]), end="")
                if (i + 1)  % 10 == 0 or i == self.N - 1:
                    print()
            print()
        except Exception as e:
            raise

    def __display(self, l, tt):
        """ Display of result

        :param list   l: target list for display
        :param float tt: elapsed time
        """
        try:
            # ソート結果を確認したければ、以下2行をコメント解除
            #print("\n#### Sorted list")
            #self.__display_list(l)
            print("Time: {:6.2f} sec.".format(tt))
        except Exception as e:
            raise


if __name__ == '__main__':
    try:
        obj = SortTest()
        obj.exec()
    except Exception as e:
        traceback.print_exc()
        sys.exit(1)

3. Python スクリプトの実行

まず、実行権限を付与。

1
$ chmod +x sort_test.py

そして、実行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ ./sort_test.py
#### Base list
   64     4    17    72     6     7    18    75    65    76
   40    46    57    74    34    95    25    67    20    90
   58    65    93    79    17    81    94    93    33    47
   20    87    28    53    16    57    88    85    63    88
    7     8     1    45     8    21    46    60    21    39
   75    25    93     2    82    44    48    61    12    25
   21    13    53     7    81     1    22     5    10    23
   49    46    20    41    51    21    42    33    57    11
   50    72    88    59    35    85    21    85    26    42
   66    82    65    89    78    13   100    93    86    18

1  : Bubble Sort      Time:  21.15 sec.
2  : Selection Sort   Time:   7.72 sec.
3  : Insertion Sort   Time:  18.46 sec.
4  : Quick Sort       Time:   3.85 sec.
5-1: Heap Sort(Up)    Time:   5.80 sec.
5-2: Heap Sort(Down)  Time:   6.06 sec.
6  : Shell Sort       Time:   5.90 sec.

クイック・ソートが最も速く、バブル・ソートが最も遅かった。
ヒープ・ソート(上方/下方)とシェル・ソートはほぼ同じだった。


以上

Comments