• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this the correct way of understanding Selection sort?  RSS feed

 
Justin Robbins
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello there!

Am having a little trouble understanding selection sort want to make sure I am following correctly.

Found the code, just trying to conceptualize it, so imagining we pass in an array such as: 9,5,2,7,1



And then we carry on to iteration 2, then 3 then 4 each time doing a inner for-loop to find the proceeding smallest value ahead of its index.

A major thing I don't get is how is this updating a list? like how is it being saved to eventually return a sorted list?

please tell me if I am misunderstanding anything
Thank you
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to understand selection sort, I would say that looking at some Java code isn't one of the best ways to do that. Not unless you understand Java really well, that is. Otherwise understanding the Java code is just going to get in the way of understanding the algorithm. In my opinion you're better off to read an English-language description (or anyway a description in your native language).
 
Ivan Jozsef Balazs
Rancher
Posts: 999
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:In my opinion you're better off to read an English-language description (or anyway a description in your native language).


Or print out the relevant variables in say each iteration to see what is going on.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Besides, the code you posted is incorrect. Line 14 should check minValue against list[i], not list[j].
 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wikipedia has a very good article on Selection Sort.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!