Friday, October 25, 2013

BUBBLE SORT ALGORITHM.

/*
TOPIC: BUBBLE SORT ALGORITHM.
SEIT
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 BubbleSort()
{
for(int i=0;i<a.length && flag==true;i++)
{
flag=false;
for(int j=0;j<a.length-1;j++)
{
if(a[j]>a[j+1])
swap(j,j+1);

}
}
System.out.println("AFTER SORTING");
display();
}
void swap(int i,int j)
{ flag=true;
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 Bubblesort
{
public static void main(String args[])throws Exception
{
Sequence s=new Sequence();
s.create();
System.out.println("BEFORE SORTING");
s.display();
s.BubbleSort();
System.out.println("THANK U CODED BY TG");
}
}
/*
OUTPUT:
enter size of array:
8
INSERT THE ELEMENTS INTO THE ARRAY
22
53
43
35
11
93
85
32
BEFORE SORTING
22              53              43              35              11              93              85              32
AFTER SORTING
11              22              32              35              43              53              85              93
THANK U CODED BY TG
*/

No comments:

Post a Comment