To perform bubble sort on Strings, we must compare adjacent Strings and, if they are not in the correct order, we must swap those strings, this process must be repeated until we reach the end. As a result, all of the strings will be sorted in ascending order; this sorting method is known as bubble sorting.
Bubble Sort on Strings example
In the following example, we have stored the strings in a String array and are comparing adjacent strings in the array using nested for loops. if they are not in order, we swap them using a temporary string variable temp.
public class JavaExample { public static void main(String []args) { String str[] = { "Sagar", "Sumit", "Hitesh", "Avdhesh", "Bhavesh"}; 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:
Strings in sorted order:
- Avdhesh
- Bhavesh
- Hitesh
- Sagar
- Sumit
In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.In Java, encapsulation is a mechanism for combining variables (data) and methods (code) into a single unit. It is the process of concealing information details and safeguarding the object’s data and behavior. It’s one of the four key concepts in OOP. Because the encapsulating class is simple to test, it’s ideal for unit testing.
perform Bubble Sort on Strings perform Bubble Sort on Strings perform Bubble Sort on Strings perform Bubble Sort on Strings perform Bubble Sort on Strings perform Bubble Sort on Strings perform Bubble Sort on Strings