-1

I need to write a method to sort an ArrayList in increasing order without using any built-in libraries or methods (not including java.util.ArrayList, which may be used to allow ArrayLists, but for nothing else). I have my near complete code below, but the .remove() function does not seem to be working; it does nothing, and the ArrayList ends up as the smallest element repeated for the entire size of the ArrayList. I tried replacing the while loop with a for loop with the update segment being tempDataCopy.remove(smallestElementIndex) but it gave multiple errors, saying that the .remove() function has an "unknown source". How do I fix this?

    public static ArrayList<Integer> sortUp(ArrayList<Integer> data) {
    ArrayList<Integer> increasingArray = new ArrayList<Integer>();
    ArrayList<Integer> tempDataCopy = data;// tempDataCopy was created so that elements may be deleted without affecting the actual ArrayList data
    int smallestElement = tempDataCopy.get(0);
    int smallestElementIndex = 0;
    while (tempDataCopy.size() > 0) {
        for (int i = 0; i < tempDataCopy.size(); i++) {
            if (tempDataCopy.get(i) < smallestElement) {
                smallestElement = tempDataCopy.get(i);
                smallestElementIndex = i;
            } // end if statement
        } // end for loop
        increasingArray.add(smallestElement);
        tempDataCopy.remove(smallestElementIndex);
    } // end while loop
    return increasingArray;
}// end sortUp

Sorry if this is a duplicate, I searched for hours and could not find another example of similar sorting.

Walshy231
  • 9
  • 2
  • tempData is actually just a reference to the existing array. So you modify both if you change one of them. Use new ArrayList<>(data) to create a copy. – Yannic Bürgmann Nov 28 '18 at 06:08
  • One of the problem(s) of your code is https://stackoverflow.com/questions/6536094/java-arraylist-copy . – user202729 Nov 28 '18 at 06:10

2 Answers2

0
public static ArrayList<Integer> sortUp(ArrayList<Integer> data) {
  ArrayList<Integer> increasingArray = new ArrayList<Integer>();
  ArrayList<Integer> tempDataCopy = new ArrayList<>(data);// tempDataCopy was created so that elements may be deleted without affecting the actual ArrayList data

  // moved initialization of smallestElement in while loop
  while (tempDataCopy.size() > 0) {
    int smallestElement = tempDataCopy.get(0);
    int smallestElementIndex = 0;
    for (int i = 1; i < tempDataCopy.size(); i++) {
        if (tempDataCopy.get(i) < smallestElement) {
            smallestElement = tempDataCopy.get(i);
            smallestElementIndex = i;
        } // end if statement
    } // end for loop
    increasingArray.add(smallestElement);
    tempDataCopy.remove(smallestElementIndex);     
  } // end while loop
  return increasingArray;
}// end sortUp

This resets the smallest element to the first on each while loop iteration. Your error occured for this example: 4,3,2,1.

After first Iteration of while loop your tempDataCopy looks like this: 4,3,2

But your smallestElement is still 1 and you dont find a smaller value in your next iteration. So you're adding 1 again and trying to remove element at index 3, which is lo longer existing

Regarding your errors I don't think the errot states that the remove method is unknown but the element to remove is not existing.

Yannic Bürgmann
  • 6,301
  • 5
  • 43
  • 77
  • I moved the initialization of smallestElement inside the while loop, but it only half fixed the problem. If I fill the ArrayList myself (`data.add(1);` `data.add(2);` etc.), then the code works; but this and the program it belongs to are meant to read from a file. All the other methods in the program work fine reading a file, but this method gives an error on the line containing the `.remove()`, saying "at java.base/jdk.internal.util.Preconditions.checkIndex(Unknown Source) at java.base/java.util.Objects.checkIndex(Unknown Source) at java.base/java.util.ArrayList.remove(Unknown Source)" – Walshy231 Nov 28 '18 at 15:48
  • You describe a problem but don't show code for it. The Problem you described above and what you showed is solved. If you have another problem, either consider to ask a new question specfically for this problem or append information to your existing question (what will reduce the reusability for other users) – Yannic Bürgmann Nov 28 '18 at 21:03
0

You have to remove this code and put it inside your if condition.

increasingArray.add(smallestElement);
tempDataCopy.remove(smallestElementIndex);
joemokenela
  • 299
  • 1
  • 9