You are on page 1of 3

Quiz II (evening) Calculator NOT allowed

Misr University for Sc & Tech (MUST) Term: Fall 2017 Department: Computer Science
Faculty of Information Technology Duration: 30 min Course: Design & Analysis of Algo CS 331

Student Name: …………………………………………………………………………………………………………………………. ID: ……………………………………

1) [7 pts] The following is psuedocode for the Insertion Sort algorithm. Convert it into an equivalent
flow chart.
Input: Array A[1 . . n]
Output: Same array but with contents sorted ascendingly
for j ←2 to n
Begin
pivot ← A[ j]
i ← j –1
while i > 0 and A[i] > pivot
Begin
A[i+1] ← A[i]
i ← i–1
End
A[i+1]←pivot
End

2) [8 pts] The following is psuedocode for the Quick Sort algorithm.


QS(left, right)
{
while ((right - left) > 0)
{
ProperPivotIndex = Partition(left, right)
QS(left, ProperPivotIndex - 1)
QS(ProperPivotIndex + 1, right)
}
}
Sketch on the back of the page the recursion chart for the function QS, if it takes the following array as
input: 8, 3, 7, 2, 8, 5, 4, 6. Number the copies of QS in the order they are instantiated, showing the input
of each copy.
p. 1 of 1, aoa_xf17q2e&a.docx, nassar
Model Answer
1) [7 pts] The following is psuedocode for the Insertion Sort algorithm. Convert it into an equivalent
flow chart.
Input: Array A[1 . . n]
Output: Same array but with contents sorted ascendingly
for j ←2 to n
Begin
pivot ← A[ j]
i ← j –1
while i > 0 and A[i] > pivot
Begin
A[i+1] ← A[i]
i ← i–1
End
A[i+1]←pivot
End

Start

Read array A

j=2

pivot = A[j]
i = j -1

No
A[i+1]=pivot i>0, A[i]>pivot? i=i-1

Yes
j=j+1
A[i+1] = A[i]

j < n?
Yes
No

Write array A

End

p. 2 of 1, aoa_xf17q2e&a.docx, nassar
2) [8 pts] The following is psuedocode for the Quick Sort algorithm.
QS(left, right)
while ((right - left) > 0)
{
ProperPivotIndex = Partition(left, right)
QS(left, ProperPivotIndex - 1)
QS(ProperPivotIndex + 1, right)
}
Sketch on the back of the page the recursion chart for the function QS, if it takes the following array as
input: 8, 3, 7, 2, 1, 5, 4, 6. Number the copies of QS in the order they are instantiated, showing the input
of each copy.

QS4
QS (right, left)
{while ((right - left) > 0)
{PivotIndex=Partition(lef
QS(left,PivotIndex-1)
1,3,4,2 QS(PivotIndex+1,right)}}

QS3
QS (right, left) 3,4,2 QS5
{while ((right - left) > 0)
QS (right, left)
{PivotIndex=Partition(lef
{while ((right - left) > 0)
QS(left,PivotIndex-1)
{PivotIndex=Partition(lef
QS(PivotIndex+1,right)}}
5,3,42,1 QS(left,PivotIndex-1)
QS(PivotIndex+1,right)}}

QS2
QS (right, left)
{while ((right - left) > 0)
{PivotIndex=Partition(lef
QS(left,PivotIndex-1)
6,3,7,2,1,5,4
QS(PivotIndex+1,right)}}
QS1
main
QS (right, left)
8,3,7,2,1,5,4,6 {while ((right - left) > 0)
QS(A) {PivotIndex=Partition(lef
QS(left,PivotIndex-1)
QS(PivotIndex+1,right)}}

Note: The arrow goes up if the first QS function is invoked, and goes down if the second is.

Course Instructor: Prof. Dr. Hamed Nassar


Course Assistant: Eng. Dina El Gammal

p. 3 of 1, aoa_xf17q2e&a.docx, nassar

You might also like