Data Structures - Unit 4 Stack and Queue
Data Structures - Unit 4 Stack and Queue
Second Year
Unit 4
Stack And
Queue
LECTURE 1
Contents
queues.
3 Prachi V. Kale
Stack
Stack is one of the Linear Data Structure.
It has the restriction that insertions and deletions can be performed in only
one position, namely, the end of the list, called the Top.
Elements are added to and removed from the Top of the stack
(the most recently added items are at the top of the stack).
4 Prachi V. Kale
Cont.…
The Last element to be added is the First to be removed (LIFO: Last In, First Out)
or
The First element to be added is the Last to be removed (FILO: First In, Last Out)
It has the restriction that insertions and deletions can be performed in only one The
Pop (Deletion)
5 Prachi V. Kale
Examples.…
6 Prachi V. Kale
Cont.…
A list for which Insert and Delete are allowed only at one end of the list (the top)
LIFO – Last in, First out
Pop Pop
Push
7 Prachi V. Kale
Stack Representation
8 Prachi V. Kale
Array Implementation
•We use the Linear Array or Linked List to represent the STACK.
•It uses pointer variable TOP element of the STACK & a variable MAXSTACK
which gives the maximum number of elements that can be held by the STACK.
TOP = 0 or TOP = NULL 5 MAXSTACK
will indicate that the STACK is empty. 4
3 30 TOP
• As there are three elements TOP = 3 2 20
• MAXSTACK = 5 1 BOTTOM
10
• We can add three more elements
9 Prachi V. Kale
Link Implementation
•Array representation is easy , but it is used for fixed size STACK.
•Sometimes size of STACK is changed during execution , so the solution for this is to
represent STACK by Linked List.
The Linked representation of STACK uses single Linked List.
The Link is divided into two parts.
Node 1
INFO Link
10 Prachi V. Kale
•The INFO fields of the NODE holds , the element of the STACK & LINK field holds
• pointer to the neighboring element in the STACK.
•The new item is added & deleted only at the beginning of the STACK.
•The START pointer of the Linked List behaves as the TOP pointer variable of the
STACK.
•The NULL pointer of the last node in the list signals the bottom of STACK.
Start / TOP
30 20 10 NULL
TO BOTTO
P M
11 Prachi V. Kale
Operations On Stack
push
pop
create
STACK
isEmpty
isFull
12 Prachi V. Kale
Cont.…
•In the Array implementation, we would:
• Declare an array of fixed size (which determines the maximum size of the stack).
• Keep a variable which always points to the “top” of the stack.
• Contains the array index of the “top” element.
•In the Linked List implementation, we would:
• Maintain the stack as a linked list.
• A pointer variable top points to the start of the list.
• The first element of the linked list is considered as the stack top.
13 Prachi V. Kale
PUSH
• Function: Adds newItem to the top of the stack.
• Preconditions: Stack has been initialized and is not full.
• Postconditions: newItem is at the top of the stack.
POP
• Function: Removes topItem from stack and returns it in item.
• Preconditions: Stack has been initialized and is not empty.
• Postconditions: Top element has been removed from stack and item is a copy of
the removed element.
14 Prachi V. Kale
Cont.…
• Stack Overflow
The condition resulting from trying to push an element onto a full stack.
if(!stack.IsFull())
stack.Push(item);
• Stack Underflow
The condition resulting from trying to pop an empty stack.
if(!stack.IsEmpty())
stack.Pop(item);
15 Prachi V. Kale
Algorithm
•PROCEDURE : PUSH(STACK,TOP,MAXSTACK,ITEM)
4. Return
16 Prachi V. Kale
TOP=4
Cont.…
TOP = 2
TOP = 1
TOP = 0
TOP = -1
17 Prachi V. Kale
Example
•Consider the STACK , where it contains 10,20 & 30 elements in the STACK
•TOP of the STACK is 3. Now using PUSH procedure we want to
insert 40 in STACK.
MAXSTACK= 5
4 ITEM = 40 to be inserted
3 30 TOP
2 20
1 BOTTOM
10
18 Prachi V. Kale
Example
TOP=TOP+1
TOP = 3+1 = 4
STACK[TOP]=STACK[4]= 40
5 MAXSTACK
4 40 TOP
3 30 TOP
2 20
1 BOTTOM
10
19 Prachi V. Kale
Algorithm
•PROCEDURE : POP(STACK, MAXSTACK , ITEM)
• This procedure deletes the TOP element of STACK and assigns it to the variable
ITEM.
4. Return
20 Prachi V. Kale
Cont.…
POP
TOP=3
TOP=2
TOP=1
TOP=0
TOP = -1
21 Prachi V. Kale
Example
•Consider the STACK , where it contains 10,20 & 30 elements in the STACK
•TOP of the STACK is 3 , Which is not NULL
• ITEM = STACK[TOP]
ITEM=30 which is the element 5
5 MAXSTACK to be removed 4
3 30 TOP 2 20 TOP
2 20 1
10
1 BOTTOM
10
22 Prachi V. Kale
Summary
• Push
– Add an element to the top of the stack
• Pop
– Remove the element at the top of the stack
Empty Push Element Push Another po
Stack p
top
B
top
top top A A
A
23 Prachi V. Kale
Applications of Stack
• Page-visited history in a Web browser
• Undo sequence in a text editor
• Chain of method calls in the Java Virtual Machine
• Validate XML
24 Prachi V. Kale
Department of CSE
Second Year
Unit 4
Stack And
Queue
LECTURE 2
Example
Consider the following Stack of City names
STACK : London , Berlin , Rome , Paris , ________ , _______
Describe stack as following operations takes place.
TOP =
4
London Berlin Rome Paris
1 2 3 4 5 6
1 2 3 4 5 6
27 Prachi V. Kale
Cont.…
POP (STACK , ITEM) TOP = 5
1 2 3 4 5 6
1 2 3 4 5 6
28 Prachi V. Kale
Cont.…
POP (STACK , ITEM) TOP = 4
1 2 3 4 5 6
1 2 3 4 5 6
29 Prachi V. Kale
Cont.…
PUSH (STACK , Madrid) TOP =
3
London Berlin Rome
1 2 3 4 5 6
TOP =
4
London Berlin Rome Madrid
1 2 3 4 5 6
30 Prachi V. Kale
Cont.…
PUSH (STACK , Moscow)
TOP =
M London Berlin Rome
4
Madrid
1 2 3 4 5 6
TOP =
5
London Berlin Rome Madrid Mosco
w
1 2 3 4 5 6
31 Prachi V. Kale
Arithmetic Expression & Polish Notations
An Arithmetic Expression consists of Operands & Operators.
The Operands are variables &
Operators are Unary & Binary Operators.
The three levels of Precedence for the usual five Binary Operations
Highest : Exponential ( )
Next Highest : Multiplication ( * ) and Division ( / )
Lowest : Addition ( + ) and Subtraction ( - )
32 Prachi V. Kale
Arithmetic Expression & Polish Notations
The operations on the same level are performed from Left to Right.
Exponentiation are performed from right to left.
Ex. 2 3 +5 * 2 2 - 12 / 6
First execute 23 which is 8 (2*2*2) and 22 which is 4 (2*2)
8+ 5 * 4 - 12 / 6
then execute 5 * 4 = 20
Next 12 / 6 = 2
8 + 20 - 2
Now 8 + 20 =28 and then 28-2=26
so final answer is 26
33 Prachi V. Kale
Polish Notations
•There are three Polish Notations i.e. Infix , Postfix and Prefix
•Infix: Operators placed between operands:
(A+B)*C
•Postfix: Operands appear before their operators
AB+*C
AB+C*
•There are no precedence rules to learn in postfix notation, and parentheses are never needed
•Prefix: Operators appear before Operands
+AB*C
*+ABC
34 Prachi V. Kale
Assignment
•Find Postfix and Prefix
1) A+(B*C)
2) (A+B)/(C-D)
35 Prachi V. Kale
More Examples
Infix Postfix
A+B AB+
A+B*C ABC*+
(A + B) * C AB+C*
A+B*C+D ABC*+D+
(A + B) * (C + D) AB+CD+*
A*B+C*D AB*CD*+
1) A + B * C (A + (B * C)) (A + (B C *) ) A B C * +
ABC* +D +
36 Prachi V. Kale
Evaluation of Postfix Expression
Let , P be an Arithmetic Expression.
37 Prachi V. Kale
Example without Stack
P = 4, 5 , + , 7 , 2 , - , * )
38 Prachi V. Kale
Using STACK
P = 4, 5 , + , 7 , 2 , - , * )
39 Prachi V. Kale
Questions
1) Evaluate 6 5 2 3 + 8 * + 3 + *
2) Evaluate 5 6 2 + * 12 4 / -
3) Evaluate 5 3 + 2 * 6 9 7 - / -
40 Prachi V. Kale
Transforming Infix to postfix Expression
•Use a stack for processing operators (Push and Pop operations).
•Scan the sequence of operators and operands from left to right and perform one of the
following:
• Output the operand,
• Push an operator of higher precedence,
• Pop an operator and output, till the stack top contains operator of a lower precedence
and push the present operator.
41 Prachi V. Kale
Algorithmic Steps
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming operator onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators until you see a left
parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on the stack.
6. If the incoming symbol has equal precedence with the top of the stack, use association. If the association
is left to right, pop and print the top of the stack and then push the incoming operator. If the association
is right to left, push the incoming operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the stack, pop the stack and
print the top operator. Then test the incoming operator against the new top of stack.
8. At the end of the expression, pop and print all operators on the stack.
9. (No parentheses should remain.)
42 Prachi V. Kale
Cont.…
Requires operator precedence information
Operands:
Add to postfix expression.
Close parenthesis:
Pop stack symbols until an open parenthesis appears.
Operators:
Pop all stack symbols until a symbol of lower precedence appears. Then push the operator.
End of input:
Pop all remaining stack symbols and add to the expression.
43 Prachi V. Kale
Cont.…
Current Operator Postfix string
Expression: symbol Stack
(
A * (B + C * D) + E 1 A A
2 * * A
A * (B + C * D) + E )
3 ( *( A
becomes 4 B *( AB
5 + *(+ AB
ABC D * +* E+
6 C *(+ ABC
7 * *(+* ABC
Postfix notation 8 D *(+* ABCD
is also called as
9 ) * ABCD*+
Reverse Polish
Notation (RPN) 10 + + ABCD*+*
11 E + ABCD*+*E
12 ) ABCD*+*E+
44 Prachi V. Kale
Current Symbol Operator Stack Postfix String ( P )
1 A A
2 + + A
Expression: 3 ( +( A
4 B AB
Q : A + (B * C –(D/E F ) * G ) *H 5 * +(* AB
6 C +(* ABC
Q : A + (B * C –(D/E F ) * G ) *H ) 7 - +(- ABC*
8 ( +(-( ABC*
becomes 9 D +(-( ABC* D
10 / +(-( / ABC* D
P:ABC *D EF /G* -H*+ 11 E +(-( / ABC* DE
12 +(-( / ABC* DE
- Is having lower 13 F +(-( / ABC* DEF
precedence than *
14 ) +(- ABC* DEF /
so Pop * and push -
15 * +(-* ABC* DEF /
16 G +(-* ABC* DEF / G
17 ) + ABC* DEF / G*-
18 * +* ABC* DEF / G*-
19 H ABC* DEF / G*-H*+
45 Prachi V. Kale
Department of CSE
Second Year
Unit 4
Stack And
Queue
LECTURE 3
Recursion
Recursion is useful for problems that can be represented by a simpler version of the same problem.
48 Prachi V. Kale
Factorial Function
We can express the Factorial Function as follows:
n! = n * (n-1)!
6! = 6 * 5!
6! = 6 * 5 * 4 * 3 * 2 * 1
49 Prachi V. Kale
Cont.…
To see how the recursion works, let’s break down the factorial function to solve
factorial(3)
50 Prachi V. Kale
Cont.…
51 Prachi V. Kale
Cont.…
We continue this process until we are able to reach a problem that has a
known solution.
In this case, that known solution is factorial(0) = 1 (BASE CASE).
The functions then return in reverse order to complete the solution.
int factorial(n)
{
if(n == 0) //Base Case
return 1;
else
return n * factorial(n-1);
//Recursion
}
52 Prachi V. Kale
EVALUATION OF FACTORIAL EXAMPLE
To evaluate Factorial(3)
evaluate 3 * Factorial(2)
To evaluate Factorial(2)
evaluate 2 * Factorial(1)
To evaluate Factorial(1)
evaluate 1 * Factorial(0)
Factorial(0) is 1
Return 1
Evaluate 1 * 1
Return 1
Evaluate 2 * 1
Return 2
Evaluate 3 * 2
Return 6
53 Prachi V. Kale
Recursive Programming
main
factorial(3) return 6
factorial(3)
3 * factorial(2)
return 2
factorial(2)
2*factorial(1)
return 1
factorial(1)
1*factorial(0)
return 1
factorial(0)
54 Prachi V. Kale
Calculate 4! Using Recursion
4! = 4 . 3! Step 1 : This defines 4! In terms of 3!
3! = 3 . 2! Step 2 : 3! In terms of 2!
2! = 2. 1! Step 3 : 2! In terms of 1!
0!=1
1!= 1. 1 = 1
2! = 2. 1! = 2 . 1 =2
3! = 3 . 21= 3. 2 = 6
4! = 4 . 3! = 4 . 6 = 24
55 Prachi V. Kale
PROCEDURE : Factorial (FACT , N)
4. Return
56 Prachi V. Kale
Fibonacci numbers
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
Recursive definition:
F(0) = 0;
F(1) = 1;
57 Prachi V. Kale
Fibonacci numbers
b) The value of Fn is defined in terms of smaller values of n which are closer to the base Values
58 Prachi V. Kale
PROCEDURE : FIBONACCI (FIB , N)
This procedure evaluates Fn And return the value in the first parameter FIB
5. Return
59 Prachi V. Kale
60 Prachi V. Kale
Problem on Recursion
◦ “ Tower Of Hanoi “.
61 Prachi V. Kale
Towers of Hanoi
62 Prachi V. Kale
Cont.…
A B C
63 Prachi V. Kale
Cont.…
A B C
Move top disk from Peg A to
Peg B
64 Prachi V. Kale
Cont.…
A B C
Move top disk from Peg A to
Peg C
65 Prachi V. Kale
A
Cont.…
B C
66 Prachi V. Kale
Cont.…
Solution to Tower Of Hanoi problem for
N= 1 : A C
N=2:A B ,A C,B C
67 Prachi V. Kale
Cont.…
TOWER (N , BEG , AUX , END) is a procedure which move the top n disks from
the initial Peg BEG to the final Peg END using the Peg AUX as an Auxiliary .
When n > 1 ,
68 Prachi V. Kale
TOWER (4 , BEG , AUX , END)
A B C
TOWER (4, A , B , C)
69 Prachi V. Kale
TOWER (1, A , C , B) A B
TOWER (2, A , B , C) A C A C
TOWER (1, B , A , C) B C
TOWER (3, A , C , B) A B A B
TOWER (1, C, B , A) C A
TOWER (2, C , A , B) C B C B
TOWER (1, A , C , B) A B
TOWER (4, A , B , C) A C A C
TOWER (1, B , A , C) B C
TOWER (2, B , C , A) B A B A
TOWER (1, C , B , A) C A
TOWER (3, B , A , C) B C B C
TOWER (1, A , C , B) A B
TOWER (2, A , B , C) A C A C
TOWER (1, B , A , C) B C
70 Prachi V. Kale
Cont.…
The recursion Solution for n= 4 disks consists of the following 15 moves
A B
A C
B C
A B
C A
C B
A B
A C
B C
B A
C A
B C
A B
A C
B C
71 Prachi V. Kale
Summary
A recursive solution solves a problem by solving a smaller instance of the
same problem.
It solves this new problem by solving an even smaller instance of the same
problem.
Eventually, the new problem will be so small that its solution will be either
obvious or known.
This solution will lead to the solution of the original problem.
72 Prachi V. Kale
Department of CSE
Second Year
Unit 4
Stack And
Queue
LECTURE 4
Recursion Problem
Let a & b denotes +ve integers .Suppose a function Q is defined recursively .
0 If a < b
Q (a, b) =
Q (a – b, b)+1 If b <= a
75 Prachi V. Kale
Recursion Problem
1) Q (2, 3) Here a =2 and b = 3
So value is 0
Q (2, 3) = 0
76 Prachi V. Kale
Recursion Problem
1) Q (14, 3) Here a =14 and b = 3
(Q (11- 3, 3)+1) +1
77 Prachi V. Kale
Recursion Problem
(Q (8, 3)+1) +1 Here a =8 and b = 3
Q (a – b, b)+1 = Q (8 – 3, 3)+2
Here a = 8 & b = 3 again If b <= a
Q (8, 3) + 2
3<=8
(Q (8- 3, 3)+1) + 2
78 Prachi V. Kale
Recursion Problem
Q (5, 3)+ 3 Here a =5 and b = 3
Q (a – b, b)+1 = (Q (5 – 3, 3)+1)+3
Here a = 2 & b = 3 again If a<b true
Q (2, 3) + 4
So output is 0
=0 + 4 =4
Q ( 14 , 3) = 4
79 Prachi V. Kale
Recursion Problem
Let J & K be integers .Suppose a function Q(J , K) is defined recursively .
5 If J < K
Q (J, K) =
Q (J – K, K+2)+JIf J >= K
1) Q ( 2, 7)
2) Q ( 5, 3)
80 Prachi V. Kale
Recursion Problem
1) Q ( 2, 7)
Q ( J, K) = Q ( 2, 7) Here J =2 and K= 7
If J < K
Here 2 < 7
Q(2 ,7 ) = 5
81 Prachi V. Kale
Queue
Queue is an abstract data structure, somewhat similar to Stacks. Unlike stacks, a queue is open at both its
ends.
It is an ordered group of homogeneous items of elements.
Queues have two ends:
Elements are added at one end.
Elements are removed from the other end.
The element added first is also removed first (FIFO: First In, First Out).
• One end is always used to insert data (enqueue) called as Rear and the other is used to remove data
Front (dequeue).
82 Prachi V. Kale
Queue
• A Queue is a Data Structures in which insertion can be done at one end and deletion can be done at the
other end.
• It is a Single Queue.
• Types of Queue
• Linear Queue (Single Queue)
• DEQUE (Double Ended Queue)
• Priority Queue
83 Prachi V. Kale
Queue
• A Queue can also be implemented using Arrays, Linked-lists, Pointers and Structures.
Remove Insert
(Dequeue) front rear (Enqueue)
84 Prachi V. Kale
Operations on Queue
Primary queue operations: Enqueue and Dequeue
Enqueue
Dequeue
85 Prachi V. Kale
Operations on Queue
enqueue
dequeue
create
QUEUE
isEmpty
size
86 Prachi V. Kale
Queue
When enqueuing, the front index is always fixed and the rear index moves forward in the array.
rear rear rear
3 3 6 3 6 9
87 Prachi V. Kale
Enqueue (ItemType newItem)
Function: Adds newItem to the rear of the queue.
Postconditions: Front element has been removed from queue and item is a copy of removed element.
88 Prachi V. Kale
Queue
Front =0
Rear = 0
89 Prachi V. Kale
Queue
90 Prachi V. Kale
Queue
Front =3 Front =3
Front =3 Rear = 4 Rear = 3
Rear = 5
91 Prachi V. Kale
Queue
•Queue may be maintained by Circular array
92 Prachi V. Kale
Queue
Now A is deleted then the Queue will be
B C D
FRONT = 2 & REAR
1 2 3 4 5 6 =4
A BB C DD E
FRONT = 2 & REAR
1 2 3 4 5 6 =5
93 Prachi V. Kale
Queue
Now if B & C are deleted then the Queue will be
B C D E
FRONT = 2 & REAR
1 2 3 4 5 6 =5
D E
B FRONT = 4 & REAR
1 2 3 4 5 6 =5
A B D D E F
FRONT = 4 & REAR
1 2 3 4 5 6 =6
94 Prachi V. Kale
Queue
Now if G is inserted then the Queue will be
G D E F
FRONT = 4 & REAR
1 2 3 4 5 6 =1
Now if H is inserted then the Queue will be
G H D E F
FRONT = 4 & REAR
1 2 3 4 5 6 =2
G H F
FRONT = 6 & REAR
1 2 3 4 5 6 =2
95 Prachi V. Kale
Queue
Procedure : QINSERT(QUEUE , N, FRONT , REAR , ITEM)
This procedure inserts an element ITEM into a queue.
1.[Queue already filled?]
If FRONT = 1 and REAR = N , or if
FRONT = REAR + 1 , then : Write ; “OVERFLOW” & Return
2. [Find new value of REAR]
If FRONT:= NULL , then :
Set FRONT := 1 and REAR := 1
Else if REAR := N , then :
Set REAR := 1
Else :
Set REAR := REAR + 1
[End of If Structure]
3. Set QUEUE[REAR] : = ITEM [This insert new element]
4. Return
96 Prachi V. Kale
Queue
Procedure : QDELETE(QUEUE , N, FRONT , REAR , ITEM)
This procedure deletes an element from a queue & assign it to the variable ITEM.
1.[Queue already empty?]
If FRONT = NULL, then : Write ; “UNDERFLOW” & Return
2. Set ITEM : = QUEUE[FRONT]
3. [Find new value of FRONT]
If FRONT:= REAR, then : . [Queue has only 1 element to start]
Set FRONT := NULL and REAR := NULL
Else if FRONT:= N , then :
Set FRONT := 1
Else :
Set FRONT:= FRONT + 1
[End of If Structure]
4. Return
97 Prachi V. Kale
Department of CSE
Second Year
Unit 4
Stack And
Queue
LECTURE 5
Examplerear
of Queue
rear
1 2 3 4 5 1 2 3 4 5
front
front
ITEM = A
A B C D
1 2 3 4 5
front
Front is not Null
Else REAR = REAR Insert new element at REAR =
Front is not Null + 1 i.e REAR = 2+1
ITEM = C
=3
Else REAR = REAR Insert new element at REAR = 2
+ 1 i.e REAR = 1+1
ITEM = B Front is not Null
=2 Else REAR = REAR Insert new element at REAR = 4
+ 1 i.e REAR = 3+1
ITEM = D
=4
A B C D E
1 2 3 4 5
Node 1
INFO Link
FRONT
30 20 10 REAR
1 2 3 4 5 6
•DEQUE : __________ , London , Berlin , Rome , Paris , _________
• Describe the deque including LEFT & RIGHT as the following operations take place.
• 1) Athens is added on Left.
•DEQUE : Athens , London , Berlin , Rome , Paris , _________
• Left RIGHT
1 2 3 4 5 6
LEFT RIGHT
LEFT=1 RIGHT = 3
Insert
1 the ITEM=
2 Madrid
3 at position
4 4 5 6
DEQUE : Athens , London , Berlin , Madrid , , _________
LEFT =1
RIGHT = 4
LEFT =1
RIGHT = 5
LEFT =1
RIGHT
1 = 52 3 4 5 6
DEQUE : Athens , London , Berlin , , , _________
LEFT =1 RIGHT = 3
LEFT =1 RIGHT = 3
1 2 3 4 5 6
DEQUE : , London , Berlin , , , _________
LEFT =2 RIGHT = 3
LEFT =1 RIGHT = 3
•priority & such that the order in which the elements are deleted & processed comes from
•Rules :
• A prototype of Priority Queue is a Time Sharing System , programs of High Priority are
processed 1st & programs with the same priority form a standard queue.
• Link Representation
A 1 B 2 c 2 D 4
•
G 5 F 4 E 4
Memory cells
TOP = 3 , STACK : 5 , 2 , 3 , _ , _ , _
ITEMA = 3
STACK : 5 ,4, 8, _ , _ , _ TOP = 3
ITEMB = 2
STACK : 5 ,4,8, 5
_ ,_,_ TOP = 4
Call PUSH(STACK , ITEMB+2)
Call PUSH(STACK ,
ITEMA+ITEMB)
ITEMA+ITEMB = 3 + 2 =
ITEMB+2= 2 + 2 = 4
5
STACK : 5 ,4, , _ , _ , _ TOP = 2 While loop will pop all the
elements as
Call PUSH(STACK , 8) Write : 5 , 8 , 4 , 5
Second Year
Unit 4
Stack And
Queue
LECTURE 6
QUICK SORT
QUICK SORT
It finds the element called pivot which divides the array into
two halves in such a way that elements in the left half are
smaller than pivot and elements in the right half are greater
than pivot.
Three steps
5 2 6 1 3 4
Array index 1 2 3 4 5 6
Array element 5 2 6 1 3 4
Array index 1 2 3 4 5 6
Array element 5 2 6 1 3 4
Left
Array index 1 2 3 4 5 6
Array element 5 2 6 1 3 4
Left
Initially pointing to the First
element of the array
Array index 1 2 3 4 5 6
Array element 5 2 6 1 3 4
Left Right
Initially pointing to the First
element of the array
Array index 1 2 3 4 5 6
Array element 5 2 6 1 3 4
Left Right
Initially pointing to the First Initially pointing to the Last
element of the array element of the array
Array index 1 2 3 4 5 6
Array element 5 2 6 1 3 4
Left Right
Initially pointing to the First Initially pointing to the Last
element of the array element of the array
Array index 1 2 3 4 5 6
5 2 6 1 3 4
Array element
Left Right
Initially pointing to the First Initially pointing to the Last
element of the array element of the array
1 2 3 4 5 6
Array index
5 2 6 1 3 4
Array element
We will
Left Right
quick
sort this
Initially pointing to the First Initially pointing to the Last
element of the array array element of the array
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
Pivot
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
1 2 3 4 5 6
5 2 6 1 3 4
LEFT Right
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
4 2 6 1 3 5
LEFT Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
1 2 3 4 5 6
4 2 6 1 3 5
LEFT Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
Pivot
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
1 2 3 4 5 6
4 2 6 1 3 5
Left
Right
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
1 2 3 4 5 6
4 2 5 1 3 6
Left Right
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
Pivot
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
Pivot
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
Pivot
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
1 2 3 4 5 6
4 2 5 1 3 6
Left
Right
Pivot
1 2 3 4 5 6
4 2 5 1 3 6
Left Right
Pivot
1 2 3 4 5 6
4 2 5 1 3 6
Left Right
1 2 3 4 5 6
4 2 5 1 3 6
Left Right
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
Pivot
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
Pivot
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
Pivot
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
Pivot
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
Pivot
1 2 3 4 6
54
4 2 3 1 55 6
Left Right
1 2 3 4 6
54
4 2 3 1 55 6
Left Right
1 2 3 4 6
54
4 2 3 1 55 6
Left Right
1 2 3 4 6
54
4 2 3 1 55 6
Left Right
So pivot has divided the array into
two sub array
1 2 3 4 6
54
4 2 3 1 55 6
1 2 3 4 6
54
4 2 3 1 55 6
Pivot
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
Pivot
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
1 2 3 4 5 6
4 2 3 1 5 6
Left Right
1 2 3 4 5 6
1 2 3 4 5 6
Left Right
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
Left Right
1 2 3 4 5 6
1 2 3 4 5 6
198
199 Prachi V. Kale