applying insertion sort on string in java

Solutions on MaxInterview for applying insertion sort on string in java by the best coders in the world

showing results for - "applying insertion sort on string in java"
Marlene
28 Jan 2019
1public static void insertion(String[] ans){			//Main Methord
2		for (int i = 0; i < ans.length-1; i++) {
3            for (int j = i + 1; j > 0; j--) {
4                if (ans[j].compareTo(ans[j - 1]) < 0) {
5                    String temp = ans[j];
6                    ans[j] = ans[j - 1];
7                    ans[j - 1] = temp;
8                }
9            }
10        }
11  }
12public static void main(String[] args){		//driver code
13  	String[] ans = {"xa","ax"};
14	insertion(ans);
15}