Question

In: Computer Science

Provide two possible ways to go beyond randomized quick sort to enhance the algorithm to a...

Provide two possible ways to go beyond randomized quick sort to enhance the algorithm to a better running time and implement in Java one of the ways you provide (add few comments to explain your code)

Solutions

Expert Solution

QuickSort picks an element as pivot and partitions the given array around the picked pivot.

There are many different versions of quickSort that pick pivot in different ways.

  • Always pick first element as pivot.
  • Always pick last element as pivot
  • Pick a random element as pivot.
  • Pick median as pivot.

==> Algorithm for random pivoting using Lomuto Partitioning

partition(arr[], low, high)
pivot = arr[high]
i = low // place for swapping
for j := low to high – 1 do
if arr[j] <= pivot then
swap arr[i] with arr[j]
i = i + 1
swap arr[i] with arr[high]
return i

partition_r(arr[], low, high)
r = Random Number from low to high
Swap arr[r] and arr[high]
return partition(arr, low, high)

quicksort(arr[], low, high)
if low < high
p = partition_r(arr, low, high)
quicksort(arr, low, p-1)
quicksort(arr, p+1, high)

==> Algorithm for random pivoting using Hoare Partitioning

partition(arr[], low, high)
pivot = arr[lo]
i = low - 1 // Initialize left index
j = high + 1 // Initialize right index

// Find a value in left side greater than pivot
do
i = i + 1
while arr[i] pivot

if i >= j then
return j

swap arr[i] with arr[j]

partition_r(arr[], lo, hi)
r = Random number from low to high
Swap arr[r] and arr[lo]
return partition(arr, low, high)

quicksort(arr[], lo, hi)
if low < high
p = partition_r(arr, low, high)
quicksort(arr, low, p)
quicksort(arr, p+1, high)

Code for random pivoting using Lomuto Partitioning

import java.util.*;
class RandomQsort
{
    // This Function helps in calculating random numbers between low and high
    static void random(int arr[],int low,int high)
    {
        Random rand= new Random();
        int pivot = rand.nextInt(high-low)+low;
        int temp1=arr[pivot];
        arr[pivot]=arr[high];
        arr[high]=temp1;
    }
    // This function takes last element as pivot,
    static int partition(int arr[], int low, int high)
    {
        // pivot is choosen randomly 
        random(arr,low,high);
        int pivot = arr[high];

        int i = (low-1); // index of smaller element 
        for (int j = low; j < high; j++)
        {
            // If current element is smaller than or 
            // equal to 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;
    }
    // The main function that implements QuickSort()
    static void sort(int arr[], int low, int high)
    {
        if (low < high)
        { 
            /* pi is partitioning index, arr[pi] is 
            now at right place */
            int pi = partition(arr, low, high);

            // Recursively sort elements before
            sort(arr, low, pi-1);
            sort(arr, pi+1, high);
        }
    }

    /* A utility function to print array of size n */
    static void printArray(int arr[])
    {
        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 n;
        Scanner s1= new Scanner(System.in);
        System.out.println("Enter value of n which is size of array : ");
        n=s1.nextInt();
        int arr[]=new int[n];
        System.out.println("Enter Array:");
        for(int i=0;i<n;i++)
        {
            arr[i]=s1.nextInt();
        }
        int j = arr.length;
        sort(arr, 0, j-1);
        System.out.println("Sorted array");
        printArray(arr);
    }
} 

output:

Enter value of n which is size of array :
5
Enter Array:
1 9 7 3 10
Sorted array
1 3 7 9 10

output(SS):

This is output of code:


Related Solutions

please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each...
The quick sort algorithm always divides the list into two equal sized sublists, then sorts each sublists, and then combines both sublists.. True of False
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show how long it took and how many movements occurred. Please write codes in C++ Here's data set (should be stored in txt file) 7426 4524 4737 9436 3997 2757 6288 5414 9590 5968 6638 3199 9514 1541 9866 2144 6731 911 2171 6135 6437 912 9417 2662 6606 6349 707 2890 5386 9718 3492 5068 9674 8578 8323...
Name and describe two possible ways a couple may, with or without technical assistance, enhance the...
Name and describe two possible ways a couple may, with or without technical assistance, enhance the possibility of the woman still conceiving with the man`s sperm and her ovum. Write your response in 3–4 paragraphs. Apply APA standards to citation of sources.
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
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement...
a) Write Quick Sort and its function algorithm b) Derive the computation time for each statement in the algorithm. (You must explain your reason for each statement about how you get the answer of each computation time in at one or two sentences each.)
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7...
Create a quick and merge sort algorithm that sorts 6 9 8 12 3 1 7 In java please.
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition()...
(50’) Implement Quick-Sort algorithm in quickSort.cpp, where you are expected to implement three functions, swap(), partition() and quickSort(). You are expected to call swap() within partition(), to call partition() within quickSort(), and you are not expected to declare/ implement other additional functions nor change the main() function. OPTIONAL: If you don’t need/ want to use swap() in your implementation, that is fine. Just delete/ comment it. quickSort.cpp #include <iostream> using namespace std;    // A helper function to facilitate swapping...
The programming language is C. In its simplest algorithm, the bubble sort technique compares each two...
The programming language is C. In its simplest algorithm, the bubble sort technique compares each two adjacent elements of an array of size “N” and exchanges their values if they are out of order. The process of comparing and exchanging is repeated for N array passes. For sorting array elements in ascending order, the smaller values “bubble” to the top of the array (toward element 0), while the larger values sink to the bottom of the array. Assuming that: double...
Companies have two quick ways to raise capital for growth: they can sell stock (Equity) or...
Companies have two quick ways to raise capital for growth: they can sell stock (Equity) or they can borrow (debt, like bonds or mortgages). Each of these methods carries some "pros" and "cons" - what is the advantages and disadvantages of raising capital through borrowing (debt). Explain (in two or three sentences) the advantages and disadvantages, citing at least one advantage and at least one disadvantage, of borrowing. Explain WHY something is an advantage or WHY it is a disadvantage....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT