Question

In: Computer Science

In Java: Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND...

In Java:

Write two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (Merge Sort OR Quick Sort). If you choose Shell Sort, experiment with different incremental sequences to see how they affect the algorithm's run time efficiency (count the number of comparisons and exchanges). If you choose to implement Radix Sort, answer the following question as well: Can you write a version of Radix Sort for String objects? If yes, explain how. If not, explain why.

Assume that input data is generated randomly and stored in a text file.

You will experiment with your program in two steps:

Step 1: Experimenting with a prototype data (integers from 1 to 10) to ensure that your implementation works correctly and the results match expectations. The results must be reported in a table format (not generated by the program, but collected manually from multiple program runs) in the a Word document as follows:

                 best case         worst case        average case

          char.1...char.N   char.1...char.N   char.1...char.N

alg.1        ...       ...             ...     ...                 ...      ...

alg.2        ...       ...             ...     ...                 ...      ...

  ...  

alg.N      ...       ...              ...     ...                 ...      ...

  

Step 2: Experimenting with large data sets of 2000 elements. The results must be reported in the same table format.

In addition, in the report, explain the empirical results generated by your program comparing them to the known theoretical results paying special attention to any discrepancies which must be clearly explained.

Must submit for grading: the java code, the random text file(s) generated by an independent module (you may need multiple random text files to better characterize the average efficiency of the respective algorithm), AND MOST IMPORTANTLY the report as explained above a Word document.

Solutions

Expert Solution

Insertion short

import java.util.*;
import java.lang.*;
import java.io.*;

class InsertionSort {
  
void sort(int arr[])
{
int n = arr.length;
for (int i = 1; i < n; ++i) {
int key = arr[i];
int j = i - 1;
  
  
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
  
  
static void printArray(int arr[])
{
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
  
System.out.println();
}
  
  
public static void main(String args[])
{
int arr[] = { 10, 8, 9, 5, 2, 3, 1, 6, 4, 7 };
  
InsertionSort ob = new InsertionSort();
ob.sort(arr);
  
printArray(arr);
}
}

Output:- 1 2 3 4 5 6 7 8 9 10

Selection Short

import java.util.*;
import java.lang.*;
import java.io.*;

class SelectionSort
{
void sort(int arr[])
{
int n = arr.length;
  
  
for (int i = 0; i < n-1; i++)
{

int min_idx = i;
for (int j = i+1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
  
  
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}
  

void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i]+" ");
System.out.println();
}
  
  
public static void main(String args[])
{
SelectionSort ob = new SelectionSort();
int arr[] = {10, 8, 9, 5, 2, 3, 1, 6, 4, 7};
ob.sort(arr);
System.out.println("Sorted array");
ob.printArray(arr);
}
}

Output:- 1 2 3 4 5 6 7 8 9 10


Related Solutions

(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the...
(C++)Radix Sort: Write C++ codes for radix sort: use counting sort for decimal digits from the low order to high order. The input array is A = {329, 457, 657, 839, 436, 720, 353}
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort,...
Import a data set (txt file) then do the sorting algorithm using bubble sort, radix sort, insertion sort, and merge sort, It must show 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 3492 5068 9674...
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.
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort,...
Import a data set (txt file) then do the sorting algorithm using quick sort, shell sort, and selection sort. It must show 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 3492 5068 9674 8578 8323...
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be...
Java code: Use Radix sort to sort them in alphabetic order. How many passes would be made through the outer while loop? Trace the contents of the list after each of these passes. Define the efficiency of the following code fragment:             K := 1             repeat                     J := 1                         repeat                              ……..                              J := J * 2                         until J >= N                         K := K + 1                until K >= N
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write...
In Python, there are different sorting algorithms. Selection Sort, Bubble Sort and Insertion Sort. • Write a Pseudo code first for each of these sort methods.   • After writing the pseudo code write python code from the pseudo code. • Upload the pseudo code and Python code for each of the three algorithm mentioned.
IN JAVA. Which sorting does in place sorting? Which sort does the minimum swap to order...
IN JAVA. Which sorting does in place sorting? Which sort does the minimum swap to order ascending /desendinf manner?
Data structure program Implement (your own) the Radix Sort using single linked list java language
Data structure program Implement (your own) the Radix Sort using single linked list java language
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT