# some complex sorting algorithm
def shell_sort(sort_list):
''' (list) -> None
Sort the sort_list.
'''
increment = len(sort_list)
iter_len = len(sort_list)
while increment > 1:
increment = increment // 3 + 1
for i in range(increment, iter_len):
if sort_list[i] < sort_list[i - increment]:
sentry = sort_list[i]
j = i - increment
while j > -1 and sort_list[j] > sentry:
sort_list[j + increment] = sort_list[j]
j -= increment
sort_list[j + increment] = sentry
def heap_adjust(sort_list, s, m):
''' (list, int, int) -> none
s is the last nonterminal node. m is the last node. This is going to build
a max heap.
'''
temp = sort_list[s]
# j is the index of s's left child
j = 2 * s + 1
while (j <= m):
# find the biggest one between her children
if j < m and sort_list[j] < sort_list[j + 1]:
j += 1
if temp >= sort_list[j]:
break
sort_list[s] = sort_list[j]
s = j
j = 2 * j + 1
sort_list[s] = temp
def heap_sort(sort_list):
iter_len = len(sort_list)
# make sort_list a max heap
for i in range(iter_len // 2 - 1, -1, -1):
heap_adjust(sort_list, i, iter_len - 1)
for i in range(iter_len - 1, 0, -1):
sort_list[0], sort_list[i] = sort_list[i], sort_list[0]
heap_adjust(sort_list, 0, i - 1)
def partition(sort_list, low, high):
''' (list, int, int) -> int
Sort the sort_list into two parts and return the pivot index of sort_list
'''
# find the midian of three
## m = low + (high - low) // 2
## if sort_list[low] > sort_list[high]:
## sort_list[low], sort_list[high] = sort_list[high], sort_list[low]
## if sort_list[m] > sort_list[high]:
## sort_list[m], sort_list[high] = sort_list[high], sort_list[m]
## if sort_list[m] > sort_list[low]:
## sort_list[low], sort_list[m] = sort_list[m], sort_list[low]
pivot = sort_list[low]
while low < high:
while low < high and sort_list[high] >= pivot:
high -= 1
sort_list[low] = sort_list[high]
while low < high and sort_list[low] <= pivot:
low += 1
sort_list[high] = sort_list[low]
sort_list[low] = pivot
return low
def q_sort(sort_list, low, high):
''' (list, int, int) -> None
'''
if low < high:
pivot = partition(sort_list, low, high)
q_sort(sort_list, low, pivot - 1)
q_sort(sort_list, low + 1, high)
def quick_sort(sort_list):
q_sort(sort_list, 0, len(sort_list) - 1)
# this short code snippets excerpted from python cookbook 2nd edithion, but it doesn't work well, I am gonna make it work.
##def quick_sort_2(sort_list):
## if len(sort_list) <= 1:
## return sort_list
## return quick_sort_2([lt for lt in sort_list[1:] if lt < sort_list[0]]) + sort_list[0:1] + \
## quick_sort_2([ge for ge in sort_list[1:] if ge >= sort_list[0]])
# constants for merging sort
MAXSIZE = 1000
def m_sort(sr, tr1, s, t):
''' (list, list, int, int) -> None
The main part of the merge sort
'''
tr2 = []
for i in range(MAXSIZE):
tr2.append(0)
if s == t:
tr1[s] = sr[s]
else:
m = (s + t) // 2
m_sort(sr, tr2, s, m)
m_sort(sr, tr2, m + 1, t)
merge(tr2, tr1, s, m, t)
def merge(sr, tr, i, m, n):
''' (list, list, int, int) -> None
'''
j = m + 1
k = i
while i <= m and j <= n:
if sr[i] < sr[j]:
tr[k] = sr[i]
i += 1
else:
tr[k] = sr[j]
j += 1
k += 1
if i <= m:
for l in range(0, m - i + 1):
tr[k + l] = sr[i + l]
if j <= n:
for l in range(0, n - j + 1):
tr[k + l] = sr[j + l]
def merge_sort(sort_list):
m_sort(sort_list, sort_list, 0, len(sort_list) - 1)
# easy test
##lis = [50, 10, 90, 30, 70, 40, 80, 60, 20]
##merge_sort(lis)
##print(lis)
# read 'random_num.txt' from disk
import tkinter.filedialog
sort_filename = tkinter.filedialog.askopenfilename()
sort_file = open(sort_filename, 'r')
contents = sort_file.read()
sort_file.close()
sort_list = contents.split(' ')
for i in range(len(sort_list) - 1):
sort_list[i] = int(sort_list[i])
sort_list.pop()
# using the sorting algorithm
shell_sort(sort_list)
# write sorted file to disk.
to_filename = tkinter.filedialog.asksaveasfilename()
sorted_file = open(to_filename, 'w')
for num in sort_list:
sorted_file.write(str(num) + ' ')
sorted_file.close()
转载于:https://my.oschina.net/seandor/blog/94158