Question

In: Computer Science

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.

Solutions

Expert Solution

Merge Sort

/*In the merge sort we divide the array into two parts recursively,until we get a pair of two and then sort them
and join subarrays again by sorting them*/

/*In the merge sort we divide the array into two parts recursively,until we get a pair of two and then sort them
and join subarrays again by sorting them*/
class Main
{
void combine(int a[], int l, int m, int r) // Combine two arrays
{
int e = m - l + 1;
int f = r - m;
int Left[] = new int [e]; //Left array
int Right[] = new int [f];
for (int i=0; i<e; ++i)
Left[i] = a[l + i];
for (int j=0; j<f; ++j)
Right[j] = a[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < e && j < f)
{
if (Left[i] <= Right[j])
{
a[k] = Left[i];
i++;
}
else
{
a[k] = Right[j];
j++;
}
k++;
}
while (i < e)
{
a[k] = Left[i];
i++;
k++;
}
while (j < f)
{
a[k] = Right[j];
j++;
k++;
}
}
void applysort(int a[], int l, int r) // Divide the array into two parts
{
if (l < r)
{
int m = (l+r)/2;
applysort(a, l, m); //divide from 0 to half
applysort(a , m+1, r); //half to end
combine(a, l, m, r); //join both by sorting
}
}
static void display(int a[]) //display the array
{
int n = a.length;
for (int i=0; i<n; ++i)
System.out.print(a[i] + " ");
System.out.println();
}
public static void main(String args[])
{
int a[] = {9,5,4,5,8,10};

display(a);
  
Main ob = new Main();
ob.applysort(a, 0, a.length-1);
display(a);
}
}

Time Complexity = o(nlogn)

Quick Sort

/* In quick sort we select the pivot element from beginning and then start comparing it with the array from end, if we find the element shorter than pivot element then we replace them, then we start comparing the pivot element from start and if we get element greater than pivot element we replace them, this process is repeated until the pivot element is placed at desired position.*/

class Main
{
int divide(int array[], int start, int end)
{
int ele = array[end];
int i = (start-1);
for (int j=start; j<end; j++)
{
if (array[j] < ele)
{
i++;
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
int temp = array[i+1];
array[i+1] = array[end];
array[end] = temp;
  
return i+1;
}
void doMaining(int array[], int start, int end)
{
if (start < end) {
int pi = divide(array, start, end);
doMaining(array, start, pi-1);
doMaining(array, pi+1, end);
}
}
  
static void display(int array[])
{
int n = array.length;
for (int i=0; i<n; ++i)
System.out.print(array[i]+" ");
System.out.println();
}
  
public static void main(String args[])
{
int array[] = {5,1,9,5,3,10};
int n = array.length;
  
Main ob = new Main();
ob.doMaining(array, 0, n-1);
  
display(array);
}
}

Average complexity - O(nlogn)

worst complexity - O(n^2)

Radix sort - The radix dort sort the element by digits that is by least significant digits

import java.io.*;
import java.util.*;
  
class Main {
static int FindMax(int array[], int n)
{
int temp = array[0];
for (int i = 1; i < n; i++)
if (array[i] > temp)
temp = array[i];
return temp;
}
  
static void findcount(int array[], int n, int s)
{
int result[] = new int[n];
int i;
int c[] = new int[10];
Arrays.fill(c,0);
  
for (i = 0; i < n; i++)
c[ (array[i]/s)%10 ]++;
for (i = 1; i < 10; i++)
c[i] += c[i - 1];
  
for (i = n - 1; i >= 0; i--)
{
result[c[ (array[i]/s)%10 ] - 1] = array[i];
c[ (array[i]/s)%10 ]--;
}
for (i = 0; i < n; i++)
array[i] = result[i];
}
  
static void dosort(int array[], int n)
{
int m = FindMax(array, n);
for (int s = 1; m/s > 0; s *= 10)
findcount(array, n, s);
}
  
static void display(int array[], int n)
{
for (int i=0; i<n; i++)
System.out.println(array[i]+" ");
}
  
public static void main (String[] args)
{
int array[] = {1,55,89,41,36};
int n = array.length;
dosort(array, n);
display(array, n);
}
}

Time complexity - O(nk) where k is size of integer


Related Solutions

Java program to implement the merge sort your own and test it to sort a list...
Java program to implement the merge sort your own and test it to sort a list of names based on the frequency.
Language: Java Implement Merge Sort
Language: Java Implement Merge Sort
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick...
Which of the following sorting algorithms are stable: insertion sort, selection sort, merge sort and quick sort? Give a simple scheme that makes any sorting algorithm stable. How much additional time and space does your scheme entail?
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.
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a...
Add bubble sort, radix sort, insertion sort, and merge sort to the code provided. Import a data set (txt file) then do the sorting algorithm to measure 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...
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
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAMM
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT...
Implementation of Quick sort and heap sorting algorithms in C++ FULL PROGRAMM BOTH THE QUICK SORT AND HEAP SORT IN THE SAME PROGRAM PS: YOU ARE ANSEWRING THE SAME PROGRAMS I WANT DIFFERENT ONE PLEASE , THANK YOU . BECAUSE THE ONE WERE POSTING DOESNT WORKING !!
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT