Question

In: Computer Science

Can someone implement the following in Java? Quicksort with switching to Insertion sort when the number...

Can someone implement the following in Java?

Quicksort with switching to Insertion sort when the number of elements in the subarray is less than or equal to 2% of the original number

Requirements:

1) functions from standard libraries implementing Quicksort are NOT allowed;

Solutions

Expert Solution

public class Main
{
//function to sort an array using insertion sort algorithm
public static void insertionSort(int arr[], int size)
{
   int k, j;
   for (int i = 1; i < size; i++)
   {
       k = arr[i];
       j = i - 1;
      
       while (j >= 0 && arr[j] > k)
       {
           arr[j + 1] = arr[j];
           j = j - 1;
       }
       arr[j + 1] = k;
   }
}
  
//method for partition
public static int partition(int a[], int l, int h)
{
int pvt = a[h];
int i = l - 1;
int temp;
  
for (int j = l; j <= h - 1; j++)
{
if (a[j] < pvt)
{
i++;
  
//swapping
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
temp = a[i + 1];
a[i + 1] = a[h];
a[h] = temp;
return i + 1;
}
  
//mthod for quick sort
public static void Quick_Sort(int A[], int l, int r, int size)
{
if (l < r)
{
  
int p = partition(A, l, r);
  
//switching to Insertion sort when the number of elements in the subarray is less than or equal to 2% of the original number
if((p-1-l)<=(.02 * size))
{
insertionSort(A, p-1-l);
}
else
{
Quick_Sort(A, l, p - 1, size);
}

  
if((r-p-1)<=(.02 * size))
{
insertionSort(A, r-p-1);
}
else
{
Quick_Sort(A, p + 1, r, size);
}

}
}
   public static void main(String[] args)
   {
   //array declaration and initialization
int a[] = {20, 55, 11, 64, 97, 99, 74, 85, 34, 35, 74, 11, 47, 65, 69};
  
//method calling
Quick_Sort(a, 0, 14, 14);
  
System.out.println("The array after sorting is: ");
for(int i=0; i<=14; i++)
{
System.out.print(a[i] + " ");
}
      
   }
}

OUTPUT:

The array after sorting is:
11 11 20 34 35 47 55 64 65 69 74 74 85 97 99


Related Solutions

Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed...
Implement Library Sort in Java which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf). Below is the algorithm and pseudo code. Implementation Algorithm Let us say we have an array of n elements. We choose the gap we intend to give. Then we would have a final array of size (1 + ε)n. The algorithm works in...
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
give a good explanation of Bubble sort, Insertion sort, Selection sort, and Quicksort.
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1...
All code in JAVA please 1. Implement Insertion Sort 2. Implement Selection Sort *For problem 1 and 2, please: a. Let the program generate a random array. b. Output both the original random array and the sorted version of it
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which...
out of the following four: 1.Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort a. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is random? b. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is 90% sorted? c. Which sorting methods perform best and worst for data sizes ≥ 25,000 when the input data is reverse sorted? d. Which sorting methods perform best and...
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort...
c++ Run the following sorting algorithms: 1. Bubble sort 2. Insertion sort 3. Quicksort 4. Mergesort Under the following scenarios for input data: 1. Uniform random 2. Almost sorted (90% sorted – 1 in 10 is out of place) 3. Reverse sorted On data of sizes 5,000, 10,000, … in increments of 5,000 up to …, 50,000 -Attach a screenshot of a program compilation below -Attach a screenshot of a successful program run below -Attach a graph (either line graph...
Implement Library Sort which is a version of Insertion Sort with gaps to speed up the...
Implement Library Sort which is a version of Insertion Sort with gaps to speed up the computation. If the pseudocode in Wikipedia (https://en.wikipedia.org/wiki/Library_sort) is not enough, you can download the research paper from (https://arxiv. org/pdf/cs/0407003.pdf).
Implement Quicksort in Java by sorting in parallel after partitioning; for this, the parent thread can...
Implement Quicksort in Java by sorting in parallel after partitioning; for this, the parent thread can continue sorting one segment and a child thread is created for sorting the other segment. However, create a new thread only if both segments contain more than S elements, otherwise sort sequentially both segments. %%writefile Quicksort.java import java.util.Random; YOUR CODE HERE public class Quicksort { static int N; // number of elements to be sorted static int S; // threshold for creating a sub-thread...
Hello i need someone to implement for me RADIX SORT IN JAVA for Characters NOT numbers...
Hello i need someone to implement for me RADIX SORT IN JAVA for Characters NOT numbers and i need the code super fast please
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT