0

I want to build an algorithm that takes into account a list of integers, sort it.

The idea is: iterate over the list of integers, the first value gets into a new list; the following value is compared with the only existing value in the new list and put it in before or after, and so on.

import java.util.*;
import java.util.function.Supplier;
import java.util.ArrayList;
  
public class SortLists {
    
    public static void main(String args[])
    {
  
        // list [5,1,4,2,8]
        List<Integer> list = new ArrayList<Integer>();
        list.add(5);
        list.add(1);
        list.add(4);
        list.add(2);
        list.add(8);

        System.out.println("list : " + list.toString());
        
        // new empty list
        List<Integer> list2 = new ArrayList<Integer>();
        
        // iterate over the integer list
        for (int i = 0; i < list.size(); i++){
        //int i=2;
        
            // *** 1st iteration - add the value to the list2
            
            if (i == 0){
                list2.add(list.get(i));
            } else {
                
                // *** in the next interations, compare with the existing values in list2
                for (int j = 0; j < list2.size(); j++){

                    
                    // *** Note: this if is unecessary but I got an error with list2.get(j)
                    if (j==0){
                        // if the value from list is lower than the only value in list2, put in index 0
                        if (list.get(i) < list2.get(0) ) {
                             list2.add(j,list.get(i));
                        } 
                        
                    }else{
                         
                        if (list.get(i) < list2.get(j) ) {
                             //list2.add(j,list.get(i)); // GIVES ERROR!
                             System.out.println("index "+j+" value "+list.get(i));
                             
                        } else {
                            //list2.add(list.get(i)); //GIVES ERROR!
                            System.out.println("else: index "+j+" value "+list.get(i));
                        }
                    }
                }
            }
        }
        //list2.add(1,4); // no error if add manually here
        System.out.println("list2 : " + list2.toString());
        
    }
}

The 5 and 1 are correctly inserted in list2.

The other two list2.add, if I uncomment them, the execution doesn't stop.

3
  • because, the i or j will always be less than list2.size() or list.size(), it is like, your adding a value to these lists and at the same time the index pointer is moving is next free space and as arraylist is dynamic, i and j will never meet size of these lists. just to validate try running the code with conditions as i < 10(example value) and j < 10(example value) in for loop definition and see your code will work Commented Oct 8, 2021 at 16:16
  • 2
    You need to break out of the inner loop after adding a value to list2. Commented Oct 8, 2021 at 16:21
  • Calling size() from within the for statement and calling `list.get(i)' within the loop is very inefficient. Commented Oct 8, 2021 at 18:02

2 Answers 2

2

the execution doesn't stop

You get into an infinite loop because in the inner for loop you are adding elements to the second list while iterating over the list.

Besides, you are overcomplicating things unnecessarily. Just store the first index for which list.get(i) < list2.get(j) is true. If such an index exists, add the i-th element from list one to list two at that index. Otherwise, add the i-th element from list one to the end of list two.

public static void main(String[] args) {
    List<Integer> list = new ArrayList<Integer>();
    list.add(1);
    list.add(5);
    list.add(1);
    list.add(4);
    list.add(2);
    list.add(8);

    System.out.println("list : " + list.toString());

    List<Integer> list2 = new ArrayList<Integer>();
    
    list2.add(list.get(0));
    
    for (int i = 1; i < list.size(); i++){
        int index = list2.size();
        for(int j = 0; j < list2.size(); j++){
            if (list.get(i) < list2.get(j)){
                index = j;
                break;
            }
        }
        list2.add(index,list.get(i));
    }

    System.out.println(list);
    System.out.println(list2);
}
Sign up to request clarification or add additional context in comments.

Comments

0

Add j++ after the commented code that gives an error since you are increasing the list size by one. Otherwise it is an infinite loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.