带负数的Python排序列表
为了通过实践学习python,我尝试使用python实现并测试快速排序算法 实现本身并不困难,但是排序的结果有些令人费解: 当我对列表排序时 [35'、'-1'、'-2'、'-7'、'-8'、'-3'、'-4'、'-20'、'-6'、'-53'] 结果告诉我 ['-1','-2','-3','-4','-6','-7','-8',20',35',53'] 所以列表是按顺序排序的,而负整数是按相反顺序排序的 我怀疑这可能是因为我正在对从文件中读取的int列表进行排序,而int的类型实际上不是int,而是其他类型(可能是字符串)。我可以做些什么来解决这个问题 下面是快速排序实现的代码带负数的Python排序列表,python,python-2.7,sorting,quicksort,negative-number,Python,Python 2.7,Sorting,Quicksort,Negative Number,为了通过实践学习python,我尝试使用python实现并测试快速排序算法 实现本身并不困难,但是排序的结果有些令人费解: 当我对列表排序时 [35'、'-1'、'-2'、'-7'、'-8'、'-3'、'-4'、'-20'、'-6'、'-53'] 结果告诉我 ['-1','-2','-3','-4','-6','-7','-8',20',35',53'] 所以列表是按顺序排序的,而负整数是按相反顺序排序的 我怀疑这可能是因为我正在对从文件中读取的int列表进行排序,而int的类型实际上不是int
#quicksort -> the conquer part of the algorithm
def quicksort(input_list, start_index, end_index):
if start_index < end_index:
#partition the list of integers.
pivot = partition(input_list, start_index, end_index)
#recursive call on both sides of the pivot, that is excluding the pivot itself
quicksort(input_list, start_index, pivot-1)
quicksort(input_list, pivot+1, end_index)
return input_list
#divide part of the algorithm
def partition(input_list, start_index, end_index):
#declare variables required for sorting
pivot = input_list[start_index]
left = start_index + 1
right = end_index
sorted = False
while not sorted:
#break condition so that left index is crossed with right index
#or if the value of left crosses the pivot value
while left <= right and input_list[left] <= pivot:
#increment left index
left = left + 1
#break the loop when right and left indexes cross
#or if the right value crosses the pivot value
while right >= left and input_list[right] >= pivot:
right = right-1
if right < left:
sorted = True
else:
#swap places for left value and the right value cause they are not in order
temp = input_list[left]
input_list[left] = input_list[right]
input_list[right] = temp
#swap the value at start index with what's now at the right half. Then return right for the new pivot
temp = input_list[start_index]
input_list[start_index] = input_list[right]
input_list[right] = temp
return right
#快速排序->算法的征服部分
def快速排序(输入列表、开始索引、结束索引):
如果开始索引<结束索引:
#对整数列表进行分区。
pivot=分区(输入\列表、开始\索引、结束\索引)
#对枢轴两侧的递归调用,即不包括枢轴本身
快速排序(输入列表、开始索引、pivot-1)
快速排序(输入列表、轴+1、结束索引)
返回输入列表
#分割算法的一部分
def分区(输入列表、开始索引、结束索引):
#声明排序所需的变量
pivot=输入列表[开始索引]
左=开始索引+1
右=结束索引
排序=假
虽然未排序:
#使左索引与右索引交叉的中断条件
#或者如果left的值与pivot值交叉
左=枢轴时:
右=右-1
如果右<左:
排序=真
其他:
#交换左值和右值的位置,因为它们不按顺序排列
temp=输入列表[左]
输入列表[左]=输入列表[右]
输入列表[右]=临时
#将开始索引处的值与右半部分的值交换。然后向右返回新轴
temp=输入列表[开始索引]
输入列表[开始索引]=输入列表[右]
输入列表[右]=临时
返回权
感谢您的帮助。谢谢大家的时间和帮助。您正在对字符串而不是数字排序,因此它们是按字母顺序而不是数字顺序排序的。
int()
函数可以将字符串转换为数字。您的数字都是字符串。如果您的输入中只需要正整数或负整数,那么在进行比较时,请使用int()
将它们包装起来。您的代码运行正常,因为字符串按字典顺序排序(按第一个字符排序,如果第一个匹配,则按第二个字符排序,如果第二个匹配,则按第三个字符排序,等等)。如果要按数字排序,最简单的方法是调整列表
,使其实际上是int
值:
# Possibly read from file as list of string
strlist = ['35', '-1', '-2', '-7', '-8', '-3', '-4', '20', '-6', '53']
intlist = map(int, strlist) # list(map(int, strlist)) on Python 3
quicksort(intlist)
如果需要,您可以在以后转换回
str
。否则,您的替代方案是实现对键
函数的支持(该函数允许您对计算值进行排序),如列表。排序
/排序
,但这可能是您现在要处理的更复杂的问题。谢谢!我将int映射到列表中,一切正常。所以这不是代码中的错误,而是我正在排序的类型及其行为中的错误。谢谢你,暗影侠。我学到了很多!感谢您包括python2
和python3
实现+1美元。我认为这比对要比较的每个整数进行类型转换更简单、更干净。