For this assignment create your own Shellsort Your output sh
For this assignment, create your own Shellsort. Your output should be similar to:
> more test.txt
S O R T E X A M P L E
> java ShellSort < test.txt
A
E
E
L
M
O
P
R
S
T
X
Solution
Code:
#function for performing shell sort
def shell_sort(ls):
incr = len(ls) // 2
while incr:
for i, el in enumerate(ls):
while i >= incr and ls[i - incr] > el:
ls[i] = ls[i - incr]
i -= incr
ls[i] = el
incr = 1 if incr == 2 else int(incr * 5.0 / 11)
return ls
#reading data from txt file and applying shell sort on the data
a = open(\"test.txt\",\"r\")
b = a.readline()
lis = b.split(\" \")
sorted_list = shell_sort(lis) #calling shell sort function
#printing the sorted list
for i in range(len(sorted_list)):
print(sorted_list[i])
Output:
