Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
498 B
2
Indexable
Never
import java.util.Scanner;
class BubbleSort{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter No. of Elements: ");
int n=sc.nextInt();
int a[]=new int[n];
System.out.print("Enter "+n+" Elements: ");
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
for(int j=1;j<n-i;j++)
{
if(a[j]<a[j-1])
{
int t=a[j];
a[j]=a[j-1];
a[j-1]=t;
}
}
}
System.out.print("Array After Bubble Sort is: ");
for(int i=0;i<n;i++)
System.out.print(" "+a[i]);
}
}
Leave a Comment