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.
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...
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...
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.
Can someone fix solver class. it suppose to call method from sort class for insertion and...
Can someone fix solver class. it suppose to call method from sort class for insertion and heap sort. However its not giving the outuput.. Run following class Solver where 20 array instances of 1M random integers are sorted using Insertion- sort and Heap-sort, respectively public class Solver {    public static void main(String[] args) { final int SIZE = 1000000; // 1 million final int Instances=20; int[][] TwoDimArray = new int[Instances][SIZE];    Sort s = new Sort();    for(int i=0;...
Hello i need someone to implement in JAVA a radix sort algorithm forsorting strings in ascending...
Hello i need someone to implement in JAVA a radix sort algorithm forsorting strings in ascending order. The input to your algorithm should be a (multi)set S = [S1, S2, . . . , Sn] of strings each of which is of length m over the English alphabet [A…Z, a…z]. The output should be the set sorted in ascending lexicographical order. Number of strings is >= 100 and number of characters >= 50 i need the answer fast please
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C...
The purpose here is to implement the QuickSort sorting algorithm to sort integers. Write a C program which accepts 1 command-line argument: the name of a text file which contains integers, one-per line. Your C program must be named project3. Your C program needs to implement the QuickSort algorithm to sort the integers read from the file specified on the command-line. Your QuickSort implementation must utilize the median-of-three algorithm for choosing a pivot, and BubbleSort any sub arrays with less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT