Question: In Java, using insertion sort algorithm, how do I get the list of desserts to sort alphabetically as shown in the example image? This is

In Java, using insertion sort algorithm, how do I get the list of desserts to sort alphabetically as shown in the example image?
This is what I've managed so far:
package lovelace;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class InsertionSort {
private static int count =0;
void sort(int arr[])
{
int n = arr.length;
for (int i =1; i n; ++i){
int key = arr[i];
int j = i -1;
while (j >=0 && arr[j]> key){
arr[j +1]= arr[j];
j = j -1;
}
arr[j +1]= key;
}
}
/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i =0; i n; ++i)
System.out.print(arr[i]+"");
System.out.println();
}
// Driver method
public static void main(String args[])
{
String fileName =
"/Users/envyi/Documents/GitHub/Lovelace-Cupcakes/lovelacecupcake/src/main/java/lovelace/cupcake_test_10.json";
JSONArray cupcakeArray = JSONFile.readArray(fileName);
String[] cupcakeNameArray = nameArray(cupcakeArray);
System.out.println(cupcakeNameArray);
// print unsorted list
System.out.println("----- Unsorted array -----");
print(cupcakeNameArray);
// print sorted list
System.out.println("----- Sorted array-----");
print(cupcakeNameArray);
// print statistics
System.out.println("----- Statistics -----");
System.out.printf("Size of array =%d
", cupcakeNameArray.length);
System.out.printf("Count =%d
", count);
int arr[]={12,11,13,5,6};
InsertionSort ob = new InsertionSort();
ob.sort(arr);
printArray(arr);
}
// print cupcake array
public static void print(String[] cupcakeNameArray){
System.out.printf("Number\tName
");
System.out.printf("------\t---------------
");
for (int i =0; i cupcakeNameArray.length; i++){
System.out.printf("%04d\t%s
", i, cupcakeNameArray[i]);
}
}
// get array of cupcake names
public static String[] nameArray(JSONArray cupcakeArray){
String[] arr = new String[cupcakeArray.size()];
// get names from json object
for (int i =0; i cupcakeArray.size(); i++){
JSONObject o =(JSONObject) cupcakeArray.get(i);
String name =(String) o.get("name");
arr[i]= name;
}
return arr;
}
}```
Number Name
0000 Tiramisu Bar
0001 Tiramisu Biscuit
0002 Tiramisu Bonbon
0003 Tiramisu Chups
0004 Tiramisu Claw
0005 Tiramisu Donut
0006 Tiramisu Fruitcake
0007 Tiramisu Gummies
0008 Tiramisu Lollipop
0009 Tiramisu Pastry
Statistics -----
Size of array =10
Count =
```
In Java, using insertion sort algorithm, how do I

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!