Question

In: Computer Science

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

Solutions

Expert Solution

Quick sort java :

import java.util.Arrays;
import java.util.Random;

public class QuickSort{

public void quickSort(int[] A){
quicksort(A,0,A.length-1);
}
private void quickSort(int[] A,int low,int high){
if(low < high+1){
int p = partition(A,low,high);
quickSort(A,low,p-1);
quickSort(A,p+1,high);
}
}
private void swap(int[] a,int index1,int index2){
int temp = A[index1];
A[index] = A[index2];
A[index2] = temp ;
}
private int getpivot(int low,int high){
   Random rand = new Random();
   return rand.nextInt((high - low) + 1) + low ;
}
private int partition(int[] A,int low,int high){
    swap(A, low,getpivot(low,high));
int border = low +1;
for(int i = border; i<=high;i++);{
if (A[i]<A[low]){
    swap(A,i,border++);
}
}
swap(A,low,border-1);
return border-1;
}
public static void main(string[] args){
Quicksort qs = new Quicksort();
int[] A = {8,2,10,10,8,10,2,8,,8,10,2,8,1,1,8};
system.out.println(Arrays.toString(A));
qs.quickSort(A);
system.out.println(Arrays.toString(A));
}
}


Related Solutions

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)
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.
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?
One way to improve quick sort is to use an insertion sort on lists that have...
One way to improve quick sort is to use an insertion sort on lists that have a small length (call it the “partition limit”). Why does this make sense?
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.
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
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.
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the insertion algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here          ...
I have this program, it sorts a file using shell sort and quick sort then prints...
I have this program, it sorts a file using shell sort and quick sort then prints amount of comparisons and swaps. I need to add the bubble sort algorithm. Here is the code. The language is Java. import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Sort {    public static int numOfComps = 0,numOfSwaps = 0;     public static void main(String[] args)    {         try{        Scanner scanner = new Scanner(new File("a.txt"));//your text file here       ...
Problem 2 For the same list as above, sort the list in such a way where...
Problem 2 For the same list as above, sort the list in such a way where the first set of numbers are ascending even and the second set of numbers are ascending odd. Your output should look like the following: [ 2, 4, 6, … 1, 3, 5 .. ]. The list is A = [ 6, 9, 0, 4, 2, 8, 0, 0, 8, 5, 2, 1, 3, 20, -1 ]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT