About

Kannur University btech CSE study materials, question papers, syllabus . . .

Thursday, July 4, 2013

S4 - DSA LAB -SELECTION SORT



             
               /*             SELECTION SORT           */

import java.io.*;
class Sort
{
DataInputStream ss=new DataInputStream(System.in);
int a[],i,j,l,min;
void read()
{
try
{
System.out.println("Enter the limit  :   ");
l=Integer.parseInt(ss.readLine());
a=new int[l];
System.out.println("Enter the elements     :");
for(i=0;i<l;i++)
a[i]=Integer.parseInt(ss.readLine());
}
catch(Exception e)
{}
}
void sort()
{
int temp,posmin;
for(i=0;i<l;i++)
{
posmin=i;
for(j=i+1;j<l;j++)
{
if(a[posmin]>a[j])
posmin=j;
}
min=posmin;
temp=a[min];
a[min]=a[i];
a[i]=temp;
}
System.out.println("Elements in ascending order  :  ");
for(i=0;i<l;i++)
System.out.println(" "+a[i]);
}
}







class ssort
{
public static void main(String args[])
{
Sort a=new Sort();
a.read();
a.sort();
}
}



OUTPUT


Enter the limit  :
5
Enter the elements     :
10      20        50        30        40
Elements in ascending order  :
10      20       30        40    50


0 comments:

Post a Comment