Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Sorting

Options
  • 22-04-2007 11:47pm
    #1
    Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭


    Hey guys,

    Iv done a few small time java projects over the last while (Football League and Top 20 Chart) and have tried out ArrayLists and LLists. I came here once or twice when troubleshooting, and so am back again for more help :)

    Anyway, I want to try sort some of my projects. A friend told me InsertionSort is the best method to use for what im looking at. I took a look at the Wikipedia article and algorithim implementation. So here is the Java code they showed;
    public static void insertionSort(int data[])
    {
       for (int i = 1; i < data.length; i++) 
       {
          int temp = data[i];           
          int j = i - 1;
            
          while (j >= 0 && temp < data[j])
          {
             data[j + 1] = data[j];
             data[j] = temp;
              
             j--;
          }
       }
    }
    

    Forgive me, but its making little sense to me. Can anybody either point me in the direction of a good article which explains this code step-by-step and a sample project code sorted using this method? My first attempt will be sorting of an ArrayList of my Top20 Chart. Will consist of sorting mainly by alphabetical order (ill decide later what fields to sort.. probably stuff like artist and record company, and general weekly / monthly / year sales (actualy gonna re-code that to be more automated, for those who remember the code from the last time I was here) sorting).

    Is Insertion Sort the right method to go by? Its only something small, nothing big of a project so im just looking for something that does the job - for a beginner. :)

    Cheers lads.


Comments

  • Registered Users Posts: 228 ✭✭Mary-Ellen


    Hey there,
    I'm just gonna explain the first iteration and you should be able to get it from there :rolleyes:


    for(i starting at 1 up to array size)

    {
    temp is = the second array element
    arrays start at 0
    j = 0

    while (second array element is smaller than the first)
    {
    second position of the array is = the first element
    ie the bigger one
    the first position is = the second element
    }
    j--
    }


    so all the while loop does is swap them if the x+1 element is smaller than the x element


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Cheers Mary. Very much appreciated :)


  • Registered Users Posts: 4,287 ✭✭✭NotMe




  • Registered Users Posts: 2,481 ✭✭✭Fremen


    I don't know how experienced you are with java, but you could try learning to use
    Collections.sort(List L, Comparitor c)
    If you're just working with things like Strings, you don't have to define your own Comparitor, and it just returns an alphabetically sorted list.
    More info here

    http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html

    I used to find the java documentation a bit intimidating, but if you stick with it, it'll make sense eventually :)


Advertisement