Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

package sorting; I need the extra credit also please and follow the instructions please import java.util.*; public class Sort3 { public static int[] quick_sort (int[]

image text in transcribed

package sorting;

I need the extra credit also please and follow the instructions please

import java.util.*;

public class Sort3 {

public static int[] quick_sort (int[] array, int p, int r) {

}

public static int partition (int[] array, int p, int r) {

}

public static int[] counting_sort (int[] array, int k) {

}

public static int[] generate_random_array (int n, int k) {

List list;

int[] array;

Random rnd;

rnd = new Random(System.currentTimeMillis());

list = new ArrayList ();

for (int i = 1; i

list.add(new Integer(rnd.nextInt(k+1)));

Collections.shuffle(list, rnd);

array = new int[n];

for (int i = 0; i

array[i] = list.get(i).intValue();

return array;

}

public static int[] generate_random_array (int n) {

List list;

int[] array;

list = new ArrayList ();

for (int i = 1; i

list.add(new Integer(i));

Collections.shuffle(list, new

Random(System.currentTimeMillis()));

array = new int[n];

for (int i = 0; i

array[i] = list.get(i).intValue();

return array;

}

/*

* Input: an integer array

* Output: true if the array is acsendingly sorted, otherwise return

false

*/

public static boolean check_sorted (int[] array) {

for (int i = 1; i

if (array[i-1] > array[i])

return false;

}

return true;

}

public static void print_array (int[] array) {

for (int i = 0; i

System.out.print(array[i] + ", ");

System.out.println();

}

public static void main(String[] args) {

// TODO Auto-generated method stub

int k = 10000;

System.out.println("Quick sort starts ------------------

");

for (int n = 100000; n

int[] array = Sort3.generate_random_array(n);

long t1 = System.currentTimeMillis();

array = Sort3.quick_sort(array, 0, n-1);

long t2 = System.currentTimeMillis();

long t = t2 - t1;

boolean flag = Sort3.check_sorted(array);

System.out.println(n + "," + t + "," + flag);

}

System.out.println("Quick sort ends ------------------");

System.out.println("Counting sort starts ----------------

--");

for (int n = 100000; n

int[] array = Sort3.generate_random_array(n,

k);

long t1 = System.currentTimeMillis();

array = Sort3.counting_sort(array, k);

long t2 = System.currentTimeMillis();

long t = t2 - t1;

boolean flag = Sort3.check_sorted(array);

System.out.println(n + "," + t + "," + flag);

}

System.out.println("Heap sort ends ------------------");

}

}

Instructions. You are provided the skeleton code named Sort2.java. The source file is available on Canvas in a folder named HW2. Please modify the skeleton code to solve the following tasks. . Task 1 (40 pts). Implement the Quick Sort algorithm as discussed in Lecture 4. (Hint: use the function checkedsorted to check if your output is indeed sorted.) . Task 2 (40 pts). Implement the Counting Sort algorithm as discussed in Lecture 4. (Hint: use the function checked.sorted to check if your output is indeed sorted.) . Task 3 (20 pts). Generate a report to discuss the time performance of the two algorithms. Compare it with their theoretical time complexity as discussed in the lecture. Plots and figures are encouraged to help draw the conclusion. Optional Task 4 (100 pts Extra Credit). Implement the Heap Sort algo- rithm as discussed in Lecture 3. (Hint: use the function checked sorted to check if your output is indeed sorted.)

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

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions

Question

What is the cycle of intimate partner abuse?

Answered: 1 week ago