Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Selection sort not working

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Selection sort not working

    I can not figure out what is wrong with my code I have looked over it many times and cant find anything wrong with it. When I run the program it doesn't sort the list by the Item Id.

    this is my tester class
    import java.util.*;

    public class Tester
    {
    public static void printInvetory(List<Item> a)
    {
    System.out.println("ItemID ItemName InStoreID Price");
    System.out.println("-------------------------------------------");
    for(int i = 0; i < a.size(); i++)
    {
    System.out.println(a.get(i));
    }
    }

    public static void sortID(List<Item> a)
    {
    int i;
    int k;
    int posmax;
    Item temp;
    for(i = a.size() - 1; i >= 0; i--)
    {
    posmax = 0;
    for(k = 0 ; k <= i ; k++)
    {
    if(a.get(k).getItemID() > a.get(posmax).getItemID())
    posmax = k;
    }
    temp = a.get(i);
    a.set(i, a.get(posmax));
    a.set(posmax, temp);
    }
    }

    public static void main(String[] args)
    {
    List<Item> myStore = new ArrayList<Item>();

    myStore.add(new Item(0004, 200, 10.5, "Air Filters"));
    myStore.add(new Item(0002, 60, 21.5, "Door Knobs"));
    myStore.add(new Item(0006, 90, 9.99, "Hammers "));
    myStore.add(new Item(0001, 80, 19.99, "Levels "));
    myStore.add(new Item(0005, 100, 59, "Ceiling fans"));
    myStore.add(new Item(0003, 55, 80, "Wrench Sets"));

    printInvetory(myStore);
    System.out.println();
    printInvetory(myStore);
    }
    }

    this is my constructor class
    public class Item
    {
    private int itemID;
    private int inStore;
    private double Price;
    private String itemName;

    public Item(int id, int is, double p, String in)
    {
    itemID = id;
    inStore = is;
    Price = p;
    itemName = in;
    }

    public String getName()
    {
    return itemName;
    }

    public int getItemID()
    {
    return itemID;
    }

    public int getInStore()
    {
    return inStore;
    }

    public double getPrice()
    {
    return Price;
    }

    public String toString()
    {
    return itemID + "\t" + itemName + "\t" + inStore + "\t" + "$ " + Price;
    }
    }

    PLZ HELP.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,972
    My Mood
    Hungover
    Thanks
    133
    Thanked 554 Times in 475 Posts

    Default Re: Selection sort not working

    When posting code, please use the highlight tags to preserve formatting.

    You're going to have to debug your code to figure out where its execution differs from your expectations. Recommended reading: http://www.javaprogrammingforums.com...t-println.html
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Selection sort not working

    Your sortID method is never called... main method should look like this (assuming you only want to print the list once)...
    public static void main(String[] args) {
    	List<Item> myStore = new ArrayList<Item>();
     
    	myStore.add(new Item(0004, 200, 10.5, "Air Filters"));
    	myStore.add(new Item(0002, 60, 21.5, "Door Knobs"));
    	myStore.add(new Item(0006, 90, 9.99, "Hammers "));
    	myStore.add(new Item(0001, 80, 19.99, "Levels "));
    	myStore.add(new Item(0005, 100, 59, "Ceiling fans"));
    	myStore.add(new Item(0003, 55, 80, "Wrench Sets"));
     
    	sortID(myStore);
    	printInvetory(myStore);
    	System.out.println();
        }


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  4. #4
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Selection sort not working

    I am gonna print it every time I sort it.

  5. #5
    Junior Member
    Join Date
    May 2012
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Selection sort not working

    Wow I just looked back a noticed I never called the method ...... I feel Kinda dumb but thanks for the help.

  6. #6
    Member
    Join Date
    Feb 2011
    Posts
    31
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Selection sort not working

    Quote Originally Posted by killer3p0 View Post
    Wow I just looked back a noticed I never called the method ...... I feel Kinda dumb but thanks for the help.
    We all have those kinds of days...


    All intelligent thoughts have already been thought;
    what is necessary is only to try to think them again.



  7. The Following User Says Thank You to WhiteSoup12 For This Useful Post:

    killer3p0 (May 16th, 2012)

Similar Threads

  1. Counting basic operations in this Selection Sort
    By Keiran in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 1st, 2012, 06:16 PM
  2. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 02:27 PM
  3. Selection sort code needs reverse
    By steel55677 in forum Algorithms & Recursion
    Replies: 7
    Last Post: September 27th, 2011, 06:40 AM
  4. bubble sort and selection sort on strings
    By Sir Saula in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2010, 09:44 AM
  5. Replies: 4
    Last Post: January 26th, 2009, 11:03 PM