Question

In: Computer Science

In JAVA Directions: You must trace through the code and determine the status of the array....

In JAVA
Directions: You must trace through the code and determine the status of the array. The code can be
found on the second page of this packet.
Assume Array A =
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Value 33 12 39 6 -2 30 15 11 55 100 40 39 1
How many elements does A have? ______________
What is the start index? _______________ (assume this value is stored in a variable called start)
What is the end index? ______________ (assume this value is stored in a variable called scan)
Keeping this figure in mind you have to represent the array this way:
What you have to do:
Pictorially represents what happens if the call doQuickSort(A, start, scan) happens.
Make sure you identify pivotPoint, pivotValue, endOfLeftList for each instance of recursion. You should
represent the array in each instance of recursion as the figure above. Make sure you identify the value
of pivotValue for each call to the partition method.

Here is the doQuickSort method:
Here is the doQuickSort method:
private static void doQuickSort(int array[], int start, int end)
{
int pivotPoint;
if (start < end)
{
// Get the pivot point.
pivotPoint = partition(array, start, end);
// Sort the first sub list.
doQuickSort(array, start, pivotPoint - 1);
// Sort the second sub list.
doQuickSort(array, pivotPoint + 1, end);
}
}
Partition method:
int partition(int A[ ], int start, int end)
{
int pivotValue = A[start];
endOfLeftList = start;
// At this point A[endOfLeftList] == pivotValue
for (int scan = start + 1; scan <= end; scan ++)
{
if (A[scan] < pivotValue)
{
endOfLeftList ++;
swap(A, endOfLeftList, scan);
// At this point A[endOfLeftList] < pivotValue
}
}
// Move the pivotValue between the left and right sublists
swap(A, start, endOfLeftList);
return endOfLeftList;
}

Solutions

Expert Solution

PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
Value 33 12 39 6 -2 30 15 11 55 100 40 39 1
How many elements does A have? ______________ 13
What is the start index? _______________0 (assume this value is stored in a variable called start)
What is the end index? ______________12 (assume this value is stored in a variable called scan)
Keeping this figure in mind you have to represent the array this way:
What you have to do:
Pictorially represents what happens if the call doQuickSort(A, start, scan) happens.

class A {

  private static void doQuickSort(int array[], int start, int end) {
    int pivotPoint;
    if (start < end) {
      // Get the pivot point.
      pivotPoint = partition(array, start, end);
      // Sort the first sub list.
      doQuickSort(array, start, pivotPoint - 1);
      // Sort the second sub list.
      doQuickSort(array, pivotPoint + 1, end);
    }
  }

  static void swap(int arr[], int a, int b) {
    int temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
  }

  static int partition(int A[], int start, int end) {
    int pivotValue = A[start];
    int endOfLeftList = start;
    // At this point A[endOfLeftList] == pivotValue
    for (int scan = start + 1; scan <= end; scan++) {
      if (A[scan] < pivotValue) {
        endOfLeftList++;
        swap(A, endOfLeftList, scan);
        // At this point A[endOfLeftList] < pivotValue
      }
    }
    // Move the pivotValue between the left and right sublists
    swap(A, start, endOfLeftList);
    return endOfLeftList;
  }

  public static void main(String[] args) {
    int arr[] = { 33, 12, 39, 6, -2, 30, 15, 11, 55, 100, 40, 39, 1 };
    doQuickSort(arr, 0, 12);
    for (int element : arr) {
      System.out.print(element + " ");
    }
  }
}

Related Solutions

In Java Script how do you code an array with the values and suits of cards...
In Java Script how do you code an array with the values and suits of cards (all 52 cards in a deck) 2 different array one that just has the face values of the card, and the other with the suit value. Then have it randomly give you 1-2 cards from the deck without repeating? (Example: Dealer gives player two cards. Player asks for a hit and the dealer gives a different card, no repeats). Game is Blackjack.
few problems example of array and 2d array and the solution code in java language. I...
few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.
In java please A boolean must be used for this problem! Directions: * Initialize a Boolean...
In java please A boolean must be used for this problem! Directions: * Initialize a Boolean variable "first" to true. * Repeat (another value has been read successfully) *        If first is true *            Set the minimum to the value. *            Set first to false. *        Else if the value is less than the minimum *            Set the minimum to the value. * Display the minimum. So...
In java run through eclipse /**    *    * Given a square array, determines if...
In java run through eclipse /**    *    * Given a square array, determines if it is diagonal    * That is, returns true if all values in the array are 0    * except the main diagonal. The main diagonal is entries of the form    * data[i][j] where i == j. Elements on the main    * diagonal can be 0 or any other number.    *    * Examples:    * {{1,0,0},    * {0,2,0}   ...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then...
*****IN JAVA***** Write a code snippet that initializes an array with ten random integers and then prints the following output: a. every element (on a single line) b. every element at an even index (on a single line) c. every even element (on a single line) d. all elements in reverse order (on a single line) e. only the first and last elements (on a single line)
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
Java Code The producer and consumer will share an integer array with a length of 5...
Java Code The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer. The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
Write a java code segment to declare an array of size 10 of type String and...
Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT