Answered step by step
Verified Expert Solution
Question
1 Approved Answer
complete the quick sort and insertion sort functions only without modifying the rest of the java code: import java.util.concurrent.ThreadLocalRandom; class ProgrammingAssignment 1 { / /
complete the quick sort and insertion sort functions only without modifying the rest of the java code:
import java.util.concurrent.ThreadLocalRandom;
class ProgrammingAssignment
Function to swap two integers without pass by reference since Java is passbyvalue only
static int swapHelperint a int b
return a;
Function to compute the median of three numbers. Used to benchmark runtimes fo sorting algorithms.
static int medianoflong a long b long c
if a c
if b a
return ;
else if c b
return ;
else
return ;
else if b c
return ;
else if a b
return ;
else
return ;
Check if the array is sorted. Used to check the correctness of the sorting algorithm implementations.
static boolean isSortedint arr, int n
for int i ; i n; i
if arri arri
return false;
return true;
Function to generate a sorted array of size n
static int sortedArrayint n
int data new intn;
for int i ; i n; i
datai i;
return data;
Function to generate an array of size n filled with constants zero in this case
static int constArrayint n
int data new intn;
return data;
Function to generate a random array of size n
static int randomArrayint n
int data sortedArrayn;
for int i n; i ; i
int swap ThreadLocalRandom.currentnextInt i;
dataswap swapHelperdatai dataidataswap;
return data;
Function to implement Selection Sort.
Input Arguments data: array to sort, n: number of elements in array
Output: Sorted array Ascending Order
static int selectionSortint data, int n
forint i; i n; i
int currmax i;
forint ki; k datak
currmax k;
datacurrmax swapHelperdatai dataidatacurrmax;
return data;
Helper function for merge sort
static int mergeFnint data, int left, int mid, int right
int n mid left ;
int n right mid;
int leftArr new intn;
int rightArr new intn;
for int i ; i n; i
leftArri dataleft i;
for int j ; j n; j
rightArrj datamid j;
int i j ;
int k left;
while i n && j n
if leftArri rightArrj
datak leftArri;
i;
else
datak rightArrj;
j;
k;
while i n
datak leftArri;
i;
k;
while j n
datak rightArrj;
j;
k;
return data;
Function to implement Merge Sort.
Input Arguments data: array to sort, n: number of elements in array
Output: Sorted array Ascending Order
static int mergeSortint data, int left, int right
ifleft right
int mid left right;
data mergeSortdata left, mid;
data mergeSortdata mid right;
data mergeFndata left, mid, right;
return data;
Function to implement Quick Sort.
Input Arguments data: array to sort, n: number of elements in array
Output: Sorted array Ascending Order
static int quickSortint data, int n
TODO: Your implementation of Quick Sort comes here.
return data;
Function to implement Insertion Sort.
Input Arguments data: array to sort, n: number of elements in array
Output: Sorted array Ascending Order
static int insertionSortint data, int n
TODO: Your implementation of Insertion Sort comes here.
return data;
Main function. Input Arguments:
Type of array to generate Allowed values: r:Random, s: Sorted, c: Constant
Size of array to evaluate on Allowed values: any positive integer
Sorting algorithm to evaluate Allowed values: q: Quick Sort, i: Insertion Sort, m: Merge Sort, s: Selection Sort
Output: time taken to run sorting algorithm
public static void mainString args
String arrayType r;
int n ;
String algo q;
if argslength
System.out.printlnInvalid command line arguments. Using defaults. Running quick sort on a random array of length ;
else
try
arrayType args;
n Integer.parseIntargs;
algo args;
catchException e
System.out.printl
try
arrayType args;
n Integer.parseIntargs
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started