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

Thread: What is the logic behind sorting an array list in this algorithm?

  1. #1
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Unhappy What is the logic behind sorting an array list in this algorithm?

    The method below is intended to use an insertion sort. For example: if I had a String ArrayList of elements ["code", "computer", "area", "school"], it should sort the elements by insertion sort into ["area", "code", "computer","school"]. I'm having a hard time understanding the logic, and have watched many videos.

    I'm mainly struggling to understand what the two for loops are doing.

     public static int insertSort(ArrayList<String> list) {
      int count = 0;
     
      for (int i = 1; i < list.size(); i++)
      {
        String toInsert = list.get(i);
        int j;
     
        for (j = i; j > 0; j--)
        {
          count++;
          if (toInsert.compareTo(list.get(j-1)) >= 0)
          {
            break;
          }
        }
     
        list.add(j, list.remove(i));
      }
      return count;
     }
    Last edited by Codestudent121; April 24th, 2021 at 09:54 AM. Reason: Added formatting

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What is the logic behind sorting an array list in this algorithm?

    How have you tried tracing the execution of the code to see what it is doing?
    Try adding some print statements that print out the values of the variables as they are set and used. The print out will help you see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Codestudent121 (April 24th, 2021)

  4. #3
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is the logic behind sorting an array list in this algorithm?

    Thanks. I've tried tracing loops, and kinda get it, but I still struggle to get why I need two loops instead of one. Is it possible to do an insertion sort with one loop?
    Last edited by Codestudent121; April 24th, 2021 at 10:54 AM.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What is the logic behind sorting an array list in this algorithm?

    why I need two loops instead of one
    Does the code work without both loops? Try it and see what happens. Be sure to use different sets of input data for testing.
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    Codestudent121 (April 24th, 2021)

  7. #5
    Junior Member
    Join Date
    Apr 2021
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What is the logic behind sorting an array list in this algorithm?

    Thanks so much for your help! You made me realize that I need the second loop to simultaneously compare 2 values in the array and move them around. Also, how do I do a selection sort on an ArrayList and find the median? I'm studying unit 7 for AP CSA and I'm stuck on sorting and searching. Do I need to make a new post for those topics?

    The areas I'm struggling with are sorting and searching (2d and 3d arrays), tracing recursion, and understanding polymorphism/inheritance.

    I was also confused a lot by the second loop since 'i' could be 1 and it seems like it might not work.
    Last edited by Codestudent121; April 24th, 2021 at 11:26 AM.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What is the logic behind sorting an array list in this algorithm?

    Do I need to make a new post for those topics?
    Yes a new thread for a new topic would be good.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Algorithm for card sorting
    By Scorks in forum Algorithms & Recursion
    Replies: 1
    Last Post: February 10th, 2014, 06:09 AM
  2. Issues with sorting algorithm
    By RAWBERRY in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 1st, 2012, 05:13 PM
  3. Reading file of floats into array list and sorting into subsets
    By Skave in forum Collections and Generics
    Replies: 2
    Last Post: November 9th, 2011, 07:03 PM
  4. Basic Java Sorting Algorithm (Selection/Insertion) help
    By Arte7 in forum Algorithms & Recursion
    Replies: 5
    Last Post: August 22nd, 2011, 01:38 PM
  5. 2 Java methods and a sorting algorithm
    By Tiberius in forum Algorithms & Recursion
    Replies: 0
    Last Post: February 26th, 2011, 03:41 PM

Tags for this Thread