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.

- Start the loop. Since we need to check all elements, we need to start the loop;

```
//Looping array
for (int i = 0; i < array.length; i++)
```

- I am setting two variables. The first variable saves the value of the minimum element. The second variable keeps the index of the minimum element. We will need the first variable to compare with the rest elements. And the second variable to switch with the minimum element by the index if will find one;

```
//Saving as current value for current element
int currentValue = array[i];
//Saving the index of the current element
int currentIndex = i;
```

- Start the second loop inside the first. The second loop aims to compare all elements on the right side plus the current element. And find the minimum value. Updating the minimum value variable and minimum value index;

```
//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.

- Switching elements. Initialize the temporary variable. With its help, we are changing the value of the current element with the minimum element on the right;

```
//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:**

**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!