SELECTION SORT

import java.io.*; class SelectionSort {             public static void main(String args[])throws IOException             { Buffe...

import java.io.*;
class SelectionSort
{
            public static void main(String args[])throws IOException
            {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
                        System.out.println("ENTER ARRAY SIZE");
                        int n=Integer.parseInt(br.readLine());
                        int a[]=new int[n];
                        System.out.println("ENTER ARRAY ELEMENTS");
                        for(int i=0;i < n;i++)
                        {
                                    System.out.println("ENTER A["+i+"]ELEMENT");
                                    a[i]=Integer.parseInt(br.readLine());
                        }
                        ss s=new ss();
                        s.sort(a);
                        for(int i=0;i < n;i++)
                        {
                                    System.out.println("a["+i+"]="+a[i]);
                        }
            }
}

class ss
{
  public int[] sort(int a[])
 {
   int n=a.length,loc=0;
   for(int i=0;i < n;i++)
  {
    int min=a[i];
    for(int j=i+1;j < n;j++)
   {
      if(a[j]
      {
        min=a[j];
        loc=j;
      }
   }
   if(a[i]>min)
   {
    int temp=a[i];
    a[i]=min;
    a[loc]=temp;
   }
 }
return a;
 }
}


OUTPUT:

ENTER ARRAY SIZE
5
ENTER ARRAY ELEMENTS
ENTER A[0]ELEMENT
33
ENTER A[1]ELEMENT
22
ENTER A[2]ELEMENT
11
ENTER A[3]ELEMENT
55
ENTER A[4]ELEMENT
44
a[0]=11
a[1]=22
a[2]=33
a[3]=44

a[4]=55

Related

Data Structures 8831790313415790644

Post a Comment

emo-but-icon

item