Selection Sort in Java with examples

0

Selection sorting is inefficient with a large number of unsorted data. As its name selection, it does the same. It get the lowest number from the unsorted list and update the unsorted data.

Selection Sort Java Algorithm:-

Suppose we have an unsorted array of size n. The first time it will find the lowest element from the unsorted list. Suppose at index m we get the lowest value, this value will be exchanged with the first element.

Now we have n-1 (from index 1 to n-1) we have an unsorted list, now get the lowest value from this unsorted list and exchange with the second element. Repeat this procedure until we do not have unsorted list.

Implementation of Java Selection Sort:-

package one;
import java.util.ArrayList;
import java.util.Random;
public class SelectionSort
{
public static void main(String[] args)
{
ArrayList<Integer> list = new ArrayList<Integer>();
Random rand = new Random();
for (int i = 0; i < 10 ; i++)
{
list.add(rand.nextInt(100));
}
System.out.println("Unsorted List ");
for (Integer i : list)
System.out.print(i + ", ");
SelectionSorting(list);
System.out.println("\nSorted List ");
for (Integer i : list)
System.out.print(i + ", ");
}
private static void SelectionSorting(ArrayList<Integer> list)
{
int temp;
int minIndex;
for (int outer = 0; outer < list.size()-1 ; outer++)
{
minIndex = outer;
// Get the lowest element
for (int inner = outer; inner < list.size()-1; inner++)
{
if (list.get(minIndex) > list.get(inner))
{
minIndex = inner;
}
}
// Swap the number if we have lowest number
// put the number at position outer
if (minIndex != outer)
{
temp = list.get(minIndex);
list.set(minIndex, list.get(outer));
list.set(outer, temp);
}
}
}
}

Output is:

Unsorted List:-

27, 53, 1, 33, 16, 99, 33, 80, 14, 77.

Sorted List:-

1, 14, 16, 27, 33, 33, 53, 80, 99, 77.

Selection Sort Java Explanation:-

Suppose we have unsorted list as:

27, 53, 1, 33, 16, 99, 33, 80, 14, 77.

For outer loop set minIndex as 0; // Just suppose that at index list has the lowest element.

Now inner loop will get the index of the lowest value in the list.

1 is the lowest value whose index is 2. So now value at index 0 and 2 will swap. Now new list is:

1, 53, 27, 33, 16, 99, 33, 80, 14, 77.

Outer loop will set minInex as 1 // // Just suppose that at index list has the lowest element.

Now inner loop will get the index of the lowest value in the list.

14 is the lowest value whose index is 8. So now value at index 1 and 8 will swap. Now new list is:

1, 14, 27, 33, 16, 99, 33, 80, 53, 77.

Outer loop will set minInex as 2  // Just suppose that at index list has the lowest element.

Now inner loop will get the index of the lowest value in the list.

16 is the lowest value whose index is 4. So now value at index 1 and 4 will swap. Now new list is:

1, 14, 16, 33, 27, 99, 33, 80, 53, 77.

Loop will go till the last index and do same as above, finally sorted list would be:

1, 14, 16, 27, 33, 33, 53, 80, 99, 77.

5/5 - (1 vote)

royal52
royal52

I’m a DevSecOps Software engineer by profession and a SEO hobbyist. I’m passionate about everything Software, including game development.

We will be happy to hear your thoughts

Leave a reply