Docsity

6 Questions in Sorting Algorithm - Assignment 5 | CS 5633, Assignments of Algorithms and Programming

Toggle sidebar
bg1
CS 5633 Analysis of Algorithms { Spring 04
2/11/04
5. Homework
Due
2/18/04
before class
As usual, justify all your answers.
1.
Sorting algorithms (6 points)
Consider the four sorting algorithms
mergesort
,
quicksort
,
heapsort
,
counting sort
.
a) Which of the three algorithms are
stable
?
b) Which of the three algorithms sort
in-place
? (An algorithm sorts
in-place
if
it needs only a constant amount of extra space.)
c) Do you think that it would be possible to slightly modify the algorithms to
make them work in-place or to make them stable?
2.
Decision tree for merge sort (3 points)
Draw the decision tree for merge sort for inputs of length 3.
3.
Leafs in decision trees (4 points)
Consider a decision tree for a comparison sort of
n
elements.
a) How close to the root can a leaf be, i.e., what is the smallest possible depth
(the highestmost possible layer) at which a leaf can be?
b) How far away from the root can a leaf be, i.e., what is the largest p ossible
depth (the lowestmost possible layer) at which a leaf can be?
4.
Median computation (7 points)
Suppose arrays
A
and
B
are
both sorted
and both contain
n
elements. Givea
randomized divide-and-conquer algorithm to ๎˜Œnd the median of
A
[
B
in
expected
O
(log
n
) time. (Describe it either in words or as pseudo-code; whatever
you prefer). Argue
shortly
why the runtime is
O
(log
n
).
Hint: Take a look at
randomized select.
4.
Radix (1 point)
How many digits are there when a 64-bit quantity is viewed as a radix-128
number? Describ e how to extract each of the digits.
5.
Ranges (7 points)
Given
n
integers eachbetween 0 and
k
(inclusive). Give an algorithm which
preprocesses the input in
O
(
n
+
k
) time such that the following query can be
answered in
O
(1) time: \How many of the integers are in the range [
a; b
] (where
a; b
are query arguments) ?"
Hint: Take a look at counting sort.
6.
Sort in linear time (2 points)
Given
n
integers in the range 0 to
n
4
๎˜€
1. How fast can you sort them?

Partial preview of the text

Download 6 Questions in Sorting Algorithm - Assignment 5 | CS 5633 and more Assignments Algorithms and Programming in PDF only on Docsity!

CS 5633 Analysis of Algorithms { Spring 04

5. Homework

Due 2/18/04 b efore class

As usual, justify all your answers.

  1. Sorting algorithms (6 p oints) Consider the four sorting algorithms mergesort, quicksort, heapsort, counting sort.

a) Which of the three algorithms are stable? b) Which of the three algorithms sort in-place? (An algorithm sorts in-place if it needs only a constant amount of extra space.) c) Do you think that it would b e p ossible to slightly mo dify the algorithms to make them work in-place or to make them stable?

  1. Decision tree for merge sort (3 p oints) Draw the decision tree for merge sort for inputs of length 3.
  2. Leafs in decision trees (4 p oints) Consider a decision tree for a comparison sort of n elements.

a) How close to the ro ot can a leaf b e, i.e., what is the smallest p ossible depth (the highestmost p ossible layer) at which a leaf can b e? b) How far away from the ro ot can a leaf b e, i.e., what is the largest p ossible depth (the lowestmost p ossible layer) at which a leaf can b e?

  1. Median computation (7 p oints) Supp ose arrays A and B are b oth sorted and b oth contain n elements. Give a randomized divide-and-conquer algorithm to nd the median of A [ B in exp ected O (log n) time. (Describ e it either in words or as pseudo-co de; whatever you prefer). Argue shortly why the runtime is O (log n). Hint: Take a look at randomized select.
  2. Radix (1 p oint) How many digits are there when a 64-bit quantity is viewed as a radix- numb er? Describ e how to extract each of the digits.
  3. Ranges (7 p oints) Given n integers each b etween 0 and k (inclusive). Give an algorithm which prepro cesses the input in O (n + k ) time such that the following query can b e answered in O (1) time: \How many of the integers are in the range [a; b] (where a; b are query arguments) ?" Hint: Take a look at counting sort.
  4. Sort in linear time (2 p oints) Given n integers in the range 0 to n^4 1. How fast can you sort them?