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:
Servlet 3 Async Support with Spring MVC and Spring Security
Java Program to Find Path Between Two Nodes in a Graph
Chuyển đổi Array sang ArrayList và ngược lại
Java Program to Implement Knight’s Tour Problem
Spring @RequestParam Annotation
Từ khóa this và super trong Java
Java Program to Implement Threaded Binary Tree
Java – Generate Random String
Notify User of Login From New Device or Location
Java Program to Check if a Matrix is Invertible
Java Program to Solve TSP Using Minimum Spanning Trees
Converting Between Byte Arrays and Hexadecimal Strings in Java
Introduction to Spring Data REST
Rest Web service: Filter và Interceptor với Jersey 2.x (P1)
Spring Boot - Enabling HTTPS
Giới thiệu SOAP UI và thực hiện test Web Service
Spring Cloud Bus
Java Program to Find kth Largest Element in a Sequence
Từ khóa throw và throws trong Java
Java Program to Construct an Expression Tree for an Prefix Expression
Java – Reader to Byte Array
Java Program to Perform Partial Key Search in a K-D Tree
A Quick Guide to Spring Cloud Consul
Filtering a Stream of Optionals in Java
Java Program to Find the Minimum Element of a Rotated Sorted Array using Binary Search approach
Spring Boot - Servlet Filter
Generate Spring Boot REST Client with Swagger
Java Program to Implement Fibonacci Heap
The SpringJUnitConfig and SpringJUnitWebConfig Annotations in Spring 5
Java Program to Check the Connectivity of Graph Using DFS
Testing in Spring Boot
Hướng dẫn Java Design Pattern – Flyweight