1-Flowchart For An Algorithm Is Given As Follows:: CE 242 Data Structures and Algorithms - Midterm Exam II Solutions
1-Flowchart For An Algorithm Is Given As Follows:: CE 242 Data Structures and Algorithms - Midterm Exam II Solutions
a(25p)- Analyze the algorithm for k=25. You should show the value of all variables in
each step of the algorithm.
k=25, I=0
Step1: k<>0 so B[0]=25 mod 2= 1
k=25/2=12, I=0+1=1
Step2: k<>0 so B[1]=12 mod 2= 0
k=12/2=6, I=2
Step3: k<>0 so B[2]=6 mod 2= 0
k=6/2=3, I=3
Step4: k<>0 so B[3]=3 mod 2=1
k=3/2=1, I=4
Step5: k<>0 so B[4]=1 mod 2=1
k=1/2=0, I=5, then the program exits from the while loop since k becomes zero.
I=I-1=4
N=I-1=4
Enter the for loop:
J=0, A[0]=B[4]=1, I=3
J=1, A[1]=B[3]=1, I=2
J=2, A[2]=B[2]=0, I=1
J=3, A[3]=B[1]=0, I=0
J=4, A[4]=B[0]=1, I=-1
Print A=1, 1, 0, 0, 1
Within the while loop, each time the number k divided by 2 until k becomes 0. So the
number of the operations ( I ) depend on the number k,
for k=25 the program will enter the loop 5 times. k=25 is not a power of 2 but
24 < k = 25 > 25 so the number of operations is equal to 5.
So in general we can conclude that the program enters the while loop I = (log 2 k ) + 1
times, which is the number of comparisons (k< >0). The number of comparisons also
represents the complexity of the algorithm.
Ignoring the constants, 3.321 and 1, the big-O notation will be: log k
The program also enters the “for” loop I − 1 = log 2 k times. This will not change big-
O notation.
2(25p)- For a given integer array, you are required to determine the items that repeats
itself in the next address, determine the number of repeats and for the repeating
number. Draw a flowchart diagram for the algorithm and analyze for A=1,2,3,3,2,2,5
1 3 7 4 4 4 7 1 1 1 1 3
The output of the program should be two separate arrays called Numbers and Repeats:
Numbers=4,1 (repeating numbers are 4 and 1)
Repeats=3,4 (they repeat 3 and 4 times respectively)
If we analyze the algorithm for A=1,2,3,3,2,2,5 :
First we create an array called Repeats and fill the array with 1.
T = 0, K = 0
a(15p)- Write the array A and array P after deleting ‘23’ from the linked list.
b(15p)- Write the array A and array P after inserting an element valued ‘35’ into the
proper place in the original linked list (before deleting 23).
X 4 49 5 44 2 42 3 32 1 23 6 18 -1
0 1 2 3 4 5 6 7
A null 23 42 32 49 44 18 null
P 4 6 3 1 5 2 -1 null
0 1 2 3 4 5 6 7
A null 23 42 32 49 44 18 null
P 4 -1 3 6 5 2 -1 null
Also you can arrange ‘23’ to point to nothing (actually you do not have to worry
about what 23 points to – since none of the other items points to ‘23’ you will not
reach ‘23’ anymore). Trying to remove ‘23’ from array A would be a mistake, after all
why do we use linked lists?
b- Inserting ‘35’ requires ‘42’ to point to ‘35’ instead of ‘32’, also now ‘35’ should
point to ‘32’;
0 1 2 3 4 5 6 7
A null 23 42 32 49 44 18 35
P 4 6 7 1 5 2 -1 3
You also do not need to worry where to place the new number ‘35’ in array A, you
should place it at the end to avoid any unnecessary operations. You should only
organize the links in array P.