如何使用冒泡排序

时间:2017-05-18 16:37:22

标签: python sorting dictionary

我有一个当前的算法来排序我的字典,但是如果你比较4个或更多元素它就不会工作。我做错了什么?

database={
    0:['Ninna','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999'],
    1:['Yela','Gregorio','201506070','09984548540','UP Diliman','yelagregorio@gmail.com','19','04/18/1999'],
    2:['Denise','Gregorio','201506070','09984548540','UP Diliman','yelagregorio@gmail.com','19','04/18/1999'],
    3:['Alia','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999'],
    4:['Keeno','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999']
}
profiles=['0','1','2','3','4']

for x in range(len(profiles)):
    print(profiles)
    for j in range(len(profiles)-1-x):
        v1=database[j][0]
        v2=database[j+1][0]

        if v1>v2:
            sorted=False
            temp=profiles[j+1]
            profiles[j+1]=profiles[j]
            profiles[j]=temp
for x in profiles:
    print(database[int(x)][0],",",database[int(x)][1])

2 个答案:

答案 0 :(得分:0)

这两行都错了。您在j中使用range()的索引而不是在这些索引中存储的配置文件密钥。它会停止工作,因为无论交换如何,您都总是在比较相同的元素。

v1=database[j][0]
v2=database[j+1][0]

他们应该是这样的:

v1 = database[int(profiles[j])][0]
v2 = database[int(profiles[j+1])][0]

这是运行第一遍的结果。顶部v1 / v2是错误值,而底部值是正确值。

x = 0                                                                    
print(['0', '1', '2', '3', '4'])                                         
j = 0        | j = 1             | j = 2             | j = 3             
v1 = 'Ninna' | v1 = 'Yela'       | v1 = 'Denise'     | v1 = 'Alia'       
v2 = 'Yela'  | v2 = 'Denise'     | v2 = 'Alia'       | v2 = 'Keeno'      
             |                   |                   |                   
v1 = 'Ninna' | v1 = 'Yela'       | v1 = 'Yela'       | v1 = 'Yela'       
v2 = 'Yela'  | v2 = 'Denise'     | v2 = 'Alia'       | v2 = 'Keeno'      
             |                   |                   |                   
             |                   |                   |                   
             | sorted = False    | sorted = False    | sorted = False    
             | temp = '2'        | temp = '3'        | temp = '4'        
             | profiles[2] = '1' | profiles[3] = '1' | profiles[4] = '1' 
             | profiles[1] = '2' | profiles[2] = '3' | profiles[3] = '4' 

此外,这是你在Python中交换的方式:

profiles[j], profiles[j+1] = profiles[j+1], profiles[j]

这是一个更好的算法:

swapped = False
while not swapped:
    swapped = True
    print(profiles)
    for j in range(len(profiles) - 1):
        v1 = database[int(profiles[j])][0]
        v2 = database[int(profiles[j+1])][0]
        if v1 > v2:
            swapped = False
            profiles[j], profiles[j+1] = profiles[j+1], profiles[j]

以下是使用标准方法的方法:

profiles = list(sorted(profiles, key=lambda x: database[int(x)]))

答案 1 :(得分:0)

我对您的代码进行了一些更改 1)

v1=database[int(profiles[j])][0]
v2=database[int(profiles[j-1])][0]

而不是

v1=database[j][0]
v2=database[j+1][0]

因为j将具有配置文件的索引,但我们需要的是配置文件中的实际值,这是字典的关键。

然后你使用bubble j和j + 1,并且只考虑从0到len(profiles)-1的值。这个-1会让你错过最后一个值。所以我考虑了从1到len(配置文件)的值,并使用了气泡j和j-1

这是完整的代码

database={
    0:['Ninna','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999'],
    1:['Yela','Gregorio','201506070','09984548540','UP Diliman','yelagregorio@gmail.com','19','04/18/1999'],
    2:['Denise','Gregorio','201506070','09984548540','UP Diliman','yelagregorio@gmail.com','19','04/18/1999'],
    3:['Alia','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999'],
    4:['Keeno','Layug','201504584','09954895032','Quezon City','ninnalayug@gmail.com','18','02/21/1999']
}
profiles=['0','1','2','3','4']

for x in range(len(profiles)):
    print(profiles)
    for j in range(1,len(profiles)-x):
        v1=database[int(profiles[j])][0]
        v2=database[int(profiles[j-1])][0]

        if v1>v2:
            sorted=False
            temp=profiles[j-1]
            profiles[j-1]=profiles[j]
            profiles[j]=temp
for x in profiles:
    print(database[int(x)][0],",",database[int(x)][1])

希望这适合你

输出如下

['0', '1', '2', '3', '4']
['1', '0', '2', '4', '3']
['1', '0', '4', '2', '3']
['1', '0', '4', '2', '3']
['1', '0', '4', '2', '3']
Yela , Gregorio
Ninna , Layug
Keeno , Layug
Denise , Gregorio
Alia , Layug