This is a java program to sort the numbers using the Bubble Sort Technique. The algorithm goes with the name, generally used to sort numbers in the ascending order. The smallest numbers bubbles up at each iteration of the sort. The time complexity of the algorithm is O(n^2).
Here is the source code of the Java Program to Implement Bubble Sort. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.
//This is a java program to sort numbers using bubble sort
import java.util.Random;
public class Bubble_Sort
{
static int[] sort(int[] sequence)
{
// Bubble Sort
for (int i = 0; i < sequence.length; i++)
for (int j = 0; j < sequence.length - 1; j++)
if (sequence[j] > sequence[j + 1])
{
sequence[j] = sequence[j] + sequence[j + 1];
sequence[j + 1] = sequence[j] - sequence[j + 1];
sequence[j] = sequence[j] - sequence[j + 1];
}
return sequence;
}
static void printSequence(int[] sorted_sequence)
{
for (int i = 0; i < sorted_sequence.length; i++)
System.out.print(sorted_sequence[i] + " ");
}
public static void main(String args[])
{
System.out
.println("Sorting of randomly generated numbers using BUBBLE SORT");
Random random = new Random();
int N = 20;
int[] sequence = new int[N];
for (int i = 0; i < N; i++)
sequence[i] = Math.abs(random.nextInt(1000));
System.out.println("\nOriginal Sequence: ");
printSequence(sequence);
System.out.println("\nSorted Sequence: ");
printSequence(sort(sequence));
}
}
Output:
$ javac Binary_Counting_Subsets.java $ java Binary_Counting_Subsets $ javac Bubble_Sort.java $ java Bubble_Sort Sorting of randomly generated numbers using BUBBLE SORT Original Sequence: 307 677 574 88 325 851 676 357 172 932 166 450 60 538 964 987 706 690 919 518 Sorted Sequence: 60 88 166 172 307 325 357 450 518 538 574 676 677 690 706 851 919 932 964 987
Related posts:
Java Program to Implement Find all Forward Edges in a Graph
Spring MVC + Thymeleaf 3.0: New Features
Java Program to Check whether Directed Graph is Connected using DFS
Java Program to Implement the Program Used in grep/egrep/fgrep
The DAO with Spring and Hibernate
Java Program to Implement Best-First Search
Batch Processing with Spring Cloud Data Flow
Java Program to Implement Doubly Linked List
Split a String in Java
Period and Duration in Java
Java Program to Implement the Edmond’s Algorithm for Maximum Cardinality Matching
ETL with Spring Cloud Data Flow
Spring MVC Tutorial
Assert an Exception is Thrown in JUnit 4 and 5
Spring Boot - Scheduling
Marker Interface trong Java
Find the Registered Spring Security Filters
Convert char to String in Java
Java Program to Implement Leftist Heap
Bootstrap a Web Application with Spring 5
Tiêu chuẩn coding trong Java (Coding Standards)
Java Program to Create a Balanced Binary Tree of the Incoming Data
Server-Sent Events in Spring
Adding Shutdown Hooks for JVM Applications
Spring Boot - Runners
Spring Cloud AWS – Messaging Support
Java Program to Print the Kind of Rotation the AVL Tree is Undergoing
Java Program to Repeatedly Search the Same Text (such as Bible by building a Data Structure)
Java Program to Give an Implementation of the Traditional Chinese Postman Problem
Java Program to Check Whether a Directed Graph Contains a Eulerian Path
Truyền giá trị và tham chiếu trong java
Java Program to Implement Hash Tables Chaining with Doubly Linked Lists