Need Help ?

Home / Expert Answers / Other / public-class-part3-private-static-int-compares-private-static-int-swaps-private-static-int-array-pub

(Answered): public class Part3 {private static int compares;private static int swaps;private static int[] array; ...



• foo1(int k, int m): This method is meant to ensure that array[k] >= array[2k+1] and array[2k+2], as long as those indices a

public class Part3 {
private static int compares;
private static int swaps;
private static int[] array;

public static void setArray(int[] a) {
   array = a;
}

public static int[] bubbleSort(int[] a) {
   array = a;
   compares = 0;
   swaps = 0;
   //TO BE IMPLEMENTED
   return new int[]{compares, swaps};
}

public static int[] selectionSort(int[] a) {
   array = a;
   compares = 0;
   swaps = 0;
   //TO BE IMPLEMENTED
   return new int[] {compares, swaps};
}

public static int[] insertionSort(int[] a) {
   array = a;
   compares = 0;
   swaps = 0;
   //TO BE IMPLEMENTED
   return new int[]{compares, swaps};
}

public static int[] mysterySort(int[] a) {
   array = a;
   compares = 0;
   swaps = 0;
   //TO BE IMPLEMENTED
   return new int[] {compares, swaps};
}

public static int foo1(int k, int m) {
   //TO BE IMPLEMENTED
}

public static void foo2(int k, int m) {
   //TO BE IMPLEMENTED
}

public static void foo3() {
   //TO BE IMPLEMENTED
}

//returns true if array[i] < array[j]
//false else
//NOTE: You can use !less for >=
private static boolean less(int i, int j) {
   compares++;
   if(array[i] < array[j])
   return true;
return false;
}

//swaps array[i] and array[j]
private static void swap(int i, int j) {
   if(i == j)
   return;
   swaps++;
   int temp = array[i];
   array[i] = array[j];
   array[j] = temp;
}
}

STARTER CODE PROVIDED!!! Need CODE for FOO1,FOO2, FOO3, ANDMYSTERY SORT BASED ON THE FOO METHODS!! ASAP THANK YOU

• foo1(int k, int m): This method is meant to ensure that array[k] >= array[2k+1] and array[2k+2], as long as those indices are less than m. You should return the final index of the element originally at k. It should do the following: o if(2k+1 > m): don't swap; return k o else if (2k+2 > m): compare array[k] to array[2k+1] and if array[k] is smaller, swap the two and return 2k+1 O else if array[k] is smaller than array[2k+1] or array[2k+2], swap it with the larger of the two and return that index (if array[2k+1] == array[2k+2], swap with array[2k+1]) • foo2(int k, int m): In this method, you should make sure that array[k] >= array[2k+1] and array[2k+2]. If not, use fool to adjust. Then call fool again on the new index of array[k]. Do this until array[k] >= array[2k+1] and array[2k+2] OR there's no more array elements to swap with that are before index m. Example: A = [3, 2, 5, 1, 7, 8, 9] k = 0 m = 6 foo2(k, m): foo1(k, m)-->3 swaps with 5: [5, 2, 3, 1, 7, 8, 9); k = 2 foo1(k, m)-->3 swaps with 9: [5, 2, 9, 1, 7, 8, 3]; R = 6 foo1(k, m)-->nothing happens because 2k+1 > m . f003(): call foo2 repeatedly on all the indices in the array from floor(N/2) down to (NOTE: You have to go backwards through the array, but you only have to start at the halfway point.) N is the size of the array. • mysterySort: This method sorts the array using foo1, foo2, and foo3 as follows: m = the length of the array o foo3() while m > 0 swap(O, m-1) o 1 foo2(0, m) o end while


We have an Answer from Expert

View Expert Answer

Expert Answer


Answer to public class Part3 { private static int compares; private static int swaps; private static int[] array; public static vo...

The Problem has Answer!

We have detailed solutions for you for more understanding.

View Answer