Answers for "insertion sort java for a array of strings"

1

applying insertion sort on string in java

public static void insertion(String[] ans){			//Main Methord
		for (int i = 0; i < ans.length-1; i++) {
            for (int j = i + 1; j > 0; j--) {
                if (ans[j].compareTo(ans[j - 1]) < 0) {
                    String temp = ans[j];
                    ans[j] = ans[j - 1];
                    ans[j - 1] = temp;
                }
            }
        }
  }
public static void main(String[] args){		//driver code
  	String[] ans = {"xa","ax"};
	insertion(ans);
}
Posted by: Guest on April-07-2021

Code answers related to "insertion sort java for a array of strings"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language