II B.Tech II Sem CSE Java Lab Exercise - 2 b(Operations, Expressions, Control-flow, Strings)

b) Bubble sort

Aim: To write a JAVA program to sort for an element in a given list of elements using bubble sort

Description:


Program:

import java.util.Scanner; 
class bubbledemo
{
public static void main(String args[]) 
{
int n, i,j, temp;
int a[ ]=new int[20];
Scanner s = new Scanner(System.in); 
System.out.println("Enter total number of elements:"); 
n = s.nextInt();
System.out.println("Enter elements:");
for (i = 0; i < n; i++)
a[i] = s.nextInt();
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++) 
{
if(a[j]>a[j+1]) 
{
temp=a[j]; 
                                        a[j]=a[j+1]; 
                                        a[j+1]=temp;

}
}
System.out.println("The sorted elements are:"); 
for(i=0;i<n;i++)
System.out.print("\t"+a[i]);
}
}

Output:



No comments

JavaFX Scene Builder

  This is an article about the JavaFX Scene Builder. You will get a short introduction about the installation and usage of the software. The...