Sie sind auf Seite 1von 9

1 Verify proof of correctness of any 4 algorithms with loop invariant property.

Example 5:
Bubble sort
Input: integer[]:A
Output: Integer[]:sorted by increasing order
Algorithm:
for I  1 to A.size-1
for j i+1 to A.size
if A[i]>A[j] then
temp  A[i]
A[i]=A[j]
A[j]= temp
End if
End for
End for
Return A
Initialization:
For i=0, the invariant is respected:the sub-array A[1….0] is sorted,trivially(it
contains no element)
Maintenance:
Given the subarray A[1…n-1] sorted.Iteration n inserts at position n the smallest o
the remaining unsorted elements of A[n….A..size],as computed by the j
loop.A[1….n-1] contains only elements smaller than A[n..A.size],andA[n] is smaller
than any element in A[n+1..A.size],then A[1…n] is sorted and the invariant is
preserved.
Termination:
At the last iteration , A[1…A.size-1] is sorted, and all elements in A[A.size-1..A.size]
are larger than elements in A[1…A.size-1]. Hence A[1..A.size] is sorted.
Example 1: Finding MAXIMUM element of array

nummax(A,n)
{
max=A[0]
for (i = 1 to n)
if(a[i]>max)
max=A[i]
return max

Proof of correctness Loop invariant for nummax(A):


“ Before the i th iteration (for i = 1, …, n) of the for loop maximum = max{A[0],
A[1], …, A[i – 1]} ”
Initialization:
We need to show loop invariant is true at the start of the execution of the for loop
Line 1 sets maximum=A[0] = max{A[i]|i=0, …,1-1} (Note: j=1) So the loop invariant
is satisfied at the start of the for loop.
Maintenance:
Assume that at the start of the i th iteration of the for loop
maximum = max{A[j] | j = 0, …, i - 1}
We will show that before the (i + 1)th iteration maximum =max{A[j] | j = 0, …, i}
The code computes
maximum=max(maximum, A[i]) = max(max{A[j] | j = 0, …, i - 1}, A[i]) = max{A[j] | j
= 0, …, i}
Termination :
i = n.
So maximum = max{A[j]|j=0, …,n - 1}

Sum of an array
int sum(int a[],int n)
{
int sum=0;
for(int i=0;i<n;i++)
{
sum+=a[i];
}
return sum;
}
Loop invariant:
At the ith iteration of the loop the variable sum contains sum of elements in array
a[0……..i]
Initialization:
At the beginning of the loop the array is empty and hence the sum=0
Maintanence:
At the ith iteration the variable sum contains the sum of elements of subarray
a[0…..i]
Termination:At the termination the sum contains the sum of elements that were
present in the array.
Linear Search
Int search(int a[],int n,i++)
{
for(int i=0;i<n;i++)
{
if(a[i]==ele)
return ele
}else
return “element not found”}
The above algorithm takes 3 arguments integer array , number of elements and
element to be searched.
Initialization:
At the start of the loop ith iteration if I is the element to be found , the array
a[0…..i-1] does not have the element
Maintenance:
If the element at ith iteration the a[0……i-1] does not contain the element.
Termination:
On terminating the loop at a[0…..i-1] does not contain element.
3. For the linear search and Binary search, which element in the following list can
be considered as a search element in Best, Worst and Average case?
10 20 30 40 50 55 60 65 70 75 80 90 100
Linear Search
Best case : searched element found at zeroth position of array – 10(element to be
searched is 10)
Worst case : searched element found at last position of array – 100(element to be
searched is 100)
Average case: searched element found in between the first and last element
(element to be searched can be any number from 20 to 90)

Binary Serach
Best case : searched element is present in middle of array
In this case if the element with index – 6 (60) to be searched then it
Is the best case time complexity.it needs only one iteration.
Worst case:[10,100] as they are in the end and need most number of iterations to
find it.
Average case:
In a binary search the average case and worst have about same time complexity
O(log n).

5.Write the pseudo code to implement queue using 2 stacks and explain with
suitable example and necessary diagrams.
enQueue() Operation:

1. Push the element to the Stack.

deQueue() Operation:

1. Pop all the elements from Main Stack to Temporary Stack.


2. Now all data is present in Temporary Stack, So simply pop element from
Temporary Stack.
3. All elements are now present in Temporary Stack and Main Stack is empty, we
will keep those 2 stack as it is and if the immediate next request continued to be
of deQueue() operation, then we will pop data from Temporary Stack.
4. Now, if next request is of enQueue() operation, then we need to pop all data
again from Temporary Stack(if present) to Main Stack first and then at last push
the requested element to Main Stack.
1.declare two stack – mainstack and tempstack
2.Do the enqueue or insertion operation in mainstack
3.move the data from main stack to tempstack in reverse orsder
4.now pop from tempstack and return the top element
5.pop one by one from tempstack.
6.if yoe enqueue again move the elements in tempstack to mainstack in reverse
order.
7.now push new element to mainstack.

4. Write the pseudo code to implement stack using 2 queues and explain with
suitable example and necessary diagrams.

pseudocode
Version A (efficient push):

 push:
 enqueue in queue1
 pop:
 while size of queue1 is bigger than 1, pipe dequeued items from queue1 into
queue2
 dequeue and return the last item of queue1, then switch the names of queue1 and
queue2
Version B (efficient pop):

 push:
 enqueue in queue2
 enqueue all items of queue1 in queue2, then switch the names of queue1 and
queue2
 pop:
 deqeue from queue1
2. Explain how stack is used for recursion process in Towers of Hanoi problem. Show
the stack content for each step with necessary diagrams.
This C Program uses recursive function & solves the tower of hanoi. The tower of hanoi
is a mathematical puzzle. It consists of threerods, and a number of disks of different
sizes which can slideonto any rod. The puzzle starts with the disks in a neat stack in
ascending order of size on one rod, the smallest at the top. We have to obtain the same
stack on the third rod.

Das könnte Ihnen auch gefallen