Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7443907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:24:25+00:00 2026-05-29T11:24:25+00:00

Hi I am using the following insertion sort algorithm and I would like to

  • 0

Hi I am using the following insertion sort algorithm and I would like to record the elements being compared. Basically I want the comparisons to be stored in two array lists, arrList1 and arrList2. If an element is in its correct place then both array lists will have the same element. If its not then arrList1 will have the chosen element and arrayList2 will have the element its comparing itself to. I am currently struggling to do this, so I was wondering if anyone can help? Thanks

public static ArrayList<Integer> arrList1 = new ArrayList<Integer>();
public static ArrayList<Integer> arrList2 = new ArrayList<Integer>();
...
        public static void insertSort(int[] A){
      for(int i = 1; i < A.length; i++){
        int value = A[i];
        int j = i - 1;
        while(j >= 0 && A[j] > value){
          A[j + 1] = A[j];
          j = j - 1;
        }
        A[j + 1] = value;
      }
    }

EDIT:
For example: If my array had the numbers 1,3,2,4,6,5 then when comparing, this is how I want my two array lists to look:

       arrList1    arrList2
#1        1           1
#2        3           3
#3        2           3 (As 2 goes before 3 then it must be compared to 3)
#4        4           4
#5        6           6
#6        5           6 (As 5 is lower then 6 then it must be compared to 6)

Sorted array: 1, 2, 3, 4, 5, 6

As you can see, arrList1 is basically the order of the input array whilst arrList2 is the element it compares to if its not in correct position. If its in correct position then arrList 2 will be same value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:24:25+00:00Added an answer on May 29, 2026 at 11:24 am

    There you have…

    public class Test {
        public static List<Integer> arrList1;
        public static ArrayList<Integer> arrList2 = new ArrayList<Integer>();
    
        public static void main (String... args){
            Integer[] toSort = {1, 3, 2, 4, 6, 5};
    
    
            arrList1 = Arrays.asList(Arrays.copyOf(toSort, toSort.length));
            insertSort(toSort);
    
            List<Integer> sortedArray = new ArrayList<Integer>();
            for(int aux: toSort){
                sortedArray.add(aux);
            }
    
            System.out.println("Array list 1: " + arrList1);
            System.out.println("Array list 2: " + arrList2);
            System.out.println("Sorted array: " + sortedArray);
        }
    
        public static void insertSort(Integer[] A){
            arrList2.add(arrList1.get(0));
            for(int i = 1; i < A.length; i++){
                int value = A[i];
                int j = i - 1;
                if ((j >= 0 && A[j] > value)){
                    arrList2.add(A[j]);
                }else{
                    arrList2.add(A[i]);
                }
                while(j >= 0 && A[j] > value){
                    A[j + 1] = A[j];
                    j = j - 1;
                }
    
                A[j + 1] = value;
            }
        }
    }
    

    The output of this code for me is:

    Array list 1: [1, 3, 2, 4, 6, 5]
    Array list 2: [1, 3, 3, 4, 6, 6]
    Sorted array: [1, 2, 3, 4, 5, 6]

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using following class to show a grid like appearance with pagination on
I'm trying to find a way to make the following algorithm being processed on
I'm using following function before insertion html markup data into db table function html($data,
I am using the rails 3 gem nested_forms and would like to change how
I am using following PHP code to connect to MS Access database: $odb_conn =
I am using following storedprocedure set ANSI_NULLS ON set QUOTED_IDENTIFIER ON GO Alter PROCEDURE
I am using following code in rowdatabound function. Protected Sub gvwMileStone_RowDataBound(ByVal sender As System.Object,
I'm using following peace of code to output an array: echo ======output without array_unique=====;
I am using following code to check whether a check box on my website
I am using following snippet to build a 32 bit integer to use with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.