how to bubblesort a string array in java

Solutions on MaxInterview for how to bubblesort a string array in java by the best coders in the world

showing results for - "how to bubblesort a string array in java"
Christina
05 Oct 2018
1public class BubbleSort {
2
3	
4	static int MAX = 100;
5	
6	public static void sortStrings (String[] arr, int n) {
7		
8		String temp;
9		
10		//Sorting strings using bubble sort
11		for (int j = 0; j < n; j++) {
12			for (int i = j + 1; i < n; i++) {
13				if (arr[j].compareTo(arr[i]) > 0) {
14					temp = arr[j];
15					arr[j] = arr[i];
16					arr[i] = temp;
17				}
18			}
19		}
20	}
21	
22	public static void main(String[] args) {
23		
24		String[] arr = { "right","subdued","trick","crayon","punishment","silk","describe","royal","prevent","slope" };
25		
26		int n = arr.length;
27		sortStrings(arr, n);
28		System.out.println("Strings in sorted are: ");
29		for (int i = 0; i < n; i++)
30			System.out.println("String " + (i + 1) + " is " + arr[i]);
31	}
32
33
34}