Friday, May 3, 2019

Write a JAVA program to sort for an element in a given list of elements using bubble sort?

program:

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
class BubbleSort
{
public static void main(String a[])throws IOException
{
int arr[]=new int[100];
int n,k,i,j,temp;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter the size of an array:");
n=Integer.parseInt(br.readLine());
System.out.println("Enter"+n+"number of elements:");
for(k=0;k<n;k++)
{
arr[k]=Integer.parseInt(br.readLine());
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("Sorted array is:");
for(k=0;k<n;k++)
{
System.out.println(arr[k]);
}
}

}

output:-



1 comment: