why doesnt this work? (quick help)

Discussion in 'Silicon (v)Alley' started by DoctorReefer, Oct 20, 2013.

  1. #1 DoctorReefer, Oct 20, 2013
    Last edited by a moderator: Oct 20, 2013
    Below is some java code that is supposed to sort an array of shapes based on their area(compareto method takes care of that). However, it never sorts the array correctly and i don't know why. here is the code:
     
    public static Shape[] sort(Shape[] array)
    {
    int indexOfMin =0;
     
    for(int j=0; j<array.length; j++)
    {
        for(int i =indexOfMin; i<array.length; i++)
        {
             if(array.compareTo(array[indexOfMin]) == -1)
             {
     
                 indexOfMin=i;
             }
        }
        swap(indexOfMin, j, array);
        indexOfMin=j+1;
    }
    return array;
    }
     
     
     
     
    The idea is that it goes through the list, starting at j, and finds the minimum value then swaps it with j. then j iterates and it does it again. What is wrong with this code can anyone help?

     
  2. #2 DoctorReefer, Oct 20, 2013
    Last edited by a moderator: Oct 20, 2013
    Okay so disregard the title. I fixed the sort. the sort you see above works fine (before i had the inner loop start at i=0) I do have another question though.
     
    what kind of sort is this? is it an insertion sort?
     
  3. I'll insert my penis into a vagina anyday hahaha.
     

Share This Page