T
- should implement the Comparable interfacepublic class MergeSort<T extends java.lang.Comparable<T>> extends SortAlgorithm<T>
Constructor and Description |
---|
MergeSort(java.util.ArrayList<T> data)
constructor for creating a merge sort instance via passing an ArrayList of data
|
MergeSort(T[] dataArray)
constructor for creating a merge sort instance via passing an array of data
|
Modifier and Type | Method and Description |
---|---|
java.util.ArrayList<T> |
getData() |
private void |
merge(int leftIndex,
int middleIndex,
int rightIndex)
This method is responsible for merging two sorted sub array together
|
void |
setData(java.util.ArrayList<T> data) |
void |
sort(int leftIndex,
int rightIndex)
this method sorts the data array using merge sort algorithm
it doesn't return anything.To access the sorted data, call the getData method.
|
swap, swap
public MergeSort(T[] dataArray)
dataArray
- an array of data of type Tpublic MergeSort(java.util.ArrayList<T> data)
data
- an ArrayList of data of type Tpublic void setData(java.util.ArrayList<T> data)
data
- a new ArrayList that replaces the existing onepublic void sort(int leftIndex, int rightIndex)
leftIndex
- the start of the sub array to sortrightIndex
- the end of the sub array to sortpublic java.util.ArrayList<T> getData()
private void merge(int leftIndex, int middleIndex, int rightIndex)
leftIndex
- the index of the start of the sub arraymiddleIndex
- the index of the middle of the sub arrayrightIndex
- the index of the end of the sub array