Insertion sort string array java

Let’s learn insertion sort string array java.

Insertion sort string array java

Here we are going to learn insertion sort with string array.

To sort array of strings using string array with insertion sort we are creating sortStringArray() method.

Here’s java program.

public class InsertionSortString
{
   public static void main(String[] args)
   {
      String[] arrFruits = {"Orange","Mango","Apple","Pineapple","Banana"};
      String[] strSorted = sortStringArray(arrFruits, arrFruits.length);
      for(int a = 0; a < strSorted.length; a++)
      {
         System.out.println(strSorted[a]);
      }
   }
   public static String[] sortStringArray(String[] strArray, int g)
   {
      String temp = "";
      for(int a = 0; a < g; a++)
      {
         for(int b = a + 1; b < g; b++)
         {
            if(strArray[a].compareToIgnoreCase(strArray[b]) > 0)
            {
               temp = strArray[a];
               strArray[a] = strArray[b];
               strArray[b] = temp;
            }
         }
      }
      return strArray;
   }
}


Output:

Apple
Banana
Mango
Orange
Pineapple


Also read – interface in java