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

Application of Stack:

Recursive Function:
OS process: Stack, Queue, Priority Queue
Compiler task: Algebric expression
Recursive Fn:
N! = 1*2…*(n-2)*(n-1)*n
Factorial(n)
{Fact=n*Factorial(n-1)
-
-
Return}
Fact(n)->fact(n-1)->fact(n-2)……..fact(2)->fact(1)=1

Mathematical expression: Infix, Postfix , Prefix


Infix : (A+B)*C
Precedence: ^
*/
+-
Parenthesis () {} []
Postfix -Polish : A B +
Prefix- Reverse Polish : + A B

A+{B*C-(D/E^F)* G}*H
Alogorithm 6.5
1. Add a right parenthesis “)” at the end of the P
2. Scan P from left to right and repeat steps 3,4 for each element of P until senitel “)” is
encountered
3. If an operand is encountered, put it on Stack
4. If an OPERATOR is encountered then:
a. Remove the two top elements of stack where A is top and B is next to top
b. Evaluate B OPERATOR A
c. Place the result of (b) back on Stack
[End of Step 2 loop]
5. Set VALUE equal to top element of Stack
6. exit

Evaluation of postfix expression


Infix Q: 5 * (6+2) – 12 / 4= 37
P Postfix: 5 6 2 + * 12 4 / - )
If operand: push stack
If operator: then pop two operand from stack then evaluate and push the result on stack
A=2, B=6
B + A= 6+2=8
A= 8 B=5
B*A=5*8=40
A= 4 B=12
B/A=3
A= 3 B=40
B-A=37

Stack Postfix, P
(+( ABC*
(+(- ABC*DEF
(+(-* ABC*DEF^/G
ABC*DEF^/G*-H*+

Prefix to Postfix:
Infix Q: A+(B*C-(D/E^F)*G)*H)
Postfix, P: A B C * DEF^/G*- H * +
Algorithm 6.6 Polish(Q,P)
1. Push “(“ onto Stack and “)” to the end of Q
2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q:
3. If an operand is encountered, add it to P
4. If left parenthesis is encountered, push onto stack
5. If and operator is encountered then:
a. Repeatedly pop from Stack and add to P each operator with same or higher precedence
than operator
b. Add operator to Stack
6. If a right parenthesis is encountered then:
a. Repetedly pop from stack and add it to P each operator until a left parenthesis is
encountered
b. Remove left parenthesis from Stack
[End of step 2 loop]
7. Exit
Sorting:
Numeric value
Alphabetic/Alphaneumeric

Ascending/Strictly increasing: Smaller to large


Descending/ Strictly decreasing: Larger to smaller

659 388
3 5 6 8 8 9 Ascending Asc
9 8 8 6 5 3 Descending Desc

Bubble Sort
Selection
Insertion Sort
Merge sort
Quicksort
Bucket sort
Counting
Nested Loop:
Algorithm 4.4 Bubble(Data, N)
1. Repeat steps 2 and 3 for K=1 to N-1
2. Set PTR=1 [Initialize pass pointer]
3. Repeat while PTR<=N-K
a. If DATA[PTR]>Data[PTR+1] then
Interchange DATA[PTR] and Data[PTR+1]
b. Set PTR=PTR+1
[End of loop]
Bubble Sort: K= 1 to 7 PTR= 1 to N-K Temp=13 ptr=1 to 6
1 2 3 4 5 6 7 8=N
13 23 27 85 66 51 32 57
13 23 27 66 51 32 57 85 K=1
13 23 27 51 32 57 66 85 K=2
13 23 27 32 51 57 66 85 K=3
K=4

K=7
K= 1
PTR=1 DATA[1]>Data[2] 13>23
PTR=2 DATA[2]>Data[3] 23>27
PTR=3 DATA[3]>Data[4] 27>85
PTR=4 DATA[4]>Data[5] 85>66 true swap
PTR=5 DATA[5]>Data[6] 85 > 51 true swap
PTR=6 DATA[6]>Data[7] 85>32 true swap
PTR=7 DATA[7]>Data[8] 85>57 true swap
PTR= 8-2=6
PTR=1 DATA[1]>Data[2]
PTR=2 DATA[2]>Data[3]
PTR=3 DATA[3]>Data[4]
PTR=4 DATA[4]>Data[5]
PTR=5 DATA[5]>Data[6]
PTR=6 DATA[6]>Data[7]
For (k 1 to N-1)
For ( j 1 to N-k)
If(Data[j]>Data[J+1]
Temp=Data[j]
Data[j]= Data[j+1]
Data[j+1]= temp
1 index 2 3 index 4 5 6 index 7 index 8=N
32 51 27 85 66 23 13 57
13 51 27 85 66 23 32 57 i=1
13 23 27 85 66 51 32 57 I=2
13 23 27 85 66 51 32 57 i=3
13 23 27 32 66 51 85 57 I=4
13 23 27 32 51 57 66 85 I=5

I=2 j=4 to 8 index=3 swap (data[3], data[index])


Pseudo-code of Selection sort
Seletion(Data, N)
For( i = 1 to N-1)
Index=i
For (j= i+1 to N)
If (Data[index] > Data[j])
Index= j
Temp=Data[i]
Data[i]=Data[Index]
Data[Index]= temp
Insertion Sort: (Adaptive)
For j=2 to N
Key= A[j]
i=j-1
While i>0 and A[i]>key
A[i+1]=A[i]
i=i-1
A[i+1]=key
1 2 3 4 5 6 7 8=N
32 51 27 85 66 23 13 57
32 51 27 85 66 23 13 57 J=2, i=1
32/ 27 51/ 32 27/ 51 85 66 23 13 57 J=3, i=2, 1
27 32 51 85 66 23 13 57 J=4, i=3,2,1
27 32 51 85/ 66 66/ 85 23 13 57 J=5, i=4,3,2,1
23 27 32 51 66 85 13 57 J=6, i=5,4,3,2,1

J=2, key=A[j=2]=51, i=1 A[1]>key 32>51 false break ; A[i+1]=A[2]=key=51


J=3 key=A[3]=27 , i=2 51>27 true A[i+1]=A[i]=51, i=1 32>27 true A[2]=A[1]=32;
i=0 break; A[0+1]=A[1]= key=27
J=4, key=85, i=3, 51>85 False Break; A[4]=85
J=5, key=66, i=4 85>66 true A[5]=A[4]; i=3, 51>66 false break; A[4]=key=66
J=6, key=23, i=5, 85>23 true, A[6]=A[5]=85;
i=4, 66>23 true, A[5]=A[4]=66;
i=3, 51>23 true, A[4]=A[3]=51;
i=2, 32>23 true, A[3]=A[2]=32;
i=1, 27>23 true, A[2]=A[1]=27;
i=0 break; A[0+1] A[1]=key=23

1 2 3 4 5 6 Best case n^2 n


6 5 4 3 2 1 Worst case n^2
Binary Search: works on sorted data
Binary(Data, LB,UB, Item, LOC)
1. Set beg=LB, end=UB and Mid= (beg+end)/2
2. Repeat steps 3 and 4 while beg<=end and Data[Mid]!=Item
3. If Item<Data[Mid]
Set end=Mid-1
Else Set beg=Mid+1
4. Set Mid=Int(beg+end)/2
5. If Data[Mid]==Item then set Loc=Mid
Else set Loc=Null
UB=10 LB=1 Item=33, Item=34
Beg=LB Beg/mid Beg/End/mid/lo End=UB
c
1 2 3 4 5 6 7 8 9 10
11 22 30 33 40 44 55 60 66 77

Beg=lb=1, end=ub=10, mid= beg+end/2= floor(1+10/2)=5 Data[mid]=40!=33 33<40 true ; end=Mid-1=4


Beg=1 end=4 mid=floor(5/2)=2 22!=33; 33<22 false; Beg=Mid+1=3
Beg=3, End=4 mid=Floor(7/2)=3 30!=33; 33<30 false; Beg=mid+1=4
Beg=4, end=4, mid=8/2=4 Data[4]==item==33, False, Loc=mid=4
Linear Search: worst n =16
Binary Search lgn=4

You might also like