ava quick sort create a method to test that your quicksort( int [ ] a ), insertion sort and removeDuplicates( int [ ] methods work sufficiently. the code is below and it needs a method to test it public class QuicksortTester { public static void quicksort(Comparable[] a) { quicksort(a, 0, a.length - 1); } static void quicksort(Comparable[] a, int low, int high) { int CUTOFF = 10; if (low + CUTOFF > high) insertionSort(a, low, high); else { int middle = (low + high) / 2; if (a[middle].compareTo(a[low]) < 0) swapReferences(a, low, middle); if (a[high].compareTo(a[low]) < 0) swapReferences(a, low, high); if (a[high].compareTo(a[middle]) < 0) swapReferences(a, middle, high); swapReferences(a, middle, high - 1); Comparable pivot = a[high - 1]; int i, j; for (i = low, j = high - 1;;) { while (a[++i].compareTo(pivot) < 0) ; while (pivot.compareTo(a[--j]) < 0) ; if (i >= j) break; swapReferences(a, i, j); } swapReferences(a, i, high - 1); quicksort(a, low, i - 1); quicksort(a, i + 1, high); } } public static final void swapReferences(Object[] a, int index1, int index2) { Object tmp = a[index1]; a[index1] = a[index2]; a[index2] = tmp; } private static void insertionSort(Comparable[] a, int low, int high) { for (int p = low + 1; p <= high; p++) { Comparable tmp = a[p]; int j; for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--) a[j] = a[j - 1]; a[j] = tmp; } } static int removeDuplicates(int arr[], int n) { // Return, if array is empty // or contains a single element if (n==0 || n==1) return n; int[] temp = new int[n]; // Start traversing elements int j = 0; for (int i=0; i
java quick sort
create a method to test that your quicksort( int [ ] a ), insertion sort and removeDuplicates( int [ ] methods work sufficiently.
the code is below and it needs a method to test it
public class QuicksortTester {
public static void quicksort(Comparable[] a) {
quicksort(a, 0, a.length - 1);
}
static void quicksort(Comparable[] a, int low, int high) {
int CUTOFF = 10;
if (low + CUTOFF > high)
insertionSort(a, low, high);
else {
int middle = (low + high) / 2;
if (a[middle].compareTo(a[low]) < 0)
swapReferences(a, low, middle);
if (a[high].compareTo(a[low]) < 0)
swapReferences(a, low, high);
if (a[high].compareTo(a[middle]) < 0)
swapReferences(a, middle, high);
swapReferences(a, middle, high - 1);
Comparable pivot = a[high - 1];
int i, j;
for (i = low, j = high - 1;;) {
while (a[++i].compareTo(pivot) < 0)
;
while (pivot.compareTo(a[--j]) < 0)
;
if (i >= j)
break;
swapReferences(a, i, j);
}
swapReferences(a, i, high - 1);
quicksort(a, low, i - 1);
quicksort(a, i + 1, high);
}
}
public static final void swapReferences(Object[] a, int index1, int index2) {
Object tmp = a[index1];
a[index1] = a[index2];
a[index2] = tmp;
}
private static void insertionSort(Comparable[] a, int low, int high) {
for (int p = low + 1; p <= high; p++) {
Comparable tmp = a[p];
int j;
for (j = p; j > low && tmp.compareTo(a[j - 1]) < 0; j--)
a[j] = a[j - 1];
a[j] = tmp;
}
}
static int removeDuplicates(int arr[], int n)
{
// Return, if array is empty
// or contains a single element
if (n==0 || n==1)
return n;
int[] temp = new int[n];
// Start traversing elements
int j = 0;
for (int i=0; i<n-1; i++)
// If current element is not equal
// to next element then store that
// current element
if (arr[i] != arr[i+1])
temp[j++] = arr[i];
// Store the last element as whether
// it is unique or repeated, it hasn't
// stored previously
temp[j++] = arr[n-1];
// Modify original array
for (int i=0; i<j; i++)
arr[i] = temp[i];
return j;
}
}
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 3 steps with 1 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)