• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Is this the correct way of understanding Selection sort?

 
Ranch Hand
Posts: 121
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 28401
100
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Rancher
Posts: 1044
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Besides, the code you posted is incorrect. Line 14 should check minValue against list[i], not list[j].
 
Sheriff
Posts: 7126
185
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Wikipedia has a very good article on Selection Sort.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic