


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Problem set 4 for cse3358, a computer science course. The problems cover various topics including sorting algorithms, dynamic sets, recursion, and heap data structures. Students are required to provide algorithms, pseudocode, and analyze running times.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!
CSE3358 Problem Set 4 02/04/ Due 02/11/
Problem 1: S0rt1ng
(a) (10 points) Consider an array A of n numbers each of which is either 0 or 1. We will refer to such an array as binary. Describe an asymptotically efficient algorithm for sorting a binary array A of size n. For full credit you should:
(b) (10 points) We define a Lomuto Quicksort algorithm as a Quicksort algorithm that (recursively) partitions an array into a left sub-array with all elements ≤ Pivot, and a right sub-array with all elements > Pivot.
Argue (your argument must be very clear, organized, and convincing, if not a formal proof) that any Lomuto Quicksort algorithm will have an average running time of Θ(n^2 ) on binary arrays of size n (i.e. even when pivot is chosen randomly). Explain why this does not contradict the result that QUICKSORT has an average running time of Θ(n log n).
Problem 2: Variations of lists
(1 point each) For each of the following types of lists, what is the asymptotic (Θ notation) worst-case running time of the following dynamic-set operations? (make sure you understand what each operation does).
unsorted sorted unsorted sorted singly linked singly linked doubly linked doubly linked search(L,k) insert(L,x) delete(L,x) successor(L,x) predecessor(L,x) minimum(L) maximum(L)
Problem 3: Recursion and Stacks
(a) (5 points) Implement in C or C++ the following Quicksort algorithm using the Hoare PARTITION (Hoare is the guy who invented Quicksort).
PARTITION(A,p,r) x←A[p]. this is the pivot i←p-1. i starts at the far left j←r+1. j starts at the far right
while TRUE do repeat j←j-1. move j down until it finds an element ≤ x until A[j]≤ x repeat i←i+1. move i up until it finds an element ≥ x until A[i]≥ x if i<j. if they did not cross then swap A[i]↔A[j] else return j
QUICKSORT(A, p, r) if p < r then q ←PARTITION(A, p, r) QUICKSORT(A, p, q). note here the difference from the one we saw QUICKSORT(A, q + 1, r). in class which uses a Lomuto PARTITION
(b) (5 points) Implement in C or C++ a doubly linked list. Each element of the doubly liked list should be able to hold some data. For instance you can use the following declaration:
struct Element { Element * prev; Element * next; void * data; //anything you need for the specific application };
(c) (5 points) Using the linked list of part (b), implement a stack that keeps track of its depth. The stack should provide the following functionality:
Problem 4: Building a Heap
Consider the problem of building a heap from an array A. As we saw in class, this can be done by repeated calls to HEAPIFY.
BUILD-HEAP(A) for i←bn/ 2 c downto 1 do HEAPIFY(A, i)
(a) (5 points) Suggest another inplace algorithm for building a heap and provide its pseudocode and analyze its running time.
(b) (5 points) Does your algorithm produce the same result as the one based on repeated HEAPIFY? If yes, provide an argument. If no, provide a counter example.
(c) (5 points) Given n distinct elements, how many different heaps of these elements can you build (I have no idea now)? How does this number change with n asymptotically?