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

Thread: selectionsort

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Worried
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy selectionsort

       public class selectionSort{
     
          public static void main(String[]args){
     
             double[] array = {1, 9, 4.5, 6.6, 5.7,};
            	selectionSort(array);
     
          }
     
          public static void selectionSort(double[]array){
     
             for (int i =0; i<array.length-1; i++){
     
                double min = array[i];
     
                int minIndex = i;
     
                for (int j = i+1;  i<array.length; i++){
                   if ( min> array[i])
     
                      array[i]= min;
                }
     
                if ( minIndex!=i){
                   array[minIndex]= array[i];
                   array[i]= min;
     
                }
             }
          }
       }






    ok SO I am still trying to learn arrays, the thing is that I keep getting these error! I do not know WHAT i AM DOING WRONG!


    THE ERROR IS THE FOLLOWING!

    ----jGRASP exec: java selectionSort

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
    at selectionSort.selectionSort(selectionSort.java:25)
    at selectionSort.main(selectionSort.java:6)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.

    can somenoe explain it to me?
    thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: selectionsort

    Valid indicies for an array are 0 to length - 1. For an array that has a length of 5 the last index is 4 which makes 5 out of bounds.
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 3
    Last Post: October 22nd, 2011, 01:53 AM
  2. sorting name using Selectionsort
    By asdfg in forum What's Wrong With My Code?
    Replies: 7
    Last Post: May 20th, 2010, 09:44 AM