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 Nth Root Algorithm
Spring Boot - Thymeleaf
Show Hibernate/JPA SQL Statements from Spring Boot
Remove HTML tags from a file to extract only the TEXT
Java Program to Implement Hash Trie
Introduction to Eclipse Collections
Java Program to Find the Edge Connectivity of a Graph
JPA/Hibernate Persistence Context
Java Program to Implement Network Flow Problem
Java Program to Generate a Random UnDirected Graph for a Given Number of Edges
Java Program to Implement Disjoint Set Data Structure
Introduction to Java Serialization
Tìm hiểu cơ chế Lazy Evaluation của Stream trong Java 8
Giới thiệu Swagger – Công cụ document cho RESTfull APIs
The Spring @Controller and @RestController Annotations
Java Program to Generate Random Partition out of a Given Set of Numbers or Characters
CharSequence vs. String in Java
Spring Security OAuth Login with WebFlux
A Guide to the Java LinkedList
Working With Maps Using Streams
Java Program to Implement Hash Tables with Linear Probing
Logout in an OAuth Secured Application
Guide to Java Instrumentation
Java Program to Implement Singly Linked List
Spring Boot - Building RESTful Web Services
Guide to Spring Cloud Kubernetes
Examine the internal DNS cache
Java Program to Generate All Pairs of Subsets Whose Union Make the Set
Java Program to Implement LinkedHashSet API
Spring Cloud AWS – S3
How to Define a Spring Boot Filter?
Create a Custom Auto-Configuration with Spring Boot