Programme of how to do bubble sort in ascending order in java
Answers
Answer:
import java.util.*;
public class BUBBLE_SORT
{
public static Scanner in = new Scanner(System.in);
public static void main (String args[])
{
System.out.println("Enter the number of array elements");
int n =in.nextInt();
int array[]=new int[n];
System.out.println("Enter array elements");
for(int i=0;i<n;i++)
array[i]=in.nextInt();
int swap=0;
for(int j=0;j<n-1;j++)
{
for(int k=0;k<n-j-1;k++)
{
if(array[k]>array[k+1])
{
swap=array[k];
array[k]=array[k+1];
array[k+1]=swap;
}
}
}
for(int l=0;l<n;l++)
System.out.print(array[l]+" ");
}
}