Write a Java program to sort the elements of an array using bubble sorting technique.

Write a Java program to sort the elements of an array using bubble sort.


Program:

import java.util.*;
public class Array_Sort
{
public static void main(String args[])
{
int n, i, j, temp;
int a[] = new int[50];
Scanner sc = new Scanner(System.in);

System.out.println("Enter  Number of Elements u want: ");
n = sc.nextInt();

System.out.println("Enter the Elements of array: ");
for(i=0; i<n; i++)
{
a[i] = sc.nextInt();
}

// Bubble Sort

for(i=0; i<(n-1); i++)
{
for(j=0; j<(n-i-1); j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
System.out.println("Array Sorted Successfully..!!\n");
System.out.print("Sorted Elements in Ascending Order : \n");
for(i=0; i<n; i++)
{
System.out.println(a[i]);
}
}
}


Output :
Enter  Number of Elements u want: 
5
Enter the Elements of array: 
10
30
40
50
20
Array Sorted Successfully..!!

Sorted Elements in Ascending Order : 
10
20
30
40
50

Comments

Popular posts from this blog

Control Statements:Selection statement ,Iteration statement and Jump statement in Java

Applets - Inheritance hierarchy for applets, differences between applets and applications, life cycle of an applet, passing parameters to applets, applet security issues.

Java Database Connectivity (JDBC) ,Steps to connect to the database in Java