In: Computer Science
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;
}
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 + " ");
}
}
}