Commit 7542ca53 by bonnieyan

Update sort_utils.py

parent 1d24455b
class SortUtils:
def sort(self, f, s):
if f == "bubble":
for i in range(len(s)-1 ):
for j in range(len(s)-i-1):
for i in range(len(s) - 1):
for j in range(len(s) - i -1):
if s[j] > s[j+1]:
s[j], s[j+1] = s[j+1], s[j]
print(s)
print(s)
elif f == "quick":
start_index = 0
end_index = len(s)-1
if start_index < end_index: # 如果角标左侧小于右侧则开始排序,否则退出
basic, i, j = s[start_index], start_index, end_index
print(s)
#print(s)
if f == "quick":
SortUtils.__quick_sort(s, 0, len(s)-1)
while i < j: # 保证左侧的index一定比右侧的小
def __quick_sort(sort_list, start_index, end_index):
# print(sort_list)
if start_index < end_index: # 如果角标左侧小于右侧则开始排序,否则退出
basic, i, j = sort_list[start_index], start_index, end_index
while i < j and basic <= s[j]: # 基准值比j(右侧)小,那么该值不做任何运算
j -= 1 # 角标左移
while i < j and basic >= s[i]: # 基准值比i(左侧)大,那么该值不做任何运算
i += 1 # 角标右移
while i < j: # 保证左侧的index一定比右侧的小
s[i], s[j] = s[j], s[i]
print("i=" + str(i) + "&&&" + "j=" + str(j) + "$$$" + "sort_list=" + str(s))
while i < j and basic <= sort_list[j]: # 基准值比j(右侧)小,那么该值不做任何运算
j -= 1 # 角标左移
while i < j and basic >= sort_list[i]: # 基准值比i(左侧)大,那么该值不做任何运算
i += 1 # 角标右移
s[i], s[start_index] = s[start_index], s[i]
print(str(s))
sort_list[i], sort_list[j] = sort_list[j], sort_list[i]
print("i=" + str(i) + "&&&" + "j=" + str(j) + "$$$" + "sort_list=" + str(sort_list))
#sort(s, start_index, i-1)
# SortUtils.sort(self, f, s)
# sort(s, i + 1, end_index)
sort_list[i], sort_list[start_index] = sort_list[start_index], sort_list[i]
SortUtils.__quick_sort(sort_list, start_index, i - 1)
SortUtils.__quick_sort(sort_list, i + 1, end_index)
p = SortUtils()
#p.sort("bubble", [5, 15, 2, 5, 3, 18, 6])
p.sort("quick", [5, 15, 2, 5, 3, 18, 6])
p.sort("bubble", [10, 5, 2, 16, 4, 9, 13, 8])
p.sort("quick", [10, 5, 2, 16, 4, 9, 13, 8])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment