Friday, October 25, 2013

SELECTION SORT ALGORITHM IN JAVA.

/*
TOPIC: SELECTION SORT ALGORITHM.
DATE:19/9/2012
*/
import java.io.*;
class Sequence
{
BufferedReader br;
int a[];
int size,temp;
boolean flag;

Sequence()
{
br = new BufferedReader ( new InputStreamReader(System.in));
flag=true;
}
void create()throws IOException
{
System.out.println("enter size of array: ");
size=Integer.parseInt(br.readLine());
a=new int[size];
insert();
}
void insert()throws IOException
{
System.out.println("INSERT THE ELEMENTS INTO THE ARRAY");
for(int i=0;i<size;i++)
a[i]=Integer.parseInt(br.readLine());
}
void SelectionSort()
{
for(int i=0;i<a.length-1;i++)
{

for(int j=i+1;j<a.length;j++)
{
if(a[j]<a[i])
swap(j,i);

}
}
System.out.println("AFTER SORTING");
display();
}
void swap(int i,int j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
void display()
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
}
class SelectionSort
{
public static void main(String args[])throws Exception
{
Sequence s=new Sequence();
s.create();
System.out.println("BEFORE SORTING");
s.display();
s.SelectionSort();

System.out.println("THANK U CODED BY TG");
}
}
/*
OUTPUT:
enter size of array:
9
INSERT THE ELEMENTS INTO THE ARRAY
402
54
669
221
74
921
36
55
1
BEFORE SORTING
402             54              669             221             74              921             36              55              1
AFTER SORTING
1               36              54              55              74              221             402             669             921
THANK U CODED BY TG
*/

No comments:

Post a Comment