Answered step by step
Verified Expert Solution
Link Copied!

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 ProgrammingAssignment1{
//Function to swap two integers without pass by reference since Java is pass-by-value only
static int swapHelper(int a, int b){
return a;
}
//Function to compute the median of three numbers. Used to benchmark runtimes fo sorting algorithms.
static int medianof3(long a, long b, long c){
if (a < c)
{
if (b < a)
return 1;
else if (c < b)
return 3;
else
return 2;
}
else if (b < c)
return 3;
else if (a < b)
return 1;
else
return 2;
}
//Check if the array is sorted. Used to check the correctness of the sorting algorithm implementations.
static boolean isSorted(int[] arr, int n)
{
for (int i =0; i < n-1; i++)
if (arr[i+1]< arr[i])
return false;
return true;
}
//Function to generate a sorted array of size n.
static int[] sortedArray(int n){
int[] data = new int[n];
for (int i =0; i < n; i++)
data[i]= i+1;
return data;
}
// Function to generate an array of size n filled with constants (zero in this case)
static int[] constArray(int n){
int[] data = new int[n];
return data;
}
// Function to generate a random array of size n.
static int[] randomArray(int n){
int[] data = sortedArray(n);
for (int i = n; i >1; i--)
{
int swap = ThreadLocalRandom.current().nextInt(0,1000)% i;
data[swap]= swapHelper(data[i-1], data[i-1]=data[swap]);
}
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[] selectionSort(int[] data, int n){
for(int i=0; i < n; i++){
int curr_max = i;
for(int k=i+1; k data[k])
curr_max = k;
}
data[curr_max]= swapHelper(data[i], data[i]=data[curr_max]);
}
return data;
}
// Helper function for merge sort
static int[] mergeFn(int[] data, int left, int mid, int right){
int n1= mid - left +1;
int n2= right - mid;
int leftArr[]= new int[n1];
int rightArr[]= new int[n2];
for (int i =0; i < n1; ++i)
leftArr[i]= data[left + i];
for (int j =0; j < n2; ++j)
rightArr[j]= data[mid +1+ j];
int i =0, j =0;
int k = left;
while (i < n1 && j < n2){
if (leftArr[i]<= rightArr[j]){
data[k]= leftArr[i];
i++;
}
else {
data[k]= rightArr[j];
j++;
}
k++;
}
while (i < n1){
data[k]= leftArr[i];
i++;
k++;
}
while (j < n2){
data[k]= rightArr[j];
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[] mergeSort(int[] data, int left, int right){
if(left < right){
int mid =(left + right)/2;
data = mergeSort(data, left, mid);
data = mergeSort(data, mid +1, right);
data = mergeFn(data, 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[] quickSort(int[] 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[] insertionSort(int[] 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 >0
// 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 main(String[] args){
String arrayType ="r";
int n =10;
String algo ="q";
if (args.length !=3){
System.out.println("Invalid command line arguments. Using defaults. Running quick sort on a random array of length 10");
}
else
{
try{
arrayType = args[0];
n = Integer.parseInt(args[1]);
algo = args[2];
}
catch(Exception e){
System.out.printl
try{
arrayType = args[0];
n = Integer.parseInt(args[1

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

List out some inventory management techniques.

Answered: 1 week ago

Question

Understanding Group Leadership Culture and Group Leadership

Answered: 1 week ago