从SQL Select语句中冒泡排序



所示的Select语句在获取后放入一个列表中,并应按从大到小的顺序排列。没有错误显示,但是,从排序的数字没有排序时显示?请帮忙:)

第一次收集时输出["('72,000,000',)('221,882,777',)('35,569,993',)"],当应该排序时也是如此。

tablesconnect.execute("SELECT Current_Views FROM Stray_Kids WHERE Order_of_CB = 1")
SKZCurrentViews = tablesconnect.fetchone()
print (SKZCurrentViews)
SKZCurrentViews = str(SKZCurrentViews)
tablesconnect.execute("SELECT Current_Views FROM TWICE WHERE Order_of_CB = 1")
TWICECurrentViews = tablesconnect.fetchone()
print (TWICECurrentViews)
TWICECurrentViews = str(TWICECurrentViews)
tablesconnect.execute("SELECT Current_Views FROM NCT_127 WHERE Order_of_CB = 1")
NCT127CurrentViews = tablesconnect.fetchone()
print (NCT127CurrentViews)
NCT127CurrentViews = str(NCT127CurrentViews)
Compare = [(SKZCurrentViews)+(TWICECurrentViews)+(NCT127CurrentViews)]
print (Compare)

n = len(比较)

for i in range(n-1):
for j in range (0, n-i-1):
if Compare[j] > Compare[j+1]:
Compare[j], Compare[j+1] = Compare[j+1], Compare[j]
print(Compare)

我认为你应该这样写你的冒泡排序算法来得到正确的输出:

for i in range(n): # Here was the error, you should iterate with range(n) not range(n - 1)
for j in range (0, n - i - 1):
if Compare[j] > Compare[j+1]:
Compare[j], Compare[j+1] = Compare[j+1], Compare[j]
print(Compare)

最新更新