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

Compile and Run this code and explain what it is doing. Can someone help break t

ID: 670307 • Letter: C

Question

Compile and Run this code and explain what it is doing. Can someone help break this down and explain what this code is actually doing? Please and Thank You

void quick(int numbers[])
{
System.out.println(" Quick Sorting process");
QuickSort(numbers, 0, 12);
System.out.println(" Quick Sorting");
System.out.print("[");
for(int i1=0; i1<13; i1++)
{
System.out.print(" " + numbers[i1]);
}
System.out.println("]");
}
private static void QuickSort(int[] num, int i, int j)
{
int low = i;
int high = j;
if (low >= j)
{
return;
}
int mid = num[(low + high) / 2];
  
while (low < high) {
while (low < high){
while (low<high && num[low] < mid)
{
low++;
}
while (low<high && num[high] > mid)
{
high--;
}
if(low < high)
{
int T = num[low];
num[low] = num[high];
num[high] = T;
}
}
if (high < low){
int T = high;
high = low;
low = T;
}
System.out.print("[");
for(int i1=0; i1<13; i1++)
{
System.out.print(" " + num[i1]);
}
System.out.println("]");
QuickSort(num, i, low);
QuickSort(num, low ==i ? low+1 : low, j);
}
}

}

Explanation / Answer

This program will not be executed, unless you write a main function. The concept of this program is to sort an array of 13 elements, using QuickSort technique.

In this technique everytime, one element is identified(pivot) and it will be placed in the right position(A position which it will be in a sorted array). We can say that an element is in the right(exact) position, if all the elements before that element are less than, and all the elements after that element are greater than. After placing the pivot element in the exact position, the same logic is applied for the left side list, and also for the right side list.

This process is repeated for all the elements.

I commented each and every line in the code, for you. Hope this will be easy for you to understand.

void quick(int numbers[])
{
System.out.println(" Quick Sorting process"); // Displays a message to screen
QuickSort(numbers, 0, 12);       //Calling the function QuickSort with 3 parameters, first parameter being the name of the array, the second parameter being the lower bound of the array(0), the third parameter being the upper bound of the array(12) which means that is an array of 13 elements.
System.out.println(" Quick Sorting");   //Displays a message to screen
System.out.print("[");                       //Displays a message to screen
for(int i1=0; i1<13; i1++)                   //Loop runs for 13 times, starting from 0 to 12.
{
System.out.print(" " + numbers[i1]);   //Displays all the elements in the array, of course in sorted order.
}
System.out.println("]");                   //Displays a message to screen
}
private static void QuickSort(int[] num, int i, int j)   //This function actually sorts the array.
{
int low = i;                               //Lower bound.
int high = j;                               //Upper bound.
//Please keep in mind that, both the boundary pointers will move towards each other.
if (low >= j)                               //If we have no elements in the array.
{
return;                                   //Do nothing, and return.
}
int mid = num[(low + high) / 2];           //If not, consider the middle of the array value as pivot(mid).
  
while (low < high) {                       //Till both indexes didn't cross each other. As they move towards each other, they will eventually cross.
while (low < high){                       //Till both indexes didn't cross each other.
while (low<high && num[low] < mid)   //Till both the indexes didn't cross or you reach a number greater than pivot.
{
low++;                           //move the low index forward(towards the right).
}
while (low<high && num[high] > mid)   //Till both the indexes didn't cross or you reach a number less than pivot.
{
high--;                           //move the high index backwards(towards the left).
}
if(low < high)                       //if both the indexes didn't cross, (if you still have some more elements in the middle).
{
int T = num[low];               //Exchange the elements num[low] and num[high].
num[low] = num[high];
num[high] = T;
}
}
if (high < low){                       //Once done with the loop, if the indexes cross over, exchange the index values.
int T = high;
high = low;
low = T;
}
System.out.print("[");                   //Display a message to screen.
for(int i1=0; i1<13; i1++)               //Loop runs for 13 times, starting from 0 to 12.
{
System.out.print(" " + num[i1]);   //Displays all the elements in the array.
}
System.out.println("]");               //Display a message to screen.
QuickSort(num, i, low);                   //Call the Quicksort function, for the subarray (after calculating mid, the array will become 2 parts) from first element to updated low(first half).
QuickSort(num, low ==i ? low+1 : low, j); //Call the Quicksort function, for the subarray from low or low+1 to last(second half).
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote