Question
In this program, you will implement an insertion sort on an ArrayList of Strings. However, you use the approach described in the notes. First, write
In this program, you will implement an insertion sort on an ArrayList of Strings. However, you use the approach described in the notes.
First, write the switchValues method
public static void switchValues( ArrayList
Precondition: 0 <= n1 < a.size
0 <= n2 < a.size
Postcondition: The ArrayList is unchanged except that the string at index n1 is now at index n2; and the string that was at index n2 is now at index n1
Second, write the insertAbove method.
public static void insertAbove( ArrayList
Precondition: 1 <= n1 < a.size
The array list is sorted in ascending from in the indices [0, n-1]
Postcondition: The array list is sorted in ascending from in the indices [0, n] and the other elements of the list are not changed.
Third, write the insertionSort method.
public static void insertionSort( ArrayList
Sort the list using the insertion sort algorithm
It should be two lines long and one line should call the insertAbove method
Main.Java
import java.util.ArrayList;
public class Main { public static void main(String[] args) { ArrayList
} public static void insertAbove( ArrayList
} public static void insertionSort( ArrayList
} }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started