How to Do Sorting in Array List Without Using Collections in Java

How to do sorting in array list without using collections in Java

arraylist.get(i)= arraylist.get(i);
arraylist.get(j) =tmp;

You can't assign a value to a method call. As the compiler told you, the left hand side of an assignment must be a variable.

Use set method :

arraylist.set(i,arraylist.get(j));
arraylist.set(j,tmp);

Is there a way to do it without using set method?

No. Unless you wish to convert your ArrayList to an array, sort the array, and update the ArrayList with the sorted array.

Sorting objects in an ArrayList without using Collections.sort

Accessing an array is different from accessing an ArrayList. This is because these two objects are fundamentally different.

Let's focus on this line of code:

System.out.println(Arrays.toString(empList) + i + " << First Loop interation");

You're going to want to bookmark the Java 7 API so that you can reference what it is these methods actually take as arguments. Believe me, it will save you lots of time in the long run.

Specifically, the code is invalid because toString does not accept a parameter of type ArrayList. You can just straight-up print an ArrayList, as it has a reasonable toString method, whereas an array doesn't (which is why you use Arrays#toString):

System.out.println(empList.toString() + i + " << First Loop interation");

Let's look at this if block next:

if (empList.get(j) > empList.get(j + 1)) {    //I get errors here in Eclipse and
temp = A[j]; //up above when I use toString
A[j] = A[j + 1];
A[j + 1] = temp;
}

I'll be blunt, you're going to get errors in any reasonable IDE with that code. The reason: you index into arrays with brackets, but you use get for an ArrayList.

The first fix is that you can't compare those two instances with >. What you'd wind up doing instead is retrieving the field you want to compare it with instead.

if(empList.get(j).getEmpNum() > empList.get(j+1).getEmpNum()) {
// more code
}

Here's the relevant Javadoc for ArrayList. You're going to need it.

Let's focus on the inner part of the if. The operation you're doing there is called a swap. You're taking an element from one location and overwriting it with another. Since arrays don't shift elements down, you have to capture the original value before you overwrite it.

To put it in English:

  • Take original value
  • Place new value in original value's original array location
  • Place original value in new value's original array location

You shouldn't have to do that with an ArrayList, as it can add the element in a specific spot.

In English, it should be as simple as:

  • Insert new value in original value's spot
  • Delete new value's occurrence in the list

In Java, it might read like this:

if(empList.get(j).getEmpNum() > empList.get(j + 1).getEmpNum()) {
empList.add(j, empList.get(j + 1));
empList.remove(j + 1);
}

Sorting an ArrayList without using 'Collections.sort'

You don't need to use Collections.sort()' here. You just want to maintain order, and that list interface takes care and remove else i++. If x is greater than multiple elements in the list, then your code adding x multiple times thats why you are getting duplicates here.

if (x == 2) break;

for (int i = 0; i < list.size() - 1; i++) {
if (x > list.get(i)) {
list.add(i, x);
break;
}
}

How do i alphabetize this ArrayList without using the Collections.sort method?

Looks like I can't explain myself well in a comment, so I will let my code do the talking for me.

Your loop that implements the bubble sort should be as follows:

String temp;
for (int i = 0; i < words.size(); i++) {
for (int j = i + 1; j < words.size(); j++) {
if (words.get(i).compareTo(words.get(j)) < 0) {
temp = words.get(i);
words.set(i, words.get(j));
words.set(j, temp);
}
}
}

Note that this sorts the list in descending order because of the comparison you are doing, i.e.

if (words.get(i).compareTo(words.get(j)) < 0) {

If you want ascending order, change < 0 to > 0

I tried to explain, in my comment, that you weren't assigning a value to local variable temp inside the loop.

How can I sort an arraylist without using collections.sort()?

I assume you want the following algorithm: find min in the rest of the array, swap it with current element starting with first, reconsider rest to be array starting of increased +1 index.

You should update your code like this:

public static void swap(List<Integer> sort, int i, int j) {
int tmp = sort.get(i);
sort.set(i, sort.get(j));
sort.set(j, tmp);
}

public static void doSort(List<Integer> sort) {
int min;
for (int i = 0; i < sort.size(); ++i) {
//find minimum in the rest of array
min = i;
for (int j = i + 1; j < sort.size(); ++j) {
if (sort.get(j) < sort.get(min)) {
min = j;
}
}

//do swap
swap(sort, i, min);
}
}

You have a bug with finding minimum and then swapping items. Please note that the code can be improved in many ways (I tried to maintain your let's say way of coding as possible), such as swapping integer references in swap(), doing BubbleSort like another answer suggests (same algorithm but simpler implementation), using O(n * log(n)) complexity algorithm, and so on.

Sort an Arraylist without modifying the original list

Well, you don't actually need to make a deep copy unless your Comparator is mutating your objects, which it shouldn't. The easiest way is therefore to make shallow copy:

ArrayList<String> original = new ArrayList<>();
ArrayList<String> copy = new ArrayList<>(original);
copy.sort(Comparator.naturalOrder());

Replace Comparator.naturalOrder() with your actual Comparator implementation. For example, if you are comparing a member field, you can use Comparator.comparing as an easy way to create your desired Comparator.

And to answer your question: No, it's not possible without an additional data structure because on the one hand you want to alter the order of the elements, i.e. sort them, and on the other hand you want them to stay in the same order.

how to use sorting in java without using the collections.sort(list);?

You can implement using Quick Sort (based on Divide and Conquer Algorithm) whose average case is Θ(n log(n)) and in the worst case it is Θ(n2). It sorts by partitioning your array into left and right sub-arrays, then it takes a pivot value and processes the entire array. This is one of the reason why quick sort is also fast. You can find its implementation on Quick Sort. They have done the entire implementation of Quick Sort without the use of Collections.sort(which is basically your requirement too).

Hope this helps!



Related Topics



Leave a reply



Submit