Professional Documents
Culture Documents
Quiz II (Morning) : P. 1 of 1, Nassar
Quiz II (Morning) : P. 1 of 1, Nassar
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
1) [7 pts] The following is psuedocode for the Bubble Sort algorithm. Convert it into its equivalent
flow chart.
Input: array a[1 … n]
Output: Same array but with the n elements sorted ascendingly
Begin
For i = 1 to n-1 do
For j = 1 to n-i do
If (a[j] > a[j+1]) then swap a[j] and a[j+1]
End
p. 1 of 1, aoa_xf17q2m&a.docx, nassar
Model Answer
1) [7 pts] The following is psuedocode for the Bubble Sort algorithm. Convert it into its equivalent
flow chart.
Input: array a[1 … n]
Output: Same array but with the n elements sorted ascendingly
Begin
For i = 1 to n-1 do
For j = 1 to n-i do
If (a[j] > a[j+1]) then swap a[j] and a[j+1]
End
Start
Read array a
i=1
j=1
j < n-i?
Yes
No
i < n-1?
Yes
No
Write array a
End
p. 2 of 1, aoa_xf17q2m&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: 7, 2, 6, 1, 4, 3, 8, 5. 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,2,3 QS(PivotIndex+1,right)}}
QS3
QS (right, left) QS5
{while ((right - left) > 0) 2,3 QS (right, left)
{PivotIndex=Partition(lef
{while ((right - left) > 0)
QS(left,PivotIndex-1)
{PivotIndex=Partition(lef
QS(PivotIndex+1,right)}}
4,2,3,1 QS(left,PivotIndex-1)
QS(PivotIndex+1,right)}}
QS2
QS (right, left)
{while ((right - left) > 0)
{PivotIndex=Partition(lef
QS(left,PivotIndex-1)
5,2,6,1,4,3
QS(PivotIndex+1,right)}}
QS1
main
QS (right, left)
7,2,6,1,4,3,8,5 {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.
p. 3 of 1, aoa_xf17q2m&a.docx, nassar