Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Sorting

  • 22-04-2007 10:47PM
    #1
    Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,071 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, Registered Users 2 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,071 Mod ✭✭✭✭Sully


    Cheers Mary. Very much appreciated :)


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




  • Registered Users, Registered Users 2 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