Monday, 19 November 2018

Selection Sorting algorithm with example and program

One of the easiest ways to sort a list is by selection. Beginning with the first element in the array, a search is performed to locate the smallest element. When this item is found, it exchanged with the first element. This interchange places the smallest element in the first position of the array.

A search for the second smallest element is then carried out. Examining the items from second position on words does this. The smallest element is exchanged with the item in second position. This process continues until all elements of the array have been sorted in ascending order.
 
 

/* Java Program Example - Selection Sort */
       
import java.util.Scanner;

public class selectionSort
{
   public static void main(String args[])
   {
       int size, i, j, temp;
       int arr[] = new int[50];
       Scanner scan = new Scanner(System.in);
      
       System.out.print("Enter Array Size : ");
       size = scan.nextInt();
      
       System.out.print("Enter Array Elements : ");
       for(i=0; i       {
           arr[i] = scan.nextInt();
       }
      
       System.out.print("Sorting Array using Selection Sort Technique..\n");
       for(i=0; i       {
           for(j=i+1; j           {
               if(arr[i] > arr[j])
               {
                   temp = arr[i];
                   arr[i] = arr[j];
                   arr[j] = temp;
               }
           }
       }
      
       System.out.print("Now the Array after Sorting is :\n");
       for(i=0; i       {
           System.out.print(arr[i]+ "  ");
       }
   }
}



Complexity of selection sort 
  1. Average Case complexity- O(n^2)
  2. Worst Case Complexity - O(n^2)
  3. Best Case Complexity - O(n^2)

0 comments :

Post a Comment

Note: only a member of this blog may post a comment.

Machine Learning

More

Advertisement

Java Tutorial

More

UGC NET CS TUTORIAL

MFCS
COA
PL-CG
DBMS
OPERATING SYSTEM
SOFTWARE ENG
DSA
TOC-CD
ARTIFICIAL INT

C Programming

More

Python Tutorial

More

Data Structures

More

computer Organization

More
Top