Java Program to selection sort program


April 3, 2022, Learn eTutorial
1480

Here we are explaining how to write a java program to perform selection sort.

What is selection sort in java?

Selection sort is a simple sorting algorithm. Here the entire list is divided into two sublists.

1)Sorted List

2)Unsorted List

The sorted list contains the sorted elements and the unsorted list contains the remaining elements.

At the beginning of the selection sort, the sorted list is empty. Then, in the first iteration, the smallest element is placed in the first position of the array. Then, the second iteration begins from i+1 to the limit of the array. It will find out the smallest element of i+1 to limit and place it in the proper position. Repeat the process until the unsorted list becomes empty.

 

How to implement a java program to perform selection sort?

First, we have to declare the class SelectionSort. Declare the integer variables i,j,temp,limit.Create an object of the scanner class as sc. Read the limit of the array elements as a limit. Declare an integer array of size limit. Read the array elements using a for loop into array[i]. Then by using nested for loop with the condition i<=limit-i-1.Set min=i.Then by using another for loop set j=i+1. Check j

ALGORITHM

STEP 1: Declare the class SelectionSort with a public modifier.

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the integer variables i,j,temp,limit.

STEP 4: Read the limit of the array into the variable limit.

STEP 5: Declare an array of size limit.

STEP 6: Read the elements into the array by using for loop.

STEP 7: By using  for loop with  the condition i

STEP 8: By using another for loop set,j=i+1,check j

STEP 9: Check if array[j]

STEP 10: Swap array[min] to array[j] using temp variable.

STEP 11: Increment i by one and repeat step 7.

STEP 12: Display sorted list as array[i] using for loop.

 

Java Source Code

                                          import java.util.Scanner;

public class SelctionSort {
  public static void main(String []args) {
    int i,j,temp,limit;
    Scanner sc = new Scanner(System.in);
 
    System.out.println("Enter the limit of the numbers:");
    limit = sc.nextInt();
 
    int array[] = new int[limit];
 
    System.out.println("Enter " + limit + " numbers: ");
 
    for (i = 0; i <limit; i++) 
      array[i] = sc.nextInt();
      
      
      
      for ( i = 0; i < limit-1; i++)
        {
           
            int min = i;
            for ( j = i+1; j < limit; j++)
                if (array[j] < array[min])
                    min = j;
 
             temp = array[min];
            array[min] = array[i];
            array[i] = temp;
        }
      
      
 
    System.out.println("******Sorted list******");
 
    for (i = 0; i < limit; i++) 
      System.out.println(array[i]);
  }
}
                                      

OUTPUT

Enter the limit of the numbers:5
Enter 5 numbers: 
56
2
89
1
400
******Sorted list******
1
2
56
89
400