Selection Sort An Array | Write a program in Java to selection sort an array. | ISC Computer Science | ICSE Computer Applications

Selection Sort in Java

Algorithm

  1.   Start
  2. a[]=null
  3. i:=0
  4. n:=0
  5. j:=0
  6. small:=0
  7. Print "Enter the size of the array"
  8. Read n
  9. a=new int[n]
  10. i=0
  11. If i<n, go to step 12 or else go to step 15
  12. Print "Enter a number"
  13. Read a[i]
  14. i++. Go to step 11
  15. i=0
  16. If i<n, go to step 17 or else go to step 26
  17. small=a[i]
  18. j=i
  19. If j<n, go to step 20 or else go to step 24
  20. If small>a[j], go to step 21 or else go to step 23
  21. small=a[j]
  22. a[j]=a[i]
  23. j++. Go to step 19
  24. a[i]=small
  25. i++. Go to step 16
  26. i=0
  27. If i<n, go to step 28 or else go to step 30
  28. Print a[i]
  29. i++. Go to step 27
  30. Stop

Program

Buffered Reader

import java.io.*;
class SortArray
{
    int a[], i, n, j, small;
    
    void accept() throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader (System.in));
        System.out.println("Enter the size of the array");
        n=Integer.parseInt(br.readLine());
        a=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number");
            a[i]=Integer.parseInt(br.readLine());
        }
    }
    
    void sort()
    {
        for(i=0;i<n;i++)
        {
            small=a[i];
            for(j=i;j<n;j++)
            {
                if(small>a[j])
                {
                    small=a[j];
                    a[j]=a[i];
                }
            }
            a[i]=small;
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(a[i]);
        }
    }
    
    void main() throws IOException
    {
        accept();
        sort();
        display();
    }
}

Scanner

import java.util.*;
class SortArray
{
    int a[], i, n, j, small;
    
    void accept() 
    {
        Scanner sc=new Scanner (System.in);
        System.out.println("Enter the size of the array");
        n=sc.nextInt();
        a=new int[n];
        for(i=0;i<n;i++)
        {
            System.out.println("Enter a number");
            a[i]=sc.nextInt();
        }
    }
    
    void sort()
    {
        for(i=0;i<n;i++)
        {
            small=a[i];
            for(j=i;j<n;j++)
            {
                if(small>a[j])
                {
                    small=a[j];
                    a[j]=a[i];
                }
            }
            a[i]=small;
        }
    }
    
    void display()
    {
        for(i=0;i<n;i++)
        {
            System.out.println(a[i]);
        }
    }
    
    void main() 
    {
        accept();
        sort();
        display();
    }
}

Documentation

Variable Description

 Data TypeVariable NameDescription
 int[]To store an array of numbers 
 intTo store the size of the array 
 int To act as a counter variable 
 intTo act as a counter variable 
 intsmall To store the smallest variable 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user 
 voidsort() To sort data in the array 
 voiddisplay() To display the array 
 voidmain() To act as an entry gateway for the compiler 

Output


Selection Sort Output




Share with all ISC Computer Science Students you know.

Comment below with all your program requests and blog suggestions.

Follow us on social media by clicking on these links. 
Instagram : @ISCJavaCode
Twitter: @ISCJavaCode
Reddit: @ISCJavaCode
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode
LinkedIn : ISC Java Code 


Stay Healthy
Stay Happy
Stay Helpful



Comments

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences