All Posts

Selection Sort Java Program

Selection Sort Java Program

In this tutorial, we will learn to write a Java Program for Selection Sort. Selection sort is one of the simple sorting techniques.

Sorting is a process of arranging things in a specified order, for example in ascending or descending order. There are many algorithms for sorting.

Consider array a, using this technique you will find the smallest element in the list and interchange it with the first element in the list i.e a[0]. In the next pass, you will find the next smallest element in the rest of the list ( a[1]…. end of the list ) and interchange with the element a[1] so on…

After all the array passes you will be left with the sorted array.

 

Selection sort

Eclipse IDE Setup

Some steps and instructions to create a Java application on Eclipse IDE:

  • Create Java Project in Eclipse

https://www.testingdocs.com/create-a-new-java-project-in-eclipse/

  • Create Java Package in Eclipse

https://www.testingdocs.com/create-java-package-in-eclipse-ide/

  • Create Java Class in Eclipse

https://www.testingdocs.com/create-a-new-java-class-in-a-project/

  • Run Java Project in Eclipse

https://www.testingdocs.com/run-java-project-in-eclipse/

Java program

import java.util.InputMismatchException;
import java.util.Scanner;

public class SelectionSort {
    public static void main(String[] args) {
        int n , minIndex;
        Scanner input = null;
        int[] a = null;
        try {
            input=new Scanner(System.in);
            System.out.println("Enter size of the array:");
            n= input.nextInt();

            if(n > 0) {
                a=new int[n];
                System.out.println("Enter array elements:");
                for (int i = 0; i < n; i++) {
                    a[i] = input.nextInt();
                }
            }else{
                System.out.println("Enter positive number.");
                System.exit(0);
            }

            System.out.println("Input Array:");
            printArray(a);

            for(int i=0;i<a.length;i++){
                minIndex=i;
                for(int j=i+1;j<a.length;j++){
                    if(a[j]<a[minIndex])
                        minIndex=j;
                }
                swap(a,i,minIndex);
                System.out.println("Smallest element marked **") ;
                printArray(a,i);
            }
            System.out.println("Sorted Array:");
            printArray(a);
        }catch (InputMismatchException ime) {
            System.out.println("Not a valid input");
        }
        catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(input!=null)
            {input.close();}
        }
    }

    private static void printArray(int[] a){
        for (int anA : a) {
            System.out.print("[ " + anA + " ]");
        }
        System.out.println( "");
    }

    private static void printArray(int[] a, int index){
        for (int i=0;i< a.length;i++) {
            if(i == index)
                System.out.print("[* " + a[i] + "*]");
            else
                System.out.print("[ " + a[i] + " ]");
        }
        System.out.println( "");
    }

    private static void swap( int[] a, int i , int j)
    {
        int temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
}

 

Run output

Selection Sort Program

 

 

 

Enter size of the array:
5
Enter array elements:
23
56
9
103
77
Input Array:
[ 23 ][ 56 ][ 9 ][ 103 ][ 77 ]
Smallest element marked **
[* 9*][ 56 ][ 23 ][ 103 ][ 77 ]
Smallest element marked **
[ 9 ][* 23*][ 56 ][ 103 ][ 77 ]
Smallest element marked **
[ 9 ][ 23 ][* 56*][ 103 ][ 77 ]
Smallest element marked **
[ 9 ][ 23 ][ 56 ][* 77*][ 103 ]
Smallest element marked **
[ 9 ][ 23 ][ 56 ][ 77 ][* 103*]
Sorted Array:
[ 9 ][ 23 ][ 56 ][ 77 ][ 103 ]

 

—

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/in/java/