Python Algorithm Pt.4: Selection sort

Che Kai LIANG
2 min readOct 24, 2021

Hello Sunday medium predators! Part 4 of our humble journey is OUT! And now…scenario narrator! Its your cue!

The castle loomed over two figures. Its shadow felt like a mass pressing down on the two uninvited guests. Even more so when they entered. Lightning flashed a dramatic affect, briefly lighting up the room, providing both a short respite to the constant darkness and a feeling this scene is somewhat exaggerated. Derp-kai opened the door to find four gates blocking his way to rescuing his fellow derps. The gates are at this order: 4, 2, 1, 3. Derpary tried to move the gates…but to no avail. Derp-kai, seeing he has no more bubbles and insertion spells, used a trick up his sleeve(get it?) He pulled a scroll out of his long wizard sleeve(HAHA!) and began chanting. Runes etched on the gates as they stirred to life. Gate 4 moved forward. 2 is blocking his way. Since/ gate 2 is smaller than gate 4, they switched places and 2 moved forward. 2 compared with 1 and resulted in 1 and 2 switching places and 1 comparing itself with 3. since 1 is smaller than 3. So the smallest one (1) is now the first one. Now, once the rest of the numbers compared itself, we can clearly compare this sort with the others. This sort is a more ineffective than others, but easier when used at larger numbers.

Here is an example:

def selection_sort (nLst):for i in range(len(nLst)-1):index = ifor j in range(i+1, len(nLst)):if nLst[index] > nLst[j]:index = jif i == index:passelse:nLst[i],nLst[index] = nLst[index],nLst[i]return nLstderps = ['Derpsss', 'Der', 'Derpo', 'D']print("The derps are:", derps)print("Using selection_sort ( ) from small to large")selection_sort(derps)print("Result = ",derps)

The result

Well, I am spent. I am tired, and I want to play some video games. Bye, and happy halloween! Definitely not a lazy ending lol.

--

--