Q. Write a program to sort a dictionary’s keys using bubble sort and produce the sorted keys as a list.


You can understand by Watching video :-



Answer :-

dic = eval (input("Enter a dictionary : "))
key = list(dic.keys())

for j in range (len(key)):
    for i in range ( len ( key ) - 1) :
        if  key [ i ] > key [ i + 1 ] :
            key [ i ] , key [ i + 1 ] =  key [ i + 1 ] , key [ i ]

print(key)

Output :-

Enter a dictionary : {9:"Python",2:"C++",7:"Java",3:"CSS"}
[2, 3, 7, 9]

>>> 

Enter a dictionary : {5:"Path",3:"Walla",4:"Portal",7:"Express"}
[3, 4, 5, 7]

>>> 



2 Comments

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post