
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Bubble sort in Java.
Following is the required program.
Example
public class Tester { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i = 0; i < n; i++){ for(int j = 1; j < (n-i); j++){ if(arr[j-1] > arr[j]){ //swap elements temp = arr[j-1]; arr[j-1] = arr[j]; arr[j] = temp; } } } } public static void main(String[] args) { int arr[] = {21,60,32,01,41,34,5}; System.out.println("Before Bubble Sort"); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i] + " "); } System.out.println(); bubbleSort(arr); System.out.println("After Bubble Sort"); for(int i = 0; i < arr.length; i++){ System.out.print(arr[i] + " "); } } }
Output
Before Bubble Sort 21 60 32 1 41 34 5 After Bubble Sort 1 5 21 32 34 41 60
- Related Questions & Answers
- Bubble Sort
- Java program to implement bubble sort
- Java Program for Recursive Bubble Sort
- Perform Bubble Sort on strings in Java
- Bubble Sort program in C#
- Bubble Sort in Go Lang
- Difference Between Bubble Sort and Selection Sort
- 8085 program for bubble sort
- Python Program for Bubble Sort
- C++ Program to Implement Bubble Sort
- C++ Program for Recursive Bubble Sort?
- Code to implement bubble sort - JavaScript
- C Program for Recursive Bubble Sort
- Sorting arrays using bubble sort in JavaScript
- C++ Program for the Recursive Bubble Sort?
Advertisements