Question

In: Computer Science

Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort....

Use the median of 3 partitioning algorithm (given in the next page) to implement quick sort. This algorithm chooses the pivot element as the median of the 3 elements namely: A[p], A[r], and A[(p+r)/2].(Java language)

Quicksort(A,p,r)

1 if p

2 N = r- p +1

3 m = Median3(A, p, p + N/2 , r)

4 exchange A[m] with A[r]

5 q = Partition (A,p,r)

6 Quicksort(A,p, q-1)

7 Quicksort(A,q+1,r)

Solutions

Expert Solution

The java code for quick sort is provided below.

Java Code:

import java.util.Scanner;

public class QuicksortMedian {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Enter the size of array: ");
int n=sc.nextInt();
int[] arr=new int[n];
System.out.println("Enter the elements");
// Read elements
for(int i=0;i<n;i++)
{
arr[i]=sc.nextInt();
}
// Print array
System.out.println("Unsorted array");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
System.out.println();
// Sort the array
quicksort(arr,0,arr.length-1);
// Print sorted array
System.out.println("Sorted array");
for(int i=0;i<n;i++)
{
System.out.print(arr[i]+" ");
}
}

private static void quicksort(int[] A, int p, int r) {
int N,q;
if(p<r)
{
N=r-p+1;
// Finding median
int m=Median3(A, p, p + N/2 , r);
// Exchange median with the last element
swap(A,m,r);
q=Partition(A,p,r);
quicksort(A,p, q-1);
quicksort(A,q+1,r);
}
}

private static int Median3(int[] A, int p, int cent, int r) {
if ((A[p] > A[cent])&&(A[p]>A[r]))
{
if(A[cent]>A[r])
return cent;
else
return r;
}
else if((A[cent] > A[p])&&(A[cent]>A[r]))
{
if(A[p]>A[r])
return p;
else
return r;
}
else
{
if(A[cent]>A[p])
return cent;
else
return p;
  
}

}

private static void swap(int[] A,int l ,int r) {
int te = A[l];
A[l] = A[r];
A[r] = te;
}

// Partitioning array based on pivot
private static int Partition(int[] A, int p, int r) {
int piv = A[r];
int i = (p-1);
for (int j=p; j<r; j++)
{
if (A[j] < piv)
{
i++;
int te = A[i];
A[i] = A[j];
A[j] = te;
}
}
int te = A[i+1];
A[i+1] = A[r];
A[r] = te;
  
return i+1;
}
  
}

Output:


Related Solutions

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
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
(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...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1,...
Sort the given keys using Counting sort algorithm. Also write the algorithm. 5, 2, 3, 1, 0, 2, 1, 5, 0  
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.
please write a C program that implements Quick Sort algorithm.
please write a C program that implements Quick Sort algorithm.
●In this task, the quick sort algorithm selects the first element in the list as the...
●In this task, the quick sort algorithm selects the first element in the list as the pivot. Revise it by selecting the median among the first, middle, and last elements in the list. ● Write the algorithm for searching for entries using linear probing. ● Write the algorithm for removing entries using linear probing. ● Create a diagram similar to the one above that shows the hash table of size 11 after entries with the keys 34, 29, 53, 44,...
IN JAVA Implement Quicksort with ‘median-of-three’ partitioning You should be able to test a 1000, 10000...
IN JAVA Implement Quicksort with ‘median-of-three’ partitioning You should be able to test a 1000, 10000 and 10000 length array with inters spanning from 1 to 10,000. You cannot use functions from standard libraries implementing Quicksort.
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input...
Implement Heap Sort and Quick Sort in different programs with the following specifications: 1. The input to the programs should be ASCII characters 2. Your program should be able to handle upper and lower case letters both 3. The sort should be done in a descending manner 4.Note: Please use array-based representation for these sorting algorithms Please write comment n explain each step clearly as well your program should show what you are taking as input array and what your...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain...
4) Implement the Selection Sort algorithm discussed in class to sort a list of a certain size. The list is to be implemented using a dynamic array as well as a singly linked list. You are required to compare the performance (sorting time) for the dynamic array and singly-linked list-based implementations. You are given the startup codes for the dynamic array and singly-linked list based implementations. You are required to implement the Selection Sort function in each of these codes....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT