Question: Please help me find solution to this method in JAVA: I need to implement three sorts in Sorts.java: 1) Insertion Sort : public static void
Please help me find solution to this method in JAVA:
I need to implement three sorts in Sorts.java:
1) Insertion Sort :
public static void insertionSort(ArrayListlist, int startIndex, int endIndex)
2) Merge Sort:
public static void mergeSort(ArrayList
3) Quick Sort:
public static void quickSort(ArrayListlist, int startIndex, int endIndex)
Given Pair Class:
/**
* This class defines a pair where the first element is a name and second element is the
* number of occurrence of this name.
*/
public class Pair{
String name;
int count;
/**
* The constructor that creates a pair of the given name with number of occurrence as 1.
*
* @param name the given name
*/
public Pair(String name){
this.name = name;
this.count = 1;
}
/**
* The constructor that creates a pair of the given name with the given number of occurrence.
*
* @param name the given name
* @param count the given number of occurrence
*/
public Pair(String name, int count){
this.name = name;
this.count = count;
}
/**
* Getter for the name in this pair
*
* @return the name in this pair
*/
public String getName(){
return name;
}
/**
* Getter for the count in this pair
* @return the number of occurrence of the name in this pair
*
*/
public int getCount(){
return count;
}
/**
* Increase the number of occurrence of name in this pair by 1.
*/
public void incrementCount(){
this.count++;
}
}
You should sort the given ArrayList of pair based on the count variable in each pair, such that when the sorting is finished, the pairs in the givenArrayList should be in increasing order of count for each pair.
Notice that you should sort the given ArrayList from startingIndex(inclusive) to endIndex(exclusive). For example, if the given ArrayList is {7,27,6,25,8,81}, startIndex is 1 and endIndex is 4, then you should only sort the bold part of the ArrayList{7,26,6,25,8,81} and the result should be {7,6,25,27,8,81}.
Note: To sort the whole list, startIndex should be 0 and endIndex should be list.size().
Example:
ArrayList toSort = new ArrayList();
toSort = {(hi, 4), (bye, 6), (cry, 1), (shy, 2)} //Bolded shows what part of ArrayList to be sorted.
Sorted After = ({hi, 4), (cry, 1), (shy, 2), (bye, 6)}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
