How does Selection Sort work?
Selection Sort implements a simple sorting algorithm as follows:
- Algorithm repeatedly searches for the lowest element.
- Swap current element with an element having the lowest value
- With every iteration/pass of selection sort, elements are swapped.
Here is how the process works graphically
import java.util.*;
public class SelectionSortAlgo
{
public static void main(String a[])
{
int[] myArray =new int[10];
int n,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of array want to create: ");
n=sc.nextInt();
System.out.println("Enter "+n+" elements of an arrary:");
for(i=0;i<n;i++)
myArray[i]=sc.nextInt();
System.out.println("------Before Selection Sort-----");
printArray(myArray);
selection(myArray);//sorting array using selection sort
System.out.println("-----After Selection Sort-----");
printArray(myArray);
}
public static void selection(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{ System.out.println("Sort Pass Number "+(i+1));
int index = i;
for (int j = i + 1; j < array.length; j++)
{
System.out.println("Comparing "+ array[index] + " and " + array[j]);
if (array[j] < array[index]){
System.out.println(array[index] + " is greater than " + array[j] );
index = j;
}
}
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
System.out.println("Swapping Elements: New Array After Swap:");
printArray(array);
}
}
static void printArray(int[] array)
{
for(int i=0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println();
}
}
public class SelectionSortAlgo
{
public static void main(String a[])
{
int[] myArray =new int[10];
int n,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of array want to create: ");
n=sc.nextInt();
System.out.println("Enter "+n+" elements of an arrary:");
for(i=0;i<n;i++)
myArray[i]=sc.nextInt();
System.out.println("------Before Selection Sort-----");
printArray(myArray);
selection(myArray);//sorting array using selection sort
System.out.println("-----After Selection Sort-----");
printArray(myArray);
}
public static void selection(int[] array)
{
for (int i = 0; i < array.length - 1; i++)
{ System.out.println("Sort Pass Number "+(i+1));
int index = i;
for (int j = i + 1; j < array.length; j++)
{
System.out.println("Comparing "+ array[index] + " and " + array[j]);
if (array[j] < array[index]){
System.out.println(array[index] + " is greater than " + array[j] );
index = j;
}
}
int smallerNumber = array[index];
array[index] = array[i];
array[i] = smallerNumber;
System.out.println("Swapping Elements: New Array After Swap:");
printArray(array);
}
}
static void printArray(int[] array)
{
for(int i=0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println();
}
}
Output:
Enter the number of array want to create:
4
4
Enter 4 elements of an array:
3
3
4
2
7
------Before Selection Sort-----
3 4 2 7 0 0 0 0 0 0
Sort Pass Number 1
Comparing 3 and 4
Comparing 3 and 2
3 is greater than 2
Comparing 2 and 7
Comparing 2 and 0
2 is greater than 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Swapping Elements: New Array After Swap:
0 4 2 7 3 0 0 0 0 0
Sort Pass Number 2
Comparing 4 and 2
4 is greater than 2
Comparing 2 and 7
Comparing 2 and 3
Comparing 2 and 0
2 is greater than 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Swapping Elements: New Array After Swap:
0 0 2 7 3 4 0 0 0 0
Sort Pass Number 3
Comparing 2 and 7
Comparing 2 and 3
Comparing 2 and 4
Comparing 2 and 0
2 is greater than 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Swapping Elements: New Array After Swap:
0 0 0 7 3 4 2 0 0 0
Sort Pass Number 4
Comparing 7 and 3
7 is greater than 3
Comparing 3 and 4
Comparing 3 and 2
3 is greater than 2
Comparing 2 and 0
2 is greater than 0
Comparing 0 and 0
Comparing 0 and 0
Swapping Elements: New Array After Swap:
0 0 0 0 3 4 2 7 0 0
Sort Pass Number 5
Comparing 3 and 4
Comparing 3 and 2
3 is greater than 2
Comparing 2 and 7
Comparing 2 and 0
2 is greater than 0
Comparing 0 and 0
Swapping Elements: New Array After Swap:
0 0 0 0 0 4 2 7 3 0
Sort Pass Number 6
Comparing 4 and 2
4 is greater than 2
Comparing 2 and 7
Comparing 2 and 3
Comparing 2 and 0
2 is greater than 0
Swapping Elements: New Array After Swap:
0 0 0 0 0 0 2 7 3 4
Sort Pass Number 7
Comparing 2 and 7
Comparing 2 and 3
Comparing 2 and 4
Swapping Elements: New Array After Swap:
0 0 0 0 0 0 2 7 3 4
Sort Pass Number 8
Comparing 7 and 3
7 is greater than 3
Comparing 3 and 4
Swapping Elements: New Array After Swap:
0 0 0 0 0 0 2 3 7 4
Sort Pass Number 9
Comparing 7 and 4
7 is greater than 4
Swapping Elements: New Array After Swap:
0 0 0 0 0 0 2 3 4 7
-----After Selection Sort-----
0 0 0 0 0 0 2 3 4 7