1. (10 pts total) For parts (1a) and (1b), justify your answers
in terms of deterministic QuickSort, and for part (1c), refer to
Randomized QuickSort. In both cases, refer to the versions of the
algorithms given in the lecture notes for Week 3.
(a) (3 points) What is the asymptotic running time of QuickSort
when every element of the input A is identical, i.e., for 1 ≤ i,j ≤
n, A[i] = A[j]? Prove your answer is correct.
(b) (3 points) Let the input array A be [2,1,−1,4,5,−4,6,−3,3,0].
What is the number of times a comparison is made to the element
with value −3 (note the minus sign)?
(c) (4 points) How many calls are made to random-int in (i) the
worst case and (ii) the best case? Give your answers in asymptotic
notation.
Randomized-Quicksort(A,p,r) {
if (p<r){
q = RPartition(A,p,r)
Randomized-Quicksort(A,p,q-1)
Randomized-Quicksort(A,q+1,r)
}}.
RPartition(A,p,r) {
i = random-int(p,r) // NEW: choose uniformly random integer on [p..r]
swap(A[i],A[r]) // NEW: swap corresponding element with last element
x = A[r] // pivot is now a uniformly random element
i = p-1 // code from deterministic Partition
for (j=p; j<=r-1;j++) {
if A[j]<=x { // same as before
i++ //
swap(A[i],A[j]) //
}}
swap(A[i+1],A[r]) //
return i+1 //
},
1. (10 pts total) For parts (1a) and (1b), justify your answers in terms of deterministic...
In an effort to balance the distribution (length) of partitions
created in the Quicksort algorithm so that worst case performance
can be avoided, one can employ randomization, rather than selecting
the element at a certain position as the pivot. Use your
favorite
programming language to implement the randomized Quicksort
algorithm. So, you will need to use the following algorithms to
implement it:
RANDOMIZED-PArtitioN (A, p, r) 1 i = RANDOM (p, r) 2 exchange A[r] with A[i] 3 return PARTITION...
In C++:
1. What is the difference between a deterministic algorithm and a randomized algorithm? 2. What is the difference between a Las Vegas algorithm (pattern) and a Monte Carlo algorithm? 3. Solve the following recurrences, giving the answer in terms of Big-Oh O() F (n) = 4F ( 1 ) +no F (n) = 2 F (*) +2 4. Given the string "DECEMBER", execute the mergesort algorithm by hand to sort it. Show your work to make it clear...
(10 pts.) Count the worst-case number of array element comparisons (A[j] < A[j-1]) made by InsertionSort on arrays of size n: void InsertionSort(int A[], int n) { for (int i = 1; i < n; ++i) for (int j = i; j > 0 && A[j] < A[j-1]; --j) swap(A[j], A[j-1]); } Do the same for the number of swap's. 2. Which function grows faster: 2^((lg?))2 or ?^(2019)? Justify your answer. 3. Use "name and conquer" to give a derivation...
And the related
algorithms:
(20 points) Consider the following strategy for choosing a pivot element for the Partition subroutine of QuickSort, applied to an array A. .Let n be the number of elements of the array A. If n 24, perform an Insertion Sort of A and return. Otherwise: Choose 2n/2)| elements at random from n; let S be the new list with the chosen elements. Sort the list S using Insertion Sort and use the median m of S...
6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...
Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...
please use c++ language 1. Request three different integers from the console. a) Write a swap function that swaps two integer values. b) Write a sort function which accepts three integers as input then sorts them from largest to smallest using the swap function. c) Output the integers before and after sorting. Example 1 Output (input in bold italics) Enter three different integers: 3 2 4 3 2 4 4 3 2 Example 2 Output (input in bold italics) Enter...
Please, explain clearly each line of code with your answers. Question 1 Consider the following code snippet: int ctr = 0; int myarray[3]; for (int i = 0; i < 3; i++) { myarray[i] = ctr; ctr = ctr + i; } cout << myarray[2]; What is the output of the code snippet? Question 2 Consider the following code snippet: int cnt = 0; int numarray[2][3]; for (int i = 0; i < 3; i++) { for (int j =...
In this assignment, you are given several classes in the cpp file “DList.cpp”. Your task is to complete the implementation of the classes specified as below. Y 1 Your Task You are given a class “Item” that contains one integer value, and two pointers. You are going to build a doubly linked list class DLinkedList. I describe the tasks below. Task 1: Implement the constructors (default and copy) of DLinkedList. You need to make sure that the copy constructor makes...
I am getting the Segmentation fault error on the Ubuntu machine
but not on macOS.
Any help would be appreciated.
/**** main.c ****/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#define WORD_LEN 6
#define TOP 10
char * delim = "\"\'.“”‘’?:;-,—*($%)! \t\n\x0A\r";
struct Word {
char word[30];
int freq;
};
int threadCount;
int fileDescriptor;
int fileSize;
off_t chunk;
struct Word* wordArray;
int arrIndex = 0;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;...