0% found this document useful (0 votes)
7 views

IterativeRecursive ReviewQuestions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

IterativeRecursive ReviewQuestions

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Iterative and Recursive Algorithms

Question Bank
-------------------------------------------------------------------------------------------------------------------------------
1 Prove that for any nonnegative parameters a and b, the following algorithms terminate
and produce identical output.

SLOWEUCLID(a, b) :
If b>a
then return SLOWEUCLID(b, a)
else if b == 0
then return a
else return SLOWEUCLID(a, b − a)

FASTEUCLID(a, b) :
if b == 0
then return a
else return FASTEUCLID(b, a mod b)

2. Consider the following sorting algorithm:

STUPIDSORT(A[0 .. n − 1]) :
if n = 2 and A[0] > A[1]
then swap A[0] ↔ A[1]
else if n>2
then m = ⌈2n/3⌉
STUPIDSORT(A[0 ..m − 1])
STUPIDSORT(A[n − m.. n − 1])
STUPIDSORT(A[0 ..m − 1])

i. Prove that STUPIDSORT actually sorts its input. Justify your answer.
ii. State a recurrence (including the base case(s)) for the number of comparisons
executed by STUPIDSORT.
iii. Solve the recurrence, and prove that your solution is correct. [Hint: Ignore the
ceiling.
iv. Does the algorithm deserve its name?
v. Would the algorithm still sort correctly if we replaced m = ⌈2n/3⌉ with m =
⌊2n/3⌋? Justify your answer.

3. Give an algorithm that finds the second smallest of n elements in at most n + ⌈lg n⌉ − 2
comparisons. Hint: divide and conquer to find the smallest; where is the second smallest?
4. Suppose you have a pointer to the head of singly linked list. Normally, each node in the list only
has a pointer to the next element, and the last node’s pointer is NULL. Unfortunately, your list
might have been corrupted by a bug in somebody else’s code, so that the last node has a pointer
back to some other node in the list instead.
Describe an algorithm that determines whether the linked list is corrupted or not. Your
algorithm must not modify the list. Your algorithm should run in O(n) time,
where n is the number of nodes in the list, and use O(1) extra space

5. Suppose you are given two sorted arrays A[1 .. n] and B[1 .. n] and an integer k. Describe
and analyze an algorithm to find the kth largest element in the union of A and B in O(log
n) time.
For example, given the input
A[1 .. 8] = [0, 1, 6, 9, 12, 13, 18, 21], B[1 .. 8] = [2, 4, 5, 8, 14, 17, 19, 20], k = 10,
your algorithm should return 13. You can assume that the arrays contain no duplicates.

6. Given a null terminated linked list, rearrange its nodes into two lists: <first node, third
node, fifth node,…> and <second node, fourth node, sixth node, ...>. Do not allocate any
new nodes. Write an algorithm split(l1) that retains odd numbered nodes in the list l1 and
returns the pointer for the list containing even numbered nodes.

7. Write an efficient algorithm to sort n records G[0::n], where each record G[i] contains a
key

G[i]:KEY with possible values of either 0 or 1. The sorting must be done IN-PLACE,
using only O(1) amount of additional space.
(a) Write an algorithm for SORT(G; n). Give a very brief explanation of the
algorithm
(b) What is the time complexity of this algorithm?
(c) Illustrate how the sorting is done on the following array of key values. Show the result
after each data movement.
010100100011011

8. Write a recursive version of the binary search algorithm modified so that it locates the
left-most occurrence of the search key X found in the array (in the event that there are
several occurrences).
For example, for the following array:
i 012345 6 7 8
A[i] 2 4 5 5 5 8 10 20 25
a search on key 5 should return index 2.

9. Write an algorithm EqualList(l1,l2) that returns true if the two singly linked lists

l1 and l2 are identical.

You might also like