Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

CSE3358 Problem Set 4: Algorithms and Data Structures, Exams of Data Structures and Algorithms

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

2012/2013

Uploaded on 04/07/2013

seshu_lin3
seshu_lin3 🇮🇳

4

(3)

59 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSE3358 Problem Set 4
02/04/05
Due 02/11/05
Problem 1: S0rt1ng
(a) (10 points) Consider an array Aof nnumbers each of which is either 0or 1. We will refer to such
an array as binary. Describe an asymptotically efficient algorithm for sorting a binary array Aof size
n. For full credit you should:
Desrcibe your algorithm in english
Provide a clear, neat, and complete pseudocode for your algorithm similar to the way we do it
in class
Analyze correctly the running time of your algorithm and express the running time in either a
Θ or an Onotation.
Achieve a running time that is the best possible asymptotically
(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 Θ(n2) 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 Θ(nlog 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)
1
pf3
pf4

Partial preview of the text

Download CSE3358 Problem Set 4: Algorithms and Data Structures and more Exams Data Structures and Algorithms in PDF only on Docsity!

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:

  • Desrcibe your algorithm in english
  • Provide a clear, neat, and complete pseudocode for your algorithm similar to the way we do it in class
  • Analyze correctly the running time of your algorithm and express the running time in either a Θ or an O notation.
  • Achieve a running time that is the best possible asymptotically

(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:

  • s.push(x): pushes an Element x onto the stack
  • s.pop(): pops an Element off the stack and returns it
  • s.depth(): returns the number of Elements in the stack
  • s.max(): returns the maximum depth ever attained by the stack

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?