-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathshellsort.py
More file actions
75 lines (54 loc) · 1.87 KB
/
shellsort.py
File metadata and controls
75 lines (54 loc) · 1.87 KB
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
"""
Shell sort
Shell Sort involves sorting elements which are away from ech other.
We sort a large sublist of a given list and go on reducing the size of the list until all elements are sorted.
The below program finds the gap by equating it to half of the length of the list size and then starts sorting all elements in it.
Then we keep resetting the gap until the entire list is sorted.
"""
"""
procedure shellSort()
A : array of items
/* calculate interval*/
while interval < A.length /3 do:
interval = interval * 3 + 1
end while
while interval > 0 do:
for outer = interval; outer < A.length; outer ++ do:
/* select value to be inserted */
valueToInsert = A[outer]
inner = outer;
/shift element towards right/
while inner > interval -1 && A[inner - interval] >= valueToInsert do:
A[inner] = A[inner - interval]
inner = inner - interval
end while
/* insert the number at hole position */
A[inner] = valueToInsert
end for
/* calculate interval*/
interval = (interval -1) /3;
end while
end procedure
"""
#Algorithm shellsort
def shellSort(input_list):
gap = len(input_list) // 2
while gap > 0:
for i in range(gap, len(input_list)):
temp = input_list[i]
j = i
# Sort the sub list for this gap
while j >= gap and input_list[j - gap] > temp:
input_list[j] = input_list[j - gap]
j = j-gap
input_list[j] = temp
# Reduce the gap for the next element
gap = gap//2
unsorted_list = []
num= int(input("Enter number of elements"))
print(f'Enter {num} elements')
for i in range(num):
data = int(input(f'{i+1}. '))
unsorted_list.append(data)
print(f'Unsorted list is{unsorted_list}')
print(f'Sorted list is {shellSort(unsorted_list)}')