0% found this document useful (0 votes)
7 views

Data Structures - Unit 4 Stack and Queue

This document covers the concepts of stacks and queues, including their definitions, operations, and implementations using arrays and linked lists. It explains the Last In First Out (LIFO) principle of stacks, details the push and pop operations, and provides examples of stack usage. Additionally, it discusses arithmetic expressions, Polish notation, and methods for transforming and evaluating expressions.

Uploaded by

shwetadatey873
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Data Structures - Unit 4 Stack and Queue

This document covers the concepts of stacks and queues, including their definitions, operations, and implementations using arrays and linked lists. It explains the Last In First Out (LIFO) principle of stacks, details the push and pop operations, and provides examples of stack usage. Additionally, it discusses arithmetic expressions, Polish notation, and methods for transforming and evaluating expressions.

Uploaded by

shwetadatey873
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 199

Department of CSE

Second Year
Unit 4
Stack And
Queue
LECTURE 1
Contents

Stacks: Sequential Memory Representation of Stack,

Arithmetic expressions: Polish notation.

Quick sort, Recursion, Tower of Hanoi.

Queues: Sequential Memory Representation of Queue, DeQueue, Priority

queues.

3 Prachi V. Kale
Stack
Stack is one of the Linear Data Structure.

It is an ordered group of homogeneous items of elements.

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

The Operations are termed as Push (Insertion) and

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

•Can be implemented by means of Array, Structure, Pointers and Linked List.

•Stack can either be a fixed size or dynamic.

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)

•This procedure pushes an ITEM onto a STACK.

1. [STACK already filled?]

IF TOP = MAXSTACK , then : print :“OVERFLOW” & Return.

2. Set TOP := TOP + 1 [Increase TOP by 1]

3. Set STACK[TOP]:= ITEM [Insert ITEM in new TOP Position]

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

5 MAXSTACK TOP = 3 And BOTTOM=1

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.

1. [STACK has an ITEM to be removed]

IF TOP = 0, then : print :“UNDERFLOW” & Return.

2. Set ITEM := STACK[TOP] [Assign TOP element to ITEM]

3. Set TOP := TOP - 1 [Decrease TOP by 1]

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

4 TOP = 2 And BOTTOM=1 3

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) PUSH ( STACK , Athens ) TOP


=5
London Berlin Rome Paris Athens

1 2 3 4 5 6

27 Prachi V. Kale
Cont.…
POP (STACK , ITEM) TOP = 5

London Berlin Rome Paris Athens

1 2 3 4 5 6

POP = Top i.e.


Athens TOP =
4
London Berlin Rome Paris

1 2 3 4 5 6

28 Prachi V. Kale
Cont.…
POP (STACK , ITEM) TOP = 4

London Berlin Rome Paris

1 2 3 4 5 6

POP = Top i.e. Paris


TOP =
3
London Berlin Rome

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 * +

2) A + B * C + D  ((A + (B * C)) + D )  ((A + (B C*) )+ D)  ((A B C *+) + D)

ABC* +D +

36 Prachi V. Kale
Evaluation of Postfix Expression
Let , P be an Arithmetic Expression.

1) To evaluate this we 1st add “ ) “ Symbol at the end of expression P. To

identify the end of Expression of P.

2) Scan the expression from left to right until “ )” symbol is encountered.

3) While scanning if the Operand is found then add it to STACK.

4) and if we found operators then remove two elements from Stack

5) Put the result back on the TOP of stack

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)!

The factorial function is only defined for positive integers.

n! = 1 i.e. 1! = 1 (if n is equal to 1)

n! = n * (n-1)! (if n is larger than 1)

Example: the Factorial Function

We could write: 0! =1 and

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.…

Here, we see that we start at the top level, factorial(3), and


simplify the problem into 3 x factorial(2).
Now, we have a slightly less complicated problem in factorial(2), and
we simplify this problem into 2 x factorial(1).

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!

1!= 1. 0! Step 4 : 1! In terms of 0!

The base value of the Recursion definition is 0 !

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)

This procedure evaluates N ! And return the value in the variable


FACT

1. If N=0 , then : Set FACT := 1 & Return

2. CALL Factorial (FACT , N-1)

3. Set FACT := N * FACT

4. Return

56 Prachi V. Kale
Fibonacci numbers
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...

where each number is the sum of the preceding two.

Recursive definition:

F(0) = 0;

F(1) = 1;

F(number) = F(number-1)+ F(number-2);

If n=0 , or n=1 the Fn = n

If n > 1 , then Fn = Fn-2 + Fn-1

57 Prachi V. Kale
Fibonacci numbers

The definition refers to itself when it uses Fn-2 & Fn-1

a) Base values are 0 & 1

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

1. If N=0 or N=1 , then : Set FIB := N & Return

2. CALL FIBONACCI (FIBA , N-2)

3. CALL FIBONACCI (FIBB , N-1)

4. Set FIB := FIBA + FIBA

5. Return

59 Prachi V. Kale
60 Prachi V. Kale
Problem on Recursion

◦ Recursion may be used as a tool in developing an algorithm to

◦ solve a particular problem , the problem we pick is known as the

◦ “ Tower Of Hanoi “.

61 Prachi V. Kale
Towers of Hanoi

◦ Only One Disc could be moved at a time


◦ A Larger Disc must never be stacked above a Smaller one
◦ One and only one extra needle could be used for intermediate
storage of discs

62 Prachi V. Kale
Cont.…
A B C

A B C Move top disk from Peg A to


Peg C

63 Prachi V. Kale
Cont.…
A B C
Move top disk from Peg A to
Peg B

C Move top disk from Peg C to


A B
Peg B

64 Prachi V. Kale
Cont.…
A B C
Move top disk from Peg A to
Peg C

C Move top disk from Peg B to


A B
Peg A

65 Prachi V. Kale
A
Cont.…
B C

Move top disk from Peg B to


Peg C

C Move top disk from Peg A to


A B
Peg C

66 Prachi V. Kale
Cont.…
Solution to Tower Of Hanoi problem for

N=1 & N=2

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 ,

TOWER (N - 1 , BEG , END , AUX )

TOWER (1 , BEG , AUX , END) or BEG END

TOWER (N - 1 , AUX , BEG , END)

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

If a < b i.e 2 < 3 condition is True

So value is 0
Q (2, 3) = 0

76 Prachi V. Kale
Recursion Problem
1) Q (14, 3) Here a =14 and b = 3

If a < b i.e 14< 3 condition is False

So evaluate next expression i.e. Q (a – b, b)+1 If b <= a


3 < = 14

Q (a – b, b)+1 = Q (14 – 3, 3)+1


Here a = 11 & b = 3 again If b <= a
Q (11 , 3) +1
3 < = 11

(Q (11, 3)) +1 Evaluate as Q (a – b, b)+1

(Q (11- 3, 3)+1) +1

77 Prachi V. Kale
Recursion Problem
(Q (8, 3)+1) +1 Here a =8 and b = 3

If a < b i.e 8 < 3 condition is False

So evaluate next expression i.e. Q (a – b, b)+1 If b <= a


3<=8

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)) + 2 Evaluate as Q (a – b, b)+1

(Q (8- 3, 3)+1) + 2

78 Prachi V. Kale
Recursion Problem
Q (5, 3)+ 3 Here a =5 and b = 3

If a < b i.e 5 < 3 condition is False

So evaluate next expression i.e. Q (a – b, b)+1 If b <= a


3 < = 5 true

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

2) Q ( 5, 3) Here J =5 and K= 3 3) Q ( 15, 3) Solve it?


If 5>= 3
Q (J – K, K+2)+J
= Q ( 5 -3 , 3+2 ) + 5
= Q ( 2, 5) + 5
Here Q ( 2, 5) i.e J < K
Q (2,5) = 5
i.e. Q (5,3) = 5+5
= 10

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 termed as First In First Out or Last in Last Out Structure.

• The ends are dedicated to perform the particular operation.

• 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

Like check-out lines in a store, a queue has a front and a rear.

Enqueue

Insert an element at the rear of the queue

Dequeue

Remove an element from the front of the queue

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

front front front


Insert(3) Insert(6) Insert(9)

87 Prachi V. Kale
Enqueue (ItemType newItem)
Function: Adds newItem to the rear of the queue.

Preconditions: Queue has been initialized and is not full.

Postconditions: newItem is at rear of queue.

Dequeue (ItemType& item)


Function: Removes front item from queue and returns it in item.

Preconditions: Queue has been initialized and is not empty.

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

Front =1 Front =1 Front =1 Front =1


Rear = 4 Rear = 3 Rear = 2 Rear = 1

89 Prachi V. Kale
Queue

Front =2 Front =2 Front =2


Rear = 4 Rear = 3 Rear = 2

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

•Queue with N=6 memory locations.

• Initially Queue is Empty.

• Queue : FRONT = 0 & REAR


=0
1 2 3 4 5 6

Now A, B, C & then D is inserted the Queue will be


A B C D FRONT = 1 & REAR
=4
1 2 3 4 5 6

92 Prachi V. Kale
Queue
Now A is deleted then the Queue will be

A B C D FRONT = 1 & REAR


=4
1 2 3 4 5 6

B C D
FRONT = 2 & REAR
1 2 3 4 5 6 =4

Now E is inserted then the Queue will be

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

Now if F is inserted then the Queue will be

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

Now if D & E are deleted then the Queue will be

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

Front is not Null

Else REAR = REAR Insert new element at REAR = 2


+ 1 i.e REAR = 1+1
=2

100 Prachi V. Kale


Queue rear

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

101 Prachi V. Kale


Queue rear

A B C D E

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 = 5+1
ITEM = C
=6
Else REAR = REAR Insert new element at REAR = 5
+ 1 i.e REAR = 4+1
ITEM = E Front is 1 So Overflow if new element is
=5 REAR is more than the size
added

102 Prachi V. Kale


Link Implementation
•Array representation is easy , but it is used for fixed size QUEUE.
•Sometimes size of QUEUE is changed during execution , so the solution for this is to
represent QUEUE by Linked List.
The Linked representation of QUEUE uses single Linked List.
The Link is divided into two parts.

Node 1
INFO Link

103 Prachi V. Kale


•The INFO fields of the NODE holds , the element of the QUEUE & LINK field holds
• pointer to the neighboring element in the QUEUE.
•The new item is added from the rear end with the help of AVAIL list
•& deleted from the Front.
•The NULL pointer of the last node in the list signals the bottom of STACK.

FRONT
30 20 10 REAR

104 Prachi V. Kale


DEQUE
•Another type of queue is known as Deque , is a linear list in which elements can be added
or removed at either end but not in the middle.
•Deque is the contraction of the name Double Ended Queue.
•Deque is maintained by a circular array DEQUE with pointers LEFT & RIGHT , which
points to the two ends of the deque.
•The element extent from the left end to the right end in the array.
•We assume a Circular Queue where DEQUE [1] comes after DEQUE [N] in the array.

105 Prachi V. Kale


DEQUE
•There are two types of DEQUE
•1) Input Restricted DEQUE
•2) Output Restricted DEQUE
•1) Input Restricted Deque
Is a queue which allows insertion at only one end of the list but deletion at both the
ends.
•2) Output Restricted Deque
Is a queue which allows deletion at only one end of the list but
insertion at both the ends.
There are various ways to represent deque in the memory and linked list.

106 Prachi V. Kale


DEQUE
•Consider the following deque where DEQUE is allocated 6 memory cells.
•LEFT = 2 and RIGHT=5

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

107 Prachi V. Kale


DEQUE
•2) Two cities are deleted from Right.

1 2 3 4 5 6

•DEQUE : Athens , London , Berlin , Rome , Paris , _________


• Left RIGHT
•Delete Paris & Rome from RIGHT
1 2 3 4 5 6

DEQUE : Athens , London , Berlin , , , _________

LEFT RIGHT

108 Prachi V. Kale


DEQUE
•3) Madrid is added on the right
1 2 3 4 5 6

DEQUE : Athens , London , Berlin , , , _________

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

109 Prachi V. Kale


DEQUE
•4) Moscow is added on the right
Insert the ITEM= Madrid at position 4
1 2 3 4 5 6

DEQUE : Athens , London , Berlin , Madrid , Moscow , _________

LEFT =1
RIGHT = 5

110 Prachi V. Kale


DEQUE
•5) Two cities are deleted from Right.
DEQUE : Athens , London , Berlin , Madrid , Moscow , _________

LEFT =1
RIGHT
1 = 52 3 4 5 6
DEQUE : Athens , London , Berlin , , , _________

LEFT =1 RIGHT = 3

111 Prachi V. Kale


DEQUE
•6) A city is deleted from Left.
DEQUE : Athens , London , Berlin , , , _________

LEFT =1 RIGHT = 3
1 2 3 4 5 6
DEQUE : , London , Berlin , , , _________

LEFT =2 RIGHT = 3

112 Prachi V. Kale


DEQUE
•7) Oslo is added on the Left
1 2 3 4 5 6

DEQUE : Oslo , London , Berlin , , , _________

LEFT =1 RIGHT = 3

113 Prachi V. Kale


Priority Queue
•A priority queue is a collection of elements such that each element has been assigned a

•priority & such that the order in which the elements are deleted & processed comes from

•the following rules.

•Rules :

• 1) An element of higher priority is processed before any element of lower priority.

• 2) Two elements with the same priority are processed according

• to the order in which they were added to the queue

114 Prachi V. Kale


Cont..
•Elements can be added or removed not only at ends but at any position on the queue.

• 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.

• There are various ways to maintain priority queue in memory.

• Link Representation
A 1 B 2 c 2 D 4

G 5 F 4 E 4

115 Prachi V. Kale


Priority Queue- Example
•Consider the priority queue as follows which is maintained by a two dimensional array
QUEUE
• FRON REA 1 2 3 4 5 6
1 T 0 R0
1
2 2 4
2 ccc xxx vvv
3 1 1
3 rrr
4 5 3
4 fff sss uu dd eee
5 4 4 u d
5 gg
g

116 Prachi V. Kale


Priority Queue- Example
•Describe the structure if two elements are deleted.

FRON REA 1 2 3 4 5 6
1 T 0 R0
1
2 4 4
2 vvv
3 1 1
3 rrr
4 5 3
4 fff sss uu dd eee
5 4 4 u d
5 gg
g

117 Prachi V. Kale


Priority Queue- Example
•Describe the structure if after the preceding deletion , the elements (jjj,3) , (kkk,1) , (lll,1)
& (mmm,5) are added to the queue.
• FRON REA 1 2 3 4 5 6
1 T 1 R2
1 kk lll
2 4 4 k
2 vvv
3 1 2
3 rrr jjj
4 5 3
4 fff sss uu dd eee
5 4 5 u d
mm
5 gg
g m

118 Prachi V. Kale


Priority Queue- Example
•Describe the structure if after the preceding insertions , six elements are deleted

FRON REA 1 2 3 4 5 6
1 T 0 R0
1
2 0 0
2
3 1 2
3
4 5 3
4 fff sss uu eee
5 4 5 u mm
5 gg
g m

119 Prachi V. Kale


Example Stack
Suppose the following stack of integers is in memory where STACK is allocated N= 6

Memory cells

TOP = 3 , STACK : 5 , 2 , 3 , _ , _ , _

Find the output of the following program segments.

Call POP (STACK , ITEMA) Repeat while TOP != 0 :


Call POP (STACK , ITEMB) Call POP (STACK , ITEM)
Call PUSH (STACK , Write : ITEM
ITEMB+2)
Call PUSH(STACK , 8) Return
Call PUSH(STACK ,
ITEMA+ITEMB)

120 Prachi V. Kale


Example Stack
Call POP (STACK , ITEMA)
Call POP (STACK , ITEMB)
TOP = 1 , STACK : 5 , , , _ , _ , _

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

121 Prachi V. Kale


Department of CSE

Second Year
Unit 4
Stack And
Queue
LECTURE 6
QUICK SORT
QUICK SORT

This sorting algorithm uses the idea of divide and conquer.

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.

125 Prachi V. Kale


QUICK SORT
Divide

Subproblem1 Subproblem2 Subproblem3


Sorting
Conqu
er
Final List (Sorted List)

126 Prachi V. Kale


QUICK SORT

Three steps

 Find pivot that divides the array into two halves.

 Quick sort the left half.

 Quick sort the right half.

127 Prachi V. Kale


Example

Consider an array having 6 elements

5 2 6 1 3 4

Arrange the elements in ascending order using quick sort


algorithm

128 Prachi V. Kale


This is our unsorted array

Array index 1 2 3 4 5 6

Array element 5 2 6 1 3 4

129 Prachi V. Kale


This is our unsorted array

Array index 1 2 3 4 5 6

Array element 5 2 6 1 3 4

Left

130 Prachi V. Kale


This is our unsorted array

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

131 Prachi V. Kale


This is our unsorted 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

132 Prachi V. Kale


This is our unsorted 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

133 Prachi V. Kale


Pivot
This is our unsorted 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

134 Prachi V. Kale


Initially pointing to the First
element
Pivot
This is our unsorted 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

135 Prachi V. Kale


Initially pointing to the First
element
Pivot
This is our unsorted 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

136 Prachi V. Kale


Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

137 Prachi V. Kale


Remember this rule:

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

138 Prachi V. Kale


Remember this rule:

All element to the RIGHT of pivot be GREATER than pivot.

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

139 Prachi V. Kale


Remember this rule:

All element to the RIGHT of pivot be GREATER than pivot.


All element to the LEFT of pivot be SMALLER than pivot.

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

140 Prachi V. Kale


As the pivot is
pointing at left

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

141 Prachi V. Kale


As the pivot is
pointing at left
So we will start
from right
Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

142 Prachi V. Kale


As the pivot is
pointing at left
So we will start
And move towards left from right
Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

143 Prachi V. Kale


Pivot = 5
Right =4

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

144 Prachi V. Kale


Is Pivot < Right( 5 < 4)
Pivot = 5
Right =4

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

145 Prachi V. Kale


Is Pivot < Right
( 5 < 4) Pivot = 5
Right =4
NO

Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

146 Prachi V. Kale


Is Pivot < Right
( 5 < 4) Pivot = 5
Right =4
NO
So we swap pivot and right
Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

147 Prachi V. Kale


Is Pivot < Right
( 5 < 4) Pivot = 5
Right =4
NO
So we swap pivot and right
Pivot

1 2 3 4 5 6

5 2 6 1 3 4

LEFT Right

148 Prachi V. Kale


Is Pivot < Right
( 5 < 4) Pivot = 5
Right =4
NO
So we swap pivot and right
Pivot

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

149 Prachi V. Kale


Is Pivot < Left
Pivot = 5
Left =4

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Now move pivot to right


LEFT Right

150 Prachi V. Kale


Is Pivot < Left
NO Pivot = 5
Left =4
So we swap pivot to the right

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

151 Prachi V. Kale


Now the pivot is
pointing at right

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

152 Prachi V. Kale


Now the pivot is
pointing at right
So we will start
from left

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

153 Prachi V. Kale


Now the pivot is
pointing at right
So we will start from left
And move
towards right
P
i
v
o
1 2 3 4 t 5 6

4 2 6 1 3 5

LEFT Right

154 Prachi V. Kale


Is Pivot >
Left
( 5 > 4) Pivot = 5
Left =4

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

155 Prachi V. Kale


Is Pivot > Left
( 5 > 4) Pivot = 5
Left =4
YES
Pivot

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

156 Prachi V. Kale


Is Pivot >
Left
( 5 > 4) Pivot = 5
Left =4
YES
So we move left one position towards Pivot
right

1 2 3 4 5 6

4 2 6 1 3 5

LEFT Right

157 Prachi V. Kale


Is Pivot > Left
( 5 > 2) Pivot = 5
Left =2

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

158 Prachi V. Kale


Is Pivot > Left
( 5 > 2) Pivot = 5
Left =2
YES
Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

159 Prachi V. Kale


Is Pivot >
Left
( 5 > 2) Pivot = 5
Left =2
YES
So we move left one position towards Pivot
right

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

160 Prachi V. Kale


Is Pivot >
Left
( 5 > 6) Pivot = 5
Left =6

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

161 Prachi V. Kale


Is Pivot > Left
( 5 > 6) Pivot = 5
Left =6
NO

Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

162 Prachi V. Kale


Is Pivot > Left
( 5 > 6) Pivot = 5
Left =6
NO

So we swap pivot and left Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

163 Prachi V. Kale


Is Pivot > Left
( 5 > 6) Pivot = 5
Left =6
NO

So we swap pivot and left Pivot

1 2 3 4 5 6

4 2 6 1 3 5

Left
Right

164 Prachi V. Kale


Is Pivot >
Left
( 5 > 6) Pivot = 5
Left =6
NO

So we swap pivot and left Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

165 Prachi V. Kale


Is Pivot > Left
( 5 > 6) Pivot = 5
Left =6
NO

So we swap pivot and left Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left Right

And move the pivot to left

166 Prachi V. Kale


Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

167 Prachi V. Kale


Now the pivot is
pointing at left

Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

168 Prachi V. Kale


Now the pivot is
pointing at left So we will start
from right
Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

169 Prachi V. Kale


And move towards left

Now the pivot is


pointing at left So we will start
from right
Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

170 Prachi V. Kale


Is Pivot < Right
( 5 < 6) Pivot = 5
Right =6

Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

171 Prachi V. Kale


Is Pivot < Right
( 5 < 6) Pivot = 5
YES Right =6

Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

172 Prachi V. Kale


Is Pivot < Right
( 5 < 6) Pivot = 5
YES Right =6

Pivot So we move right one position


towards left

1 2 3 4 5 6

4 2 5 1 3 6

Left
Right

173 Prachi V. Kale


Is Pivot <
Right
( 5 < 3) Pivot = 5
Right =3

Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left Right

174 Prachi V. Kale


Is Pivot < Right
( 5 < 3) Pivot = 5
NO Right =3

Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left Right

175 Prachi V. Kale


Is Pivot < Right
( 5 < 3) Pivot = 5
NO Right =3
So we swap pivot and right
Pivot

1 2 3 4 5 6

4 2 5 1 3 6

Left Right

176 Prachi V. Kale


Is Pivot < Right
( 5 < 3) Pivot = 5
NO Right =3
So we swap pivot and right
Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

177 Prachi V. Kale


Is Pivot > Right
( 5 > 3) Pivot = 5
NO Right =3
So we swap pivot and right
Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

And move the pivot to right

178 Prachi V. Kale


Is Pivot > Left
( 5 > 3) Pivot = 5
Left =3

Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

179 Prachi V. Kale


Is Pivot > Left Pivot = 5
( 5 > 3) YES Left =3

Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

180 Prachi V. Kale


Is Pivot > Left Pivot = 5
( 5 > 3) YES Left =3
So we move left one position towards
right
Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

181 Prachi V. Kale


Is Pivot > Left
( 5 > 1) Pivot = 5
Left = 1

Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

182 Prachi V. Kale


Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

183 Prachi V. Kale


Now both left and right are pointing at the same element of the array

Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

184 Prachi V. Kale


Now both left and right are pointing at the same element of the array

This time 5 is the pivot and it is at the sorted position

Pivot

1 2 3 4 6
54
4 2 3 1 55 6

Left Right

185 Prachi V. Kale


Now both left and right are pointing at the same element of the array

This time 5 is the pivot and it is at the sorted position

Elements left of pivot are smaller


Pivot

1 2 3 4 6
54
4 2 3 1 55 6

Left Right

186 Prachi V. Kale


Now both left and right are pointing at the same element of the array

This time 5 is the pivot and it is at the sorted


position
Elements right of
pivot are greater
Elements left of pivot are smaller
Pivot

1 2 3 4 6
54
4 2 3 1 55 6

Left Right

187 Prachi V. Kale


Now both left and right are pointing at the same element of the array

This time 5 is the pivot and it is at the sorted


position
Elements right of
pivot are greater
Elements left of pivot are smaller
Pivot

1 2 3 4 6
54
4 2 3 1 55 6

Left Right
So pivot has divided the array into
two sub array

188 Prachi V. Kale


Now both left and right are pointing at the same element of the array

This time 5 is the pivot and it is at the sorted


position
Elements right of
pivot are greater
Elements left of pivot are smaller
Pivot

1 2 3 4 6
54
4 2 3 1 55 6

Left sub array Right sub array


Left Right
So pivot has divided the array into
two sub array

189 Prachi V. Kale


Now both left and right are pointing at the same element of the array

This time 5 is the pivot and it is at the sorted


position
Elements right of
pivot are greater
Elements left of pivot are smaller
Pivot

1 2 3 4 6
54
4 2 3 1 55 6

Left sub array Right sub array Right


Left
So pivot has divided the array into
two sub array We will now quick sort the
left sub array
190 Prachi V. Kale
Is Pivot < Left
( 4 < 1) Pivot = 4
Left =1

Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

191 Prachi V. Kale


Is Pivot < Left Pivot = 4
( 4 < 1) NO Left =1

Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

192 Prachi V. Kale


Is Pivot < Left Pivot = 4
( 4 < 1) NO Left =1
So we swap pivot and right
Pivot

1 2 3 4 5 6

4 2 3 1 5 6

Left Right

193 Prachi V. Kale


Is Pivot < Left Pivot = 4
( 4 < 1) NO Left =1
So we swap pivot and right
Pivot

1 2 3 4 5 6

1 2 3 4 5 6

Left Right

194 Prachi V. Kale


Is Pivot < Left Pivot = 4
( 4 < 1) NO Left =1
So we swap pivot and right
Pivot

1 2 3 4 5 6

1 2 3 4 5 6

Now move the pivot to right


Left Right

195 Prachi V. Kale


Is Pivot < Left Pivot = 4
( 4 < 1) NO Left =1
So we swap pivot and right
Pivot

1 2 3 4 5 6

1 2 3 4 5 6

Left Right

196 Prachi V. Kale


The Array is Sorted

1 2 3 4 5 6

1 2 3 4 5 6

197 Prachi V. Kale


Complexity of Quick Sort
The Worst Case occurs when the list is sorted. Then the first element will require n comparisons to
recognize that it remains in the first position. Furthermore, the first sublist will be empty, but the second
sublist will have n-1 elements. Accordingly the second element require n-1 comparisons to recognize that it
remains in the second position and so on.

F(n)= n+(n-1)+(n-2)+..............................+2+1=n(n+1)/2 = O(n2)

198
199 Prachi V. Kale

You might also like