C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Sort String Bubble Sort Program - Java Programs

Define a class and store the given city names in a single dimensional array. Sort these names in alphabetical order using the bubble sort technique only.

INPUT: Delhi, Bangalore, Agra, Mumbai, Calcutta
OUTPUT: Agra, Bangalore, Calcutta, Delhi, Mumbai

class Bubble{ public static void main(String args[]){ String a[] = {"Delhi", "Bangalore", "Agra", "Mumbai", "Calcutta"}; for(int i = 0; i < a.length; i++){ for(int j = 0; j < a.length - 1 - i; j++){ if(a[j].compareTo(a[j + 1]) > 0){ String temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } System.out.println("Sorted List:"); for(int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); } }

Output

 
 OUTPUT : 
Sorted List:
Agra Bangalore Calcutta Delhi Mumbai