Beruflich Dokumente
Kultur Dokumente
The order in which elements are pushed onto a stack is reverse of the
• (A + B) * (C - D)
•
• A ^ B * C - D + E / F /(G + H)
•
• ((A + B) * C – (D - E)) ^ (F + G)
•
• A - B/(C * D ^ E)
•
• Example:
• Infix: Postfix/ Prefix
• A+B-C AB + C -
• - + ABC
• (A + B) * (C - D) AB + CD - *
• * + AB - CD
• A ^ B * C - D + E / F /(G + H) AB ^ C * D - EF / GH + / +
• + - * ^ ABCD / / EF + GH
• ((A + B) * C – (D - E)) ^ (F + G) AB + C * DE - - FG + ^
• ^ - * + ABC – DE + FG
• A - B/(C * D ^ E) ABCDE ^ * / -
• - A / B * C ^ DE
The computer usually evaluates an arithmetic expression written in infix
notation in two steps.
• Converts expression to postfix notation
• Evaluates the postfix notation.
( /
+ + +
• P= A B C* D / +
( ( (
• Example: Consider the following infix expression Q:
• Q : A + ( B * C – ( D / E ^ F) * G) * H
• Convert Q into it’s postfix expression p using stack.
A + B * C - D / E
Solve by Yourself:
Infix Stack
Postfix Expression
Postfix Evaulation
Operand: push
Operator: pop 2 operands, do the math, pop result
back onto stack
1 2 3 + *
Postfix Stack[top ]
a) 1 2 3 + *
b) 2 3 + * 1
c) 3 + * 1 2
d) + * 1 2 3
e) * 1 5 // 5 from 2 + 3
f) 5 // 5 from 1 * 5
• Ex:
• Evaluate the following postfix expression
1- p= 5, 6, 2, +, *, 12, 4, /, -
2- p=6, 2, 3, +, -, 3, 8, 2, /, +, *, 2, ^, 3, +
2- p=6, 2, 3, +, -, 3, 8, 2, /, +, *, 2, ^, 3, +
Ans: 52
3- Write: BEG->END
5- Return
• Let Gn(i) be a function from [0,…,2n-1]
• Total no of moves = 2n -1
Application of Stack: Quick Sort
It is an algorithm of divide and conquer.
Example
We are given array of n integers to sort:
40 20 10 80 60 50 7 30 70
Pick Pivot Element
There are a number of ways to pick the pivot element. In this
example, we will use the first element in the array:
40 20 10 80 60 50 7 30 70
Quick sort algorithm
QUICKSORT(A,LOW, HIGH)
1:- if(LOW< HIGH)
Pivot = PARTITION(A, LOW,HIGH)
QUICKSORT(A, LOW, Pivot-1)
QUICKSORT(A, Pivot+1, HIGH)
[End of If]
2:-END
Quick sort algorithm
PARTITION(A, BEG, END, LOC)
1. 1- Set LEFT = BEG, RIGHT=END, LOC=BEG
2. 2- [Scan from right to left]
(a) Repeat while A[LOC] <=A[RIGHT] and LOC != RIGHT
RIGHT = RIGHT-1
[End of Loop]
(b) If LOC == RIGHT then return
(c) If A[LOC] > A[RIGHT] then
(i) Interchange A[LOC] and A[RIGHT]
(ii) Set LOC = RIGHT
[End of if structure]
3. 3- [Scan from left to right]
(a) Repeat while A[LEFT] <=A[LOC] and LEFT != LOC
LEFT = LEFT+1
[End of Loop]
(b) If LOC == LEFT then return
(c) If A[LEFT] > A[LOC] then
(i) Interchange A[LEFT] and A[LOC]
(ii) Set LOC = LEFT
(iii) Go to step 2.
[End of if structure]
Complexity of Quick Sort
Worst case
O(n2)
Average case
O(n log n)
Merge Sort
18 26 32 6 43 15 9 1 1 6 9 15 18 26 32 43
18 26 32 6 43 15 9 1 6 18 26 32 1 9 15 43
43
18 26 32 6 43 15 9 1 18 26 6 32 15 43 1 9
18 26 32 6 43 15 9 1 18 26 32 6 43 15 9 1
18 26 32 6 43 15 9 1
Merge-Sort (A, p, r)
A … 61 8
6 26
8 32
9 1
26 9
32 42 43 …
k k k k k k k k k
L 6 8 26 32
R 1 9 42 43
i i i i i j j j j j
Comparing the Sorting Algorithms