0% found this document useful (0 votes)
16 views

java-bubble sort

Uploaded by

fojimaw327
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

java-bubble sort

Uploaded by

fojimaw327
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import java.util.

Scanner;
public class freshers
{
public static void main(String[] args)
{
int i,j,k;
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of elements to be sorted");
int n=sc.nextInt();
int a[]=new int[n];
System.out.println("enter the elements to be sorted");
for(k=0;k<n;k++)
{
int num=sc.nextInt();
a[k]=num;
}
System.out.println("the elements before sorting are:");
for(int value: a)
{
System.out.println(value+" ");
}
for(i=0;i<n-1;i++)
{
int flag=0;
for(j=0;j<n-1-i;j++)
{
if(a[j]>a[j+1])
{
int temp;
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
flag=1;
}
}
if(flag==0);
break;
}
System.out.println();
System.out.println("the elements after sorting are:");
for(int value: a)
{
System.out.println(value+" ");
}
}
}

You might also like