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

Java code.I had commented the code, but I think my logic is incorrect. Can you c

ID: 3764376 • Letter: J

Question

Java code.I had commented the code, but I think my logic is incorrect. Can you correct my comments and correct if I missunderstand what is going on in each step of the code?

all comments are with: //

Thanks,

public class QuickSort {
   public static void main(String[] args) {
       int[] x = { 9, 2, 4, 7, 3, 7, 10 }; //array of 7 elements
       System.out.println(Arrays.toString(x)); //print

       int low = 0; //variable low starts with 0
       int high = x.length - 1; // variable high starts at the end of the list (length-1)

       quickSort(x, low, high); //call function quickSort
       System.out.println(Arrays.toString(x)); //print array x
   }

   public static void quickSort(int[] arr, int low, int high) {
       if (arr == null || arr.length == 0) //if array does not exist ,stop
           return;

       if (low >= high) // if the first element is greater than the last element, stop
           return;

       // pick the pivot
       int middle = low + (high - low) / 2; //finding the pivot in the middle of an array
       int pivot = arr[middle]; //store middle element called pivot.

       // make left < pivot and right > pivot
       int i = low, j = high; //instantiate i as first and j as last
       while (i <= j) //while first element is less or equal than last element, do this:
       {
           while (arr[i] < pivot) //while first element is less than pivot
           {
               i++; // keep moving-ascending
           }

           while (arr[j] > pivot) //while last element is greater than pivot
           {
               j--; // keep moving- descending
           }

           if (i <= j) //if first element is less than last element

           {
               int temp = arr[i]; //create a temporary array store i
               arr[i] = arr[j]; //array with all elements
               arr[j] = temp; // exchange high elements into j
               i++; // increment low in an ascending order
               j--; //increment high in an descending order
           }
       }

       // recursively sort two sub parts
       if (low < j) // if first element is less than last element
           quickSort(arr, low, j); // sort the array low into j
       if (high > i) //if last element is greater than first element
           quickSort(arr, i, high); //sort the array i into high
   }
}

Explanation / Answer

import java.util.Arrays;

public class QuickSort {
   public static void main(String[] args) {
       int[] x = { -9, 2, 4, 7, 3, 7, -10 }; // array of 7 elements
       System.out.println(Arrays.toString(x)); // print
       int low = 0; // variable low starts with 0
       int high = x.length - 1; // variable high starts at the end of the list
                                   // (length-1)
       quickSort(x, low, high); // call function quickSort
       System.out.println(Arrays.toString(x)); // print array x
   }

   public static void quickSort(int[] arr, int low, int high) {
       if (arr == null || arr.length == 0) // if array does not exist ,stop
           return;
       if (low >= high) // if the first element is greater than the last
                           // element, stop
           return;
       // pick the pivot
       int middle = low + (high - low) / 2; // finding the pivot in the middle
                                               // of an array
       int pivot = arr[middle]; // store middle element called pivot.
       // make left < pivot and right > pivot
       int i = low, j = high; // instantiate i as first and j as last
       while (i <= j) // while first element is less or equal than last
                       // element, do this:
       {
           while (arr[i] < pivot) // while first element is less than pivot
           {
               i++; // keep moving-ascending
           }
           while (arr[j] > pivot) // while last element is greater than pivot
           {
               j--; // keep moving- descending
           }
           if (i <= j) // if first element is less than last element
           {
               int temp = arr[i]; // create a temporary array store i
               arr[i] = arr[j]; // array with all elements
               arr[j] = temp; // exchange high elements into j
               i++; // increment low in an ascending order
               j--; // increment high in an descending order
           }
       }
       // recursively sort two sub parts
       if (low < j) // if first element is less than last element
           quickSort(arr, low, j); // sort the array low into j
       if (high > i) // if last element is greater than first element
           quickSort(arr, i, high); // sort the array i into high
   }
}

OUTPUT:

[-9, 2, 4, 7, 3, 7, -10]
[-10, -9, 2, 3, 4, 7, 7]

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