所需排序文件txt:
7415120123 name 22.71
7415120134 name 22.51
7415120109 name 20.95
7415120115 name 20.17
7415120131 name 18.04
6411120127 name 17.87
7415120117 name 17.25
7415120106 name 16.09
7415120102 name 16.06
7415120111 name 16.01
7415120126 name 15.47
7415120124 name 15.34
7415120128 name 14.66
7415120113 name 13.85
7415120118 name 13.77
7415120105 name 13.64
7415120140 name 13.62
7415120101 name 13.57
7415120104 name 13.23
7415120110 name 12.89
7415120127 name 12.47
7415120121 name 12.37
7415120125 name 12.26
7415120116 name 11.85
7415120130 name 11.57
7415120139 name 11.47
7415120136 name 11.29
7415120138 name 10.72
7415120103 name 10.25
7415120133 name 9.51
7415120137 name 9.46
7415120129 name 9.43
7415120108 name 7.97
7415120112 name 6.6
7415120120 name 6.46
7415120122 name 6.22
7415120107 name 4.95
7415120119 name 4.53
7415120132 name 2.28
def bubbleSort(data):
for i in range(len(data)):
for j in range(0, len(data) - i - 1):
if data[j] > data[j + 1]:
data[j], data[j + 1] = data[j + 1], data[j]
def selectionSort():
for i in range(len(data)-1,0,-1): #9-0
positionOfMax=0 #记录当前元素前面的最大值的下标
for j in range(1,i+1): #1-9
if int(data[j][0])>int(data[positionOfMax][0]):
positionOfMax=j
data[i][0],data[positionOfMax][0]=data[positionOfMax][0],data[i][0]
data[i][1], data[positionOfMax][1] = data[positionOfMax][1], data[i][1]
data[i][2], data[positionOfMax][2] = data[positionOfMax][2], data[i][2]
print(data)
def insertionSort():
for i in range(1, len(data)): # 1-9
currentvalue = data[i]
position = i
while position > 0 and data[position - 1] > currentvalue:
data[position] = data[position - 1]
position = position - 1
data[position] = currentvalue
print(i, data)
if __name__=="__main__": # 分别调用三种排序,学号排序,顺序学号查找
f = open('files/scores.txt', 'r', encoding='UTF-8-sig')
data = f.readlines()
data = [item.strip().split() for item in data] # 生成式
arr = []
for i in range(len(data)):
arr.append(data[i][0])
bubbleSort(data)
print("冒泡排序:",data)
# selectionSort()
# print("选择排序:",data)
# insertionSort()
# print("插入排序:",data)