Docsity

CS303 Assignment 4: Algorithms and Data Structures - Binary Heaps and QuickSort, Assignments of Computer Science

Toggle sidebar
bg1
CS303: Algorithms and Data Structures Fall 2007
Assignment 4 (written) Due Date: Oct 31, 2007 (during lab hours)
1. (40 points) Justify your answers to the following questions:
(a) (10 points) Given a binary heap of height h, what are the least number of elements and most
number of elements that can be stored in the heap?
(b) (30 points) With initially an empty binary heap, show the result of inserting 10, 12, 1, 14, 6, 5, 8,
15, 3, 9, 7, 4, 11, 13, and 2 (each number is inserted one at a time).
2. (20 points) Describe an efficient algorithm to find, given a key k and a binary (minimum) heap, all
heap-nodes with keys less than k. For full credit your algorithm should run in time linear to the
number of output nodes.
3. (20 points each) Consider the following high-level code for QuickSort:
/* sort A[low] thru A[high] */
Procedure QUICKSORT(A, low, high)
If low >= high then return;
/* find a pivot */
P = PIVOT(A, low, high);
/* partition A using p such that A[mid] == p;
all the elements A[low] through A[mid-1] are less than p;
all the elements A[mid + 1] through A[high] are greater than p; */
PARTITION(A, low, high, p, mid);
/* sort the first half */
QUICKSORT(A, low, mid-1);
/* sort the second half */
QUICKSORT(A, mid +1, high);
End;
(a) Suppose PIVOT(A, low, high) always finds the smallest element in A[low] through A[high].
Describe and analyze the complexity of QUICKSORT() using O notation.
(b) Suppose PIVOT(A, low, high) always finds the median element in A[low] through A[high].
Describe and analyze the complexity of QUICKSORT() using O notation.

Partial preview of the text

Download CS303 Assignment 4: Algorithms and Data Structures - Binary Heaps and QuickSort and more Assignments Computer Science in PDF only on Docsity!

CS303: Algorithms and Data Structures Fall 2007 Assignment 4 (written) Due Date: Oct 31, 2007 (during lab hours)

  1. (40 points) Justify your answers to the following questions: (a) (10 points) Given a binary heap of height h , what are the least number of elements and most number of elements that can be stored in the heap? (b) (30 points) With initially an empty binary heap, show the result of inserting 10, 12, 1, 14, 6, 5, 8, 15, 3, 9, 7, 4, 11, 13, and 2 (each number is inserted one at a time).
  2. (20 points) Describe an efficient algorithm to find, given a key k and a binary (minimum) heap, all heap-nodes with keys less than k. For full credit your algorithm should run in time linear to the number of output nodes.
  3. (20 points each) Consider the following high-level code for QuickSort:

/* sort A[low] thru A[high] */ Procedure QUICKSORT(A, low, high)

If low >= high then return;

/* find a pivot */ P = PIVOT(A, low, high);

/* partition A using p such that A[mid] == p; all the elements A[low] through A[mid-1] are less than p; all the elements A[mid + 1] through A[high] are greater than p; */ PARTITION(A, low, high, p, mid);

/* sort the first half */ QUICKSORT(A, low, mid-1);

/* sort the second half */ QUICKSORT(A, mid +1, high);

End;

(a) Suppose PIVOT(A, low, high) always finds the smallest element in A[low] through A[high]. Describe and analyze the complexity of QUICKSORT() using O notation. (b) Suppose PIVOT(A, low, high) always finds the median element in A[low] through A[high]. Describe and analyze the complexity of QUICKSORT() using O notation.