Stack Recursion Quick Sort Word
Stack Recursion Quick Sort Word
Stack
The Linear list and linear array allows us to insert or
delete elements at any place in the list :
At the beginning.
At the end.
Or in the middle.
Representation of stack
Stack may be represented in various ways usually by
means of a one way list or linear array.
PUSH Operation
Algorithm : Push an element in Stack.
PUSH(STACK, TOS, max, Item)
Step 1: If (TOS = max)
print “overflow condition”
Endif
Step 2: TOS=TOS+1.
Step 3: STACK[TOS] = Item.
Step 4: END.
Sweta Bohra
POP Operation
Algorithm : Pop an element in Stack.
POP(STACK, TOS, max, Item)
Step 1: If (TOS = -1)
print “underflow condition”
Endif
Step 2: Item = STACK[TOS}
Step 3: TOS = TOS - 1
Step 4: END.
Sweta Bohra
`
TOS TOS
501 501
NewNode NewNode
501 501
TOS
501 51 101 211
212
NULL
Colour
NewNode
212
Sweta Bohra
TOS
501 51 101 211
212
Colour 501
NewNode
212
TOS
501 51 101 211
TOS 501
Arithmetic Expression
Let
Q is an Arithmetic expression involving constant and
operations.
For simplicity
we use BINARY operations
No UNARY operations
Operations performed left to right
2. InFix
A+B OpRand Opr OpRand
PUSH(Val)
V1 = POP()
V2 = POP()
Step 6. PUSH(Res)
Algorithm
o Uses STACK to hold Operators and (.
o Will be constructed from Left to Right using
Operands from Q
Operators which are removed from STACK
o Fds
Sweta Bohra
Step 1. PUSH (
Step 2. ADD ) at End of Q
Step 3. Scan Q from Left to Right
Step 4. Repeat Step _ to _ Until Stack is Empty
Step 5. If OPERAND
ADD it to P
Step 6. If (
PUSH (
Step 7. If OPEARTOR
a. Val = POP and ADD to P
Until having same precedence and
higher precedence than Val
b. PUSH Val
Step 8. If )
a. Val = POP and ADD to P
Until ( encountered
b. Remove (; but not add to P
Step 9. Exit
Sweta Bohra
K K
+ + K
L + KL
- - K L+
M - K L+ M
* -* K L+ M
N -* KL+MN
+ + K L + M N*
K L + M N* -
( +( K L + M N *-
O +( KL+MN*-O
^ +(^ K L + M N* - O
P +(^ K L + M N* - O P
) + K L + M N* - O P ^
* +* K L + M N* - O P ^
W +* K L + M N* - O P ^ W
/ +/ K L + M N* - O P ^ W *
U +/ K L + M N* - O P ^W*U
Sweta Bohra
/ +/ K L + M N* - O P ^W*U/
V +/ KL + MN*-OP^W*U/V
* +* KL+MN*-OP^W*U/V/
T +* KL+MN*-OP^W*U/V/T
+ + KL+MN*-OP^W*U/V/T*
KL+MN*-OP^W*U/V/T*+
Q + KL+MN*-OP^W*U/V/T*Q
KL+MN*-OP^W*U/V/T*+Q+
Recursion
int fact(int n)
{
if (n < = 1) // base case
return 1;
else
return n*fact(n-1);
}
int fact(int n)
{
// wrong base case (it may cause
// stack overflow).
if (n == 100)
return 1;
else
return n*fact(n-1);
}
directRecFun();
// Some code...
}
// Driver Code
int main()
{
int test = 3;
printFun(test);
}
Output :
3 2 1 1 2 3
When printFun(3) is called from main(), memory is
allocated to printFun(3) and a local variable test is
initialized to 3 and statement 1 to 4 are pushed on the
stack as shown in below diagram. It first prints ‘3’. In
Sweta Bohra
Factorial
#include <stdio.h>
if(i <= 1) {
return 1;
}
return i * factorial(i - 1);
}
int main() {
int i = 12;
printf("Factorial of %d is %d\n", i,
factorial(i));
return 0;
}
Output
Factorial of 12 is 479001600
Sweta Bohra
Sweta Bohra
Fibonacci Series
The following example generates the Fibonacci series for
a given number using a recursive function –
#include <stdio.h>
int fibonacci(int i) {
if(i == 0) {
return 0;
}
if(i == 1) {
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main() {
int i;
return 0;
}
Tower of Hanoi
Tower of Hanoi is a mathematical puzzle where we have
three rods and n disks. The objective of the puzzle is to move
the entire stack to another rod, obeying the following simple
rules:
void main(){
clrscr();
int n;
cout<<"\n\t\t*****Tower of Hanoi*****\n";
cout<<"\t\tEnter number of discs : ";
cin>>n;
cout<<"\n\n";
tower(n,'A','B','C');
getch();
}
Sweta Bohra
Sweta Bohra
Sweta Bohra
0 A C B
1 A B C A->C
0 C B A
2 A C B A->B
0 B A C
To
Aux
1 B C A B->A
From
0 A C B
3 A B C A->C
0 C B A
1 C A B C->B
0 B A C
2 C B A C->A
0 A C B
1 A B C A->C
0 C B A
Sweta Bohra
Sweta Bohra
QuickSort
Partition Exchange Sort
Application of Stack
It picks an element as pivot and partitions the given array around the picked
pivot.
There are many different versions of quickSort that pick pivot in different
ways.
Target of partitions is
given an array and an element x of array as pivot
put x at its correct position in sorted array
put all smaller elements (smaller than x) before x
put all greater elements (greater than x) after x
Iteration 1:
10 is Pivot
Put 10 (Pivot) to its correct place
Iteration 2:
Now the above method will be applied to both the segment
9 is Pivot
Put 9 (Pivot) to its correct place
Iteration 3:
Now the above method will be applied to both the segment
8 is Pivot
Put 8 (Pivot) to its correct place
Iteration 4:
Now the above method will be applied to both the segment
6 is Pivot
Put 6 (Pivot) to its correct place
QuickSort(LA,BEG,END)
1. If(BEG < END)
2. X=partition(LA,BEG,END)
3. QuickSort(LA,BEG,x-1)
4. QuickSort(LA,x+1,END)
Endif
5. End.
Partition function
Partition(LA,BEG,END)
1. Piv=BEG
2. Repeat while(1)
3. Repeat while(LA[Piv]<=LA[END] and
Piv!=END)
4. END—
[end while]
5. If(Piv = = End)
6. Return Piv
7. Swap(LA[Piv],LA[End])
8. Piv=End
9. Repeat while(LA[Piv]>=LA[BEG]and
Piv!=BEG)
Sweta Bohra
10. BEG++
11. If(Piv= =BEG)
12. Return Piv
13. Swap(LA[Piv],LA[BEG])
14. Piv=BEG
[end while]
15. End.
Swap(LA,BEG,END)
1. Temp=LA[Piv]
2. LA[Piv]=LA[END}
3. LA[END]=Temp
4. End.