+ 6

How to implement selection sort in list where list elements are strings?

a=['ram','ham','sal','david'] x=len(a) for i in range(0,x): small=i for j in range(i+1,x): if a[small]>a[j]: small=a[j] a[small],a[i]=a[i],a[small] print(a) This is my code but it doesn't work

19th Dec 2018, 1:47 PM
Mara
4 odpowiedzi
+ 6
You almost got it! Try changing lines 6-7 for this: if a[small]>a[j]: small = j
19th Dec 2018, 2:08 PM
Diego
Diego - avatar
+ 5
You’re welcome. Happy coding!
19th Dec 2018, 2:25 PM
Diego
Diego - avatar
+ 4
Diego Acero thanks for your suggestions .it worked!
19th Dec 2018, 2:25 PM
Mara
0
I havent tried strings yet, though I understand why how they are ordered, spoilers
19th Dec 2018, 8:27 PM
Genghis
Genghis - avatar