BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java program to perform Bubble Sort on Strings

Last Updated: April 10, 2019 by Chaitanya Singh | Filed Under: Java Examples

To perform bubble sort on Strings we need to compare adjacent Strings and if they are not in the order then we need to swap those strings, this process needs to be done until we reach at the end. This way all the strings will be sorted in an ascending order, this process of sorting is known as bubble sorting.

Bubble Sort on Strings example

In the following example we have stored the strings in a String array and we are using nested for loops to compare adjacent strings in the array, if they are not in order we are swapping them using a temporary string variable temp.

Here we are using compareTo() method to compare the adjacent Strings.

public class JavaExample {
   public static void main(String []args) {
	String str[] = { "Ajeet", "Steve", "Rick", "Becky", "Mohan"};
	String temp;
	System.out.println("Strings in sorted order:");
	for (int j = 0; j < str.length; j++) {
   	   for (int i = j + 1; i < str.length; i++) {
		// comparing adjacent strings
		if (str[i].compareTo(str[j]) < 0) {
			temp = str[j];
			str[j] = str[i];
			str[i] = temp;
		}
	   }
	   System.out.println(str[j]);
	}
   }
}

Output:
Java bubble sort on strings example

Related Java examples

1. Java program to sort an array
2. Java program to sort Strings in an Alphabetical order
3. Java program to reverse words in a String
4. Java program to find duplicate characters in a String

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to Print alternate Prime numbers
  3. Java program to print number of elements in an array
  4. Java program to convert decimal to binary
  5. Java Program to Sort Strings in an Alphabetical Order

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap