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 3 of 3

Thread: Java selection sort

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Java selection sort

    I'm currently doing selection sort and this is my method to sort a array of string into alphabet order. I interpret the code line by line into words and test it by hand it seem to make sense but when I run it, the array won't be in order. Can anyone help me find where I went wrong? Thanks
        public static void sortTitles(Movie3[]list )
        {   
                 //Movie3 [] dest = new Movie3[list.length];
                 Movie3 temp;
                 int max ;
                for(int x = list.length-1 ; x > 0 ;x--)
                    {
                    String next = list[x].getTitle();
                    max = x;
                    for(int i = 0 ; i < x ;i++)
                    {
     
                        if((next.compareTo(list[i].getTitle())) > 0 )
                        {
                            max =  i ;
                        }
     
                    }
                    temp = list[x];
                    list[x] = list[max];
                    list[max] = temp;
                }
     
        }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java selection sort

    What will be the value in max at the end of the inner loop?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: Java selection sort

    Thanks for the hints on the value of max.
    Here is my new code and it working, but I don't understand why the inner loop need to have an index of max which = i for it to work.
        public static void sortTitles(Movie3[]list )
        {   
     
             Movie3 temp;
             int max ;
            for(int x = list.length-1 ; x > 0 ;x--)
            { 
             max = 0 ;
             for(int i = 1 ; i <= x ;i++)
             {
                 String next = list[max].getTitle();
                if((next.compareTo(list[i].getTitle())) > 0 )
                {
                 max =  i ;
                }
                System.out.println(max);
                    }
             temp = list[x];
             list[x] = list[max];
             list[max] = temp;
             }
         }

Similar Threads

  1. optimized selection sort
    By IHeartProgramming in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 9th, 2012, 08:02 PM
  2. [SOLVED] Selection sort not working
    By killer3p0 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 7th, 2012, 12:24 PM
  3. Counting basic operations in this Selection Sort
    By Keiran in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 1st, 2012, 07:16 PM
  4. Selection sort code needs reverse
    By steel55677 in forum Algorithms & Recursion
    Replies: 7
    Last Post: September 27th, 2011, 06:40 AM
  5. 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