File tree 2 files changed +38
-0
lines changed
2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 9
9
from pigeonholeSort import pigeonhole_sort
10
10
from quickSort import quick_sort
11
11
from selectionSort import selection_sort
12
+ from shellSort import shell_sort
12
13
13
14
from random import randint
14
15
33
34
temp = array [:]
34
35
insertion_sort (temp )
35
36
temp = array [:]
37
+ shell_sort (temp )
38
+ temp = array [:]
36
39
lsd_radix_sort (temp )
37
40
temp = array [:]
38
41
pigeonhole_sort (temp )
Original file line number Diff line number Diff line change
1
+ from random import random
2
+
3
+
4
+ def shell_sort (arr ):
5
+ comparisons = 0
6
+ array_accesses = 0
7
+ additional_space = 0
8
+ n = len (arr )
9
+ gap = n // 2
10
+ while gap > 0 :
11
+ for i in range (gap , n ):
12
+ array_accesses += 1
13
+ temp = arr [i ]
14
+ j = i
15
+ while j >= gap and arr [j - gap ] > temp :
16
+ array_accesses += 2
17
+ comparisons += 2
18
+ arr [j ] = arr [j - gap ]
19
+ j -= gap
20
+ array_accesses += 1
21
+ arr [j ] = temp
22
+ gap //= 2
23
+
24
+ print ("Shell sort:" )
25
+ print ("No. comparisons: " + str (comparisons ) +
26
+ ", no. array accesses: " +
27
+ str (array_accesses ) + ", no. additional space required: "
28
+ + str (additional_space ))
29
+
30
+
31
+ if __name__ == "__main__" :
32
+ array = []
33
+ for i in range (20 ):
34
+ array .append (random ())
35
+ shell_sort (array )
You can’t perform that action at this time.
0 commit comments