• 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:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Modifying selection sort algorithm

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone, I was trying to improve my method and I tried everything to fix what was the problem with my method but I can't figure out what is wrong with it. I'm modifying selection sort by not just finding the minIndex and also the maxIndex than swap the minIndex to the left of the array index and the maxIndex with the right side and so on. Could someone look at my method and give me any feedback please? thanks



P.S: my swap method is fine you don't have to worry about it
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jack Sam wrote:Could someone look at my method and give me any feedback please? thanks


Right, I assume that what you're trying to do is to get your selection sort to determine the minimum and maximum remaining values on each pass, correct?

First, your outer loop looks wrong because it will theoretically execute data.length times, when in fact you only need to execute data.length/2 times; which is why you have to include a break statement. Have a think how you might write it so that you don't need that break.

Second: The business of finding the minimum and maximum values is independent of what you want to do with them (ie, swap), so my advice would be to remove that function to a separate method, for example:The T stuff is just to make it generic, so it can be used with with any Comparable type. If you don't follow it at the moment, just replace all the T's with String and remove the '<T extends Comparable<? super T>>' altogether.
You'll also notice that I made the 'end' index exclusive. It's not required, but when dealing with ranges you'll find that it makes a lot of things easier. If you look at foundation class methods that deal with ranges (eg, String.subString()) they almost always follow this pattern.

Do you see how a method like that might simplify your sorting loop?

HIH

Winston

PS: Welcome to JavaRanch.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just one other thing:

There are two conditions that you need to watch out for:
1. An array with an odd number of elements.
2. The situation where ALL remaining elements have the same value.

Winston
 
We must storm this mad man's lab and destroy his villanous bomb! Are you with me tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic