Question: help with solving code runs but answer Is no. test code given. I feel like. im missing something package quickSort; public class QuickSort { public

help with solving code runs but answer Is no. test code given. I feel like. im missing something

package quickSort;

public class QuickSort

{

public static void sort (Object arr[])

{

}

private static void sortFirstMiddleLast (Object arr[], int first, int mid,int last)

{

if (((Comparable) arr[first]).compareTo (arr[mid]) > 0)

{

swap (arr, first, mid);

}

if (((Comparable) arr[mid]).compareTo (arr[last]) > 0)

{

swap (arr, mid, last);

}

if (((Comparable) arr[first]).compareTo (arr[mid]) > 0)

{

swap (arr, first, mid);

}

}

private static void swap (Object arr[], int idx1, int idx2)

{

Object temp = arr[idx1];

arr[idx1] = arr[idx2];

arr[idx2] = temp;

}

private static void insertionSort (Object arr[], int first, int last)

{

for (int i = first; i < last; i++)

{

for (int j = i + 1; j > first; j--)

{

int res = ((Comparable) arr[j]).compareTo (arr[j - 1]);

if (res < 0)

{

swap (arr, j, j - 1);

}

else

{

break;

}

}

}

}

}

package quickSort;

public class QuickSortTest {

private static void randomIntArray(Integer arr[], int min, int max) {

int mn = Math.min(min, max);

int mx = Math.max(min, max) + 1;

for(int i = 0; i < arr.length; i++) {

int value = (int)(Math.random() * (mx - mn)) + mn;

arr[i] = Integer.valueOf(value);

}

}

private static void randomStringArray(String arr[], int min, int max) {

if(min < 0) {

throw new IllegalArgumentException("Min for randomStringArray must be >= 0");

}

int mn = Math.min(min, max);

int mx = Math.max(min, max) + 1;

char alphabet[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();

for(int i = 0; i < arr.length; i++) {

String s = "";

int rLen = (int)(Math.random() * (mx - mn)) + mn;

for(int j = 0; j < rLen; j++) {

int rCharIdx = (int)(Math.random() * alphabet.length);

s += alphabet[rCharIdx];

}

arr[i] = s;

}

}

private static boolean isSorted(Object arr[]) {

for(int i = 0; i < arr.length - 1; i++) {

if(((Comparable) arr[i]).compareTo(arr[i+1]) > 0) {

return false;

}

}

return true;

}

public static void main(String args[]) {

Integer iArr[] = new Integer[1000];

randomIntArray(iArr, -100, 100);

QuickSort.sort(iArr);

System.out.println("QUICK SORT INTEGER ARRAY IS SORTED? " + (isSorted(iArr) ? "YES" : "NO"));

String sArr[] = new String[1000];

randomStringArray(sArr, 10, 20);

QuickSort.sort(sArr);

System.out.println("QUICK SORT STRING ARRAY IS SORTED? " + (isSorted(iArr) ? "YES" : "NO"));

}

}

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 Databases Questions!