Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

ABDUL WASAY

Question 1 Following is an algorithm for Sorting an array:


Procedure: SORT(A, N)Where A is the array with N elements. This procedure sorts the array A having N
elements.

1. Repeat steps 2 to 4 for J = 1 to N –1.


2. Set MIN := J.
3. Repeat for K = J + 1 to N.
a. If A[K] < A[MIN], then:
Set MIN : = J.
[end inner for-loop]
4. Swap A[J], A[MIN].
[end outer for-loop]
5. return
Understand the algorithm and describe its working by specifying every single pass using the
following array of integers:
12, 23, 56, 38, 19, 45

IMPLEMENTATION:
PASS 1:
12 23 56 38 19 45

PASS 2:
ABDUL WASAY

PASS 3:

PASS 4:

Question 2) Let C denotes the number of times LOC is updated to find the largest element
in array A with N elements. Write a procedure COUNT(A, N, C)which finds C.
ANSWER:
ABDUL WASAY

ALGORITHM:
STEP 1: START [Initialize] Set LOC=1 LARGEST=A[1]
STEP 2: Repeat STEP 3 and 4 for C=1 to C<=N, then:
STEP 3: If LARGEST< A[C] then :
LOC = C , LARGEST = A[C]
[ end of if structure ]
STEP 4: Set C++
[ end of Step 2 ]
STEP 5: Write C , LARGEST
STEP 6: END
Question 3) Write a procedure INSERT (P, T, LOC, R, S)that inserts pattern P in string T
having lengths R and S respectively, at location LOC.
ANSWER:
ALGORITHM:
STEP 1: [Initialize] Set T: =text, LOC: =position and P: =pattern
STEP 2: Set R:= SUBSTRING (T, 1, LOC-1)
STEP 3: Set S:= SUBSTRING (T, K, LENGTH (T)-LOC+1)
STEP 4: [Concatenate] Set P2:=R||P||S
STEP 5: Write: P2
STEP 6: Exit

Question 4) Can stacks be used to implement queues? If yes, show how to implement a
queue using stacks and describe the working of your algorithms (enqueue and dequeue)
using appropriate diagrams.
ANSWER: Yes, stack can be used to implement queues. We uses two stacks to implement
queues first we added element in first stack (stack_1) then then pop (removing) the element and
pushed (added) in second stack (stack_2) thus in result it reverses the arrangement (order) of
element and forms a Queue. We will discuss and describe with the help of diagram to understand
enqueue and dequeue method.
ALGORITHM [ENQUEUE(ITEM)]:
ABDUL WASAY

STEP 1: START [initialize S1 and S2]


STEP 2: Repeat STEP 3 While S1 != empty
STEP 3: push S1 to S2.TOP and pop S1
[end of loop]
STEP 4: push ITEM to S1
STEP 5: Repeat STEP 6 While S2 != empty
STEP 6: push S2 to S1.TOP and pop S2
[end of loop]
STEP 7: EXIT

4
Rear Front
3

Stack_1 Stack_2
ENQUEUE
ALGORITHM [DEQUEUE]:
STEP 1: START [initialize S1 and S2]
STEP 2: [Check] if S1 = empty
Write “queue is empty” and return
STEP 3: Then;
ITEM= S1 TOP
STEP 4: pop S1 and Return ITEM
STEP 5: END

Push pop
ABDUL WASAY

pop
1
Rear Front
2

Stack_1 Stack_2
DEQUEUE
Question 5) Consider the following Tower of Hanoi with 4 discs:
Describe the working of the recursive method of moving the entire stack of discs from
SOURCE to DEST using appropriate diagram.
ANSWER)

SOURCE DEST AUX

SOURCE DEST AUX


ABDUL WASAY

SOURCE DEST AUX

SOURCE DEST AUX

SOURCE DEST AUX

You might also like