Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Objective Implement the quicksort algorithm using Java. Description Create a cla

ID: 3758387 • Letter: O

Question

Objective

Implement the quicksort algorithm using Java.

Description

Create a class QuickSorter.java that implements the quicksort algorithm over arrays of Integers (later on we will use generic lists). You may use this file as a template. Your goal is twofold:

Complete the partition method.

Fill in the recursive portion of the quicksort algorithm within the `quicksort' method.

Hint

You may use the following algorithm for your partition method:

Function Partition(array, start, end)

pivotI GetPivotI(start, end)
pivot array[pivotI]
Swap(end, pivotI) // Move pivot to the end
large start
For index from start upto end

If array[index] > pivot

Swap(index, large)
large large + 1

EndIf

EndFor
Swap(large, end)
Return large // Return the index of the pivot

EndFunction

After you have completed the above, you should experiment with the `getPivotIndex' method to see how this affects the time of the algorithm. Here are some ideas to try:

First item of the array (already done for you)

Last item of the array.

Middle element of the array.

Randomized location within the array.

Be careful not to introduce potential ArrayIndexOutOfBoundsExceptions

Finally:

Upload the .java file to the dropbox

From the description “this file” you may use as a template =

public class QuickSorter {

/**

   * Sort an array using the `quicksort' algorithm.

   * @param array The array to sort.

   */

public void sort(Integer[] array) {

    quicksort(array, 0, array.length - 1);

}

/**

   * Quicksort recursive algorithm

   * @param array The array to sort.

   * @param start The start of the sub array that we are sorting

   * @param end The end of the sub array that we are sorting

   */

private void quicksort(Integer[] array, int start, int end) {

    /* Part II: For you to complete */

}

/**

   * Partition the array around a pivot value.

   * @param array The array to partition

   * @param start The position to begin the partitioning from

   * @param end The position to end the partitioning at

   */

public int partition(Integer[] array, int start, int end) {

    /* Part I: For you to complete */

}

/**

   * Find a 'good' pivot value within a sub array.

   * @param array The total array

   * @param start The beginning of the sub array

   * @param end The end of the sub array

   */

private int getPivotIndex(Integer[] array, int start, int end) {

    return start;

}

/**

   * Swap two items within the array.

   * @param array The array in which to swap the elements

   * @param first The index of the first item

   * @param second The index of the second item.

   */

private void swap(Integer[] array, int first, int second) {

    Integer temp = array[first];

    array[first] = array[second];

    array[second] = temp;

}

/**

   * Prints the elements of the array, delimited by spaces.

   */

public void printCollection(Integer[] items) {

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

      System.out.print(items[i] + " ");

    }

    System.out.println();

}

}

Explanation / Answer

CODE :