-1

I have a current algorithm for sorting my dictionary but it wont work if you compare 4 or more elements. What am I doing wrong?

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])
Harvey
  • 5,703
  • 1
  • 32
  • 41
  • Any particular reason you're implementing your own sorting algorithm instead of using the built in `sort`? – Kevin May 18 '17 at 16:42
  • 1
    Possible duplicate of [Bubble Sort Homework](http://stackoverflow.com/questions/895371/bubble-sort-homework) – Chiheb Nexus May 18 '17 at 16:46

2 Answers2

0

These two lines are wrong. You're using the indexes of j in range() rather than the profile keys stored at those indexes. It stops working because you're always comparing the same elements regardless of swapping.

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

They should be this:

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

Here's the result of running the first pass. The top v1/v2 are the mistaken values while the bottom ones are the correct values.

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' 

Also, this is how you swap in Python:

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

Here's a better algorithm:

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]

And here's how to do it using standard methods:

profiles = list(sorted(profiles, key=lambda x: database[int(x)]))
Harvey
  • 5,703
  • 1
  • 32
  • 41
  • Hi! What can I do to also change the order in my of data the way it was sorted in my dictionay? Because my previous code doesn't change the arrangement inside the database={} after the sorting. – Ninna Layug May 18 '17 at 17:41
0

I have made a few changes to your code 1)

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

instead of

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

Because j will have the index of profiles but what we need is the actual value in profiles which is the key to the dictionary.

Then you were using bubble j and j+1 and was only considering values ranging from 0 to len(profiles)-1. This -1 makes you miss your last value . So i have considered values ranging from 1 to len(profiles) and used bubble j and j-1

Here is the complete code

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])

Hope this works for you

The output is as follows

['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
George
  • 109
  • 8
  • change if v1>v2: to if v1 – George May 18 '17 at 17:18
  • If i want to permanently change the order of my data in my dictionary according to how it was sorted how do i do it? For example the order of how it was sorted is the way it would also be written on the text file if i save it – Ninna Layug May 18 '17 at 17:21
  • Unless you use an ordered dictionary, python dictionaries don't have a specific order of saving it's data. In othe words, Lists are ordered and Dictionaries are unordered. But you can still write your sorted answer into a text file without changing the dictionary. This is unrelated to the question you posted. So i suggest you start a new post on that. And upvote whatever solution you found here to be helpful for the one you posted. – George May 18 '17 at 17:27
  • No Problem. If I was able to answer your question, do accept my answer.To mark an answer as accepted, click on the check mark beside the answer to toggle it from greyed out to filled in. You may change which answer is accepted, or simply un-accept the answer, at any time. – George May 18 '17 at 17:40
  • Last question! This code doesn't quite work if I sort the birthday part which is index 7 in my data, how do i fix it? :( – Ninna Layug May 18 '17 at 18:15
  • Again another topic. But here it goes. import time and use v1=time.strptime(database[int(profiles[j])][7],"%m/%d/%Y") v2=time.strptime(database[int(profiles[j-1])][7],"%m/%d/%Y") And you can see the sorted data in your print using this line print(database[int(x)][0],",",database[int(x)][1],",",database[int(x)][7]) – George May 18 '17 at 18:26
  • Oops, sorry huhu we're not allowed to import time, we're just asked to compare what was inputed hehe – Ninna Layug May 18 '17 at 18:29
  • Also, here's the link to my new question, I hope you can help me again!! TYSM GEORGE THIS MEANS SO MUCH TO ME <3 http://stackoverflow.com/questions/44055160/how-to-update-the-order-of-data-in-the-dictionary-i-sorted-by-bubble-sort – Ninna Layug May 18 '17 at 18:34