Java program to perform Bubble Sort on 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]);
 }
   }
}
Java program to perform Bubble Sort on Strings. Java program to perform Bubble Sort on Strings. Reviewed by on December 20, 2019 Rating: 5

No comments:

Powered by Blogger.