Question

In: Computer Science

How to write a quick sort using just one recursive call in Java?

How to write a quick sort using just one recursive call in Java?

Solutions

Expert Solution

Please go through screenshots for better understanding.Any doubts comment below.Thank you.

JAVA CODE:

public class QuickSort
{
   int partition(int arr[], int low, int high)
   {
       int pivot = arr[high]; //taking last element as pivot
       int i = (low-1); // index of smaller element
      
       for (int j=low; j<high; j++)
       {
           // If current element is smaller than the pivot
           if (arr[j] < pivot)
           {
               i++;
               // swap arr[i] and arr[j]
               int temp = arr[i];
               arr[i] = arr[j];
               arr[j] = temp;
           }
       }

       // swap arr[i+1] and arr[high] (or pivot)
       int temp = arr[i+1];
       arr[i+1] = arr[high];
       arr[high] = temp;

       return i+1;
   }

   void sort(int arr[], int low, int high) //Method for sorting array
   {
       if (low < high) // low for starting index and high for ending index
       {
           int pi = partition(arr, low, high);
           // Recursively sort elements before
           // partition and after partition
           sort(arr, low, pi-1);
           sort(arr, pi+1, high);
       }
   }

   static void printArray(int arr[]) //Method for printing array elements
   {
       int n = arr.length;
       for (int i=0; i<n; ++i)
           System.out.print(arr[i]+" ");
       System.out.println();
   }
  
   public static void main(String args[])
   {
       int arr[] = {22,1,13,6,3,11,44}; //Array for sorting
       int n = arr.length; // finding length of the array

       QuickSort ob = new QuickSort(); //Creating object ob for class QuickSort
       ob.sort(arr, 0, n-1); //Passing array to method sort

       System.out.println("sorted array");
       printArray(arr); // Passing array to printArray method
   }
}


Related Solutions

Write a program in Java to sort the given array using merge sort, quick sort, insertion...
Write a program in Java to sort the given array using merge sort, quick sort, insertion sort, selection sort and bubble sort based on the input from the user which sorting technique they wanted to use. Get the array size, array elements from the user, and also display the sorted array along with the name of the sorting technique used.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
write a java merge sort called MERGE-SORT-A(), Using recursive calls and NO INSERTION-SORT() as a sub-procedure.
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will...
implement merge sort,quick sort, and radix sort algorithms in java and test how long it will take to sort with random data sets of users input numbers.
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e.,...
Sorting (quick) Sort the array using quick sort. Write the array content after each pass (i.e., from pass 1 to pass 7). Each pass is defined as the completion of one partition. We always pick the first array element as the pivot for each partition.
8,2,10,10,8,10,2,8,,8,10,2,8,1,1,8 Trace the above using (3 way partitioning) quick sort java
8,2,10,10,8,10,2,8,,8,10,2,8,1,1,8 Trace the above using (3 way partitioning) quick sort java
prove the worst case of quick sort using subsetution, what is T(n) of quick sort and...
prove the worst case of quick sort using subsetution, what is T(n) of quick sort and what is the worst case for it
Write a MIPS program that uses an implementation of quick sort to sort an array of...
Write a MIPS program that uses an implementation of quick sort to sort an array of numbers (Translate the following code into (Mars Assembly language). Quicksort Implementation - C int partition(int arr [] , int left , int right) { int i=left, j=right; int tmp; int pivot = arr[(left + right) / 2]; while (i <= j) { while (arr [ i ] < pivot) i ++; while (arr [ j ] > pivot) j −−; if (i <= j)...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with...
Iterative Linear Search, Recursive Binary Search, and Recursive Selection Sort I need it in Java with comments and I need the input file to be placed with Scanner not BufferedReader Please help I need Class River Class CTRiver and Class Driver Class River describes river’s name and its length in miles. It provides accessor methods (getters) for both variables, toString() method that returns String representation of the river, and method isLong() that returns true if river is above 30 miles...
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm....
Sort 33, 77, 22, 11, 34, 21, 88, 90, 42 using Quick sort. Write the algorithm. show work
Write a recursive implementation of insertion sort. Draw a tree of recursive calls for your prototype...
Write a recursive implementation of insertion sort. Draw a tree of recursive calls for your prototype random dataset and compare its big-O efficiency to that of iterative insertion sort. Run your prototype datasets (best, worst, random) from homework 1 and compare the results to the ones for iterative insertion sort. Which version is better? Make sure to present the results in the table form used in homework 1. Include the data for both iterative and recursive versions.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT