# Bubble Sort Java Program with Source Code

Bubble Sort Java Program ;Bubble Sort is the most basic sorting algorithm, which works by exchanging neighboring components in the wrong order repeatedly. First Pass: (5 1 4 2 8) –> (1 5 4 2 8), where the algorithm compares the first two elements and swaps because 5 > 1. (1 5 4 2 8) –> (1 4 5 2 8), where the method swaps because 5 > 4.

We have included downloadable zip file at the end of the program so that you may run the following codes.

## Bubble Sort Java Program : Sample Code

``````//Bubble Sort Java Program : Sample Code

package bubble_sort_java;

import java.util.Scanner;

public class Bubble_Sort_Java {

public static void Sort(int a[]) {
int n = a.length, i, j, p, temp;
for (i = 0; i < n - 1; i++) {

for (j = 0; j < n - i - 1; j++) {
if (a[j + 1] < a[j]) {
temp = a[j + 1];
a[j + 1] = a[j];
a[j] = temp;
}

}

}
}

public static void printarray(int a[]) {
for (int i = 0; i < a.length; i++) {

System.out.print(a[i] + " ");
}

}

public static void main(String[] args) {
int n, res, i;
Scanner s = new Scanner(System.in);
System.out.print("Enter number of elements in the array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter " + n + " elements ");
for (i = 0; i < n; i++) {
a[i] = s.nextInt();
}

System.out.println("elements in array ");
printarray(a);
Sort(a);
System.out.println("\nelements after sorting");
printarray(a);
}

}
``````

## Bubble Sort Java Program : Sample Code Output

``````//Bubble Sort Java Program: Sample Code Output

Enter number of elements in the array:3
Enter 3 elements
6
4
7
elements in array
6 4 7
elements after sorting
4 6 7
``````

## What is the importanceofBubble sort program in java?

What is the importance of Bubble sort program

Bubble sort has the following advantages: It is simple to write and understand. Only a few lines of code are required. Because bubble sort is an in-place sorting algorithm, the data is stored in memory, with low memory overhead.

Because the algorithm is simple to understand and implement, bubble sort is a good way to teach new programmers how to sort data sets.

## What is the use of Java program to bubble sort?

What is the use of program to bubble sort

To sort the array elements, Bubble Sort is one of the easiest sorting techniques in Java. The goal is to go from the first to the last element by comparing nearby components and swapping them if they are not in the correct sequence.

## Bubble Sort Java Program with Source Code : About the Project

Bubble sort is popular in computer graphics because it can detect a very tiny defect (such as a swap of only two elements) in almost-sorted arrays and rectify it with only linear complexity (2n).

The java project below covers all of the necessary and vital elements that can be used for college projects by first-year, second-year, and final-year IT students. The project is entirely written in Java and built in the Netbeans code editor.

## Bubble Sort Java Program with Source Code: Project Details and Technology

To start executing a Bubble Sort Java Program, makes sure that you have  NetBeans IDE or any platform of code editor installed in your computer. You can also watch tutorial in Transport Management System in Java with Source Code | Java Projects with Source Code

## Java program to implement bubble sort: Procedure to run the system

Time needed: 3 minutes.

Java program to implement bubble sort: Procedure to run the system • Step 2: Extract File • Step 3: Run the project • Step 4: Run the project.

Next, right click the project folder and click run. 