Selection Sort.

Program of Selection Sort.


Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.


SOURCE CODE


import java.io.*;
import java.util.*;
public class Selection
{
    public static void main(String args[])throws IOException
    {
        int small,place;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the limit of number");
        int a=sc.nextInt();
        int ar[]=new int [a];
        for(int i=0;i<a;i++)
        {
            System.out.print("Input a No.");
            ar[i]=sc.nextInt();
        }
        System.out.println("Array element before sorting are");
        for(int i=0;i<a;i++)
        {
            System.out.print(ar[i]+"\t");
        }
        for(int i=0;i<a;i++)
        {
            small=ar[i];
            place=i;
            for(int j=i+1;j<a;j++)
            {
                if(ar[j]<small)
                {
                    small=ar[j];
                    place=j;
                }
            }
            int k=ar[i];
            ar[i]=ar[place];
            ar[place]=k;
        }
        System.out.println(" \nArray elements after sorting are");
        for(int i=0;i<a;i++)
        {
            System.out.print(ar[i]+"\t");
        }
    }
}

OUTPUT





If you have any question then leave a comment below I will do my best to answer that question.

Comments

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.