Easily solution of selection sort of Array List in Java. – IT Blog
IT Blog
Boris IT Expert

Easily solution of selection sort of Array List in Java.

Boris~November 11, 2020 /Sorting Algorithms

How to sort an array list with the help of the selection sort in Java? If the interviewer asked you to write a code for the technical interview. I give 90% that you will have to sort something. One of my preferred sorting algorithms is the selection sort. Why? Because for me, it looks pretty straight forward. It’s easy to understanding and easy to implement. Here, the main plan is to point to the first element and find any value less than the current element’s value. If we find the value, which is less, we are switching two elements. After that, move to the next and next after next until the list is over.

Selection sort of the Array List? The step-by-step solution in Java.

Detailed implementation with the pictures. The most apparent solution below:

First part. The first part aims to find the minimum value between the elements on the sorted side’s right.

//Looping array
        for (int i = 0; i < array.length; i++) 
selection sort java
//Saving as current value for current element
            int currentValue = array[i];
//Saving the index of the current element
            int currentIndex = i;
//Start loop to compare current element value ALL OTHER elements
            for (int j = i + 1; j < array.length; j++) {
//Comparing j element value with current element value
                if (array[j] < currentValue) {
//If j value less that current element value
//Saving / updating minimum value with a new found one
                    currentValue = array[j];
//Saving the index of the minimum value. We will use it in switching elements later
                    currentIndex = j;
                }
            }

Second part. When we have found the minimum value on the right unsorted side, we need to switch it with the current element.

//After all next elements after current element were looped we need to swap minimum value with current element
//In temporary variable we are saving value of current item for swap
            int temporary = array[i];
//In that place we are saving minimum value that we got after j loop
            array[i] = currentValue;
//And in the place where was the minimum value we put current element value
            array[currentIndex] = temporary;

Full schema:

selection sort java

Full code:

private void selectionSort() {
//Creating new array
    int[] array = new int[]{7, 1, 5, 4, 2, 13, 3, 11};

//Looping array
    for (int i = 0; i < array.length; i++) {
 //Saving as current value for current element
        int currentValue = array[i];
 //Saving the index of the current element
        int currentIndex = i;
 //Start loop to compare current element value ALL OTHER elements
        for (int j = i + 1; j < array.length; j++) {
//Comparing j element value with current element value
            if (array[j] < currentValue) {
//If j value less that current element value
//Saving / updating minimum value with a new found one
                currentValue = array[j];
//Saving the index of the minimum value. We will use it in switching elements later
                currentIndex = j;
            }
        }

 //After all next elements after current element were looped we need to swap minimum value with current element
 //In temporary variable we are saving value of current item for swap
        int temporary = array[i];
 //In that place we are saving minimum value that we got after j loop
        array[i] = currentValue;
//And in the place where was the minimum value we put current element value
        array[currentIndex] = temporary;
    }

    for (int i : array) {
        Log.d("arrayList", "result !-> " + i);
    }
}

In this article, I show you the selection sort algorithm on the Array List (Java).

Full code and more examples in my facebook group.

Useful links:

Leave Any Questions Related To This Article Below!