0

After I have read about selection sort and tried to write the code for it in java (Why doesn't my selection sort algorithm do what it's supposed to (java)?), and then looked at our script, I got confused. The pseudo-code for selection sort seems wrong or rather incomplete for me. It looks more similar to bubble sort than selection sort pseudo-code.

I mean, where is the important part of the code where you look for the smallest value of the array and put it to the beginning and repeat that process?

I hope this is the correct section to ask, and if not, please tell me where I can ask it (I would delete that question immediately). But it's very important for me to understand what has been written there and your opinion about it.

(I'm especially very curious and afraid how I shall write pseudo code for selection sort in an exam, if asked.)

enter image description here

cnmesr
  • 345
  • 3
  • 11
  • This is not similar to bubble sort in any way. Bubble sort swaps adjacent elements. "where is the important part of the code where you look for the smallest value" it's right here before your eyes, it is just cleverly disguised. Ask yourself where's the smallest value of the unsorted part of the array at the end of the inner loop. – n. m. could be an AI Jul 08 '17 at 22:28
  • @n.m. Doesn't this pseudo code swap adjacent elements as well? I see that S[i] is compared with S[j]. If S[i] is greater, then both will be swapped. Both are neigbors because i=i and j = i+1 – cnmesr Jul 08 '17 at 22:46
  • "Doesn't this pseudo code swap adjacent elements as well?" Absolutely not. "i=i and j = i+1" No, j = i+1 is not generally true. – n. m. could be an AI Jul 08 '17 at 22:54
  • @n.m. But line 4 says that we start at j which equals i+1, and we repeat that inner for loop till we reach n. Or what exactly is that line saying? This is pure confusion :s – cnmesr Jul 08 '17 at 22:58
  • 1
    We *start at* j which equals i+1. That doesn't mean j *always* equals i+1. I recommend you running the code manually for a small array, say n=5, and see what's going on. – n. m. could be an AI Jul 08 '17 at 23:01
  • 1
    It "selects" the minimum by doing a metric ton of exchanges, while in a typical explanation of selection sort you find the minimum and *then* exchange it. – harold Jul 08 '17 at 23:29
  • Which college is that? – Pushan Gupta Jul 09 '17 at 12:54
  • @VidorVistrom Humboldt-Universität zu Berlin – cnmesr Jul 09 '17 at 13:17

0 Answers0