Write a Java Program to perform Selection Sort using static function

Java Program to perform Selection Sort


Selection sort is a sorting algorithm which sorts the given array elements either in descending or ascending order by finding the misplaced element first and putting it in it’s final place.

Selection sort algorithm first selects the lowest (or highest) element and places it in the ordered position. Selection sort is recommended only for small size arrays (less than 1000 elements).

It doesn’t give much performance as compared to other sorting algorithms when used on large size arrays.

It has O(n2) time complexity, making it inefficient on large lists, and generally performs worse than the similar insertion sort.

Selection sort is noted for its simplicity, and it has performance advantages over more complicated algorithms in certain situations, particularly where auxiliary memory is limited.

Worst case performance : О(n2)
Best case performance : O(n2)
Average case performance : О(n2)


Here is source code of the Java Program to implement Selection Sort. The Java program is successfully compiled and run on a Windows system(NetBeans). The program output is also shown below.

 

SOURCE CODE : :

 

import java.util.Scanner;

public class SelectionSort {
    
    //------------------Enter data Function--------------------------------
    
    static int [] enter_data()
    {
        int i,n;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter no. of elements u want to sort :");
        n=sc.nextInt();
        int a[]=new int[n];
        System.out.println("Enter elements--->");
        
        for(i=0;i<n;i++)
        {
            System.out.print((i+1)+" Element : ");
            a[i]=sc.nextInt();
        }
        return a;
    }

    //--------------------Selection Sort Function-----------------------------
    
    static int [] selectionsort(int a[])
    {
        int i,j,temp,len,min;
        len=a.length;
        
        for(i=0;i<len-1;i++)
        {
            min=a[i];
            for(j=i+1;j<len;j++)
            { 
                if(min>a[j])
                {
                    temp=a[j];
                    a[j]=min;
                    min=temp;
                }
                a[i]=min;
            }
        }
        
        return a;
    }
    
    //-----------------Print array---------------------------------------
    
    static void printarray(int arr[])
    {
        int i,len;
        len=arr.length;
         System.out.println("\nAfter Sorting Elements are : ");
         for(i=0;i<len;i++)
        {
            System.out.println((i+1)+" Element : "+arr[i]);
        }
    }
    
    //-----------------------Main Function-------------------------------
    
    public static void main(String[] args) {
        
        int a[],b[];
        
        a=enter_data();
        
        b=selectionsort(a);
        
        printarray(a);
        
    }
    
}

 

OUTPUT : :

 

Enter no. of elements u want to sort :5
Enter elements--->
1 Element : 3
2 Element : 1
3 Element : 8
4 Element : 5
5 Element : 7

After Sorting Elements are : 
1 Element : 1
2 Element : 3
3 Element : 5
4 Element : 7
5 Element : 8

 

One Comment

Leave a Reply