subject

Task 1 (40 pts). Implement the Quick Sort algorithm as discussed in Lecture 4. (Hint: use the function checked_sorted 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.)package sorting;

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 <= n; i++)
list. add(new Integer(rnd. nextInt(k+1)));

Collections. shuffle(list, rnd);

array = new int[n];
for (int i = 0; i < n; 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 <= n; i++)
list. add(new Integer(i));

Collections. shuffle(list, new Random(System. currentTimeMillis()));

array = new int[n];
for (int i = 0; i < n; 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 < array. length; i++) {
if (array[i-1] > array[i])
return false;
}
return true;
}

public static void print_array (int[] array) {
for (int i = 0; i < array. length; 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 <= 1000000; n=n+100000) {
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 <= 1000000; n=n+100000) {
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("Counting sort ends ");
}
}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 10:40
"it security policy enforcement and monitoring" respond to the following: describe how monitoring worker activities can increase the security within organizations. describe the rationale that managers should use to determine the degree of monitoring that the organization should conduct. explain the extent to which you believe an organization has the right to monitor user actions and traffic. determine the actions organizations can take to mitigate the potential issues associated with monitoring user actions and traffic.
Answers: 3
question
Computers and Technology, 23.06.2019 13:10
What is domain name system (dns)? allows dynamic ip address allocation so users do not have to have a preconfigured ip address to use the network converts ip addresses into domains, or identifying labels that use a variety of recognizable naming conventions the efficient coexistence of telephone, video, and data communication within a single network, offering convenience and flexibility not possible with separate infrastructures the integration of communication channels into a single service
Answers: 2
question
Computers and Technology, 23.06.2019 15:10
What role did women fill during world war ii?
Answers: 1
question
Computers and Technology, 23.06.2019 19:30
Of the following pieces of information in a document, for which would you most likely insert a mail merge field?
Answers: 3
You know the right answer?
Task 1 (40 pts). Implement the Quick Sort algorithm as discussed in Lecture 4. (Hint: use the functi...
Questions
question
Mathematics, 03.03.2021 04:20
question
History, 03.03.2021 04:20
question
Mathematics, 03.03.2021 04:20
question
Mathematics, 03.03.2021 04:20
question
Mathematics, 03.03.2021 04:20
Questions on the website: 13722363