DSPPT

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 15

Contents

Stacks

Stack push operations


Take array size is 5 And initially the top value =-1

push(50)

push(70)

push(90)

Top=2 70 50

90

Top=1

70 50

Top=0

50

Stack push operations

push(10)

push(20)

push(20)

Top=4 Top=3 10 90

20

10 90

Top>size-1 stack overflow

70 50

70 50

Stack pop operations


Pop() Pop() Pop()

Top=4

20

10

Top=3

10

90

90

Top=2

90

70 50

70 50

70 50

Top=1

70 50

Stack pop operations


Pop() Pop() Pop()

Top=-1 stack underflow

Top=0

50

Top=-1

Queues

Queue enqueue operations


Take array size is 5 And initially the front =0 and rear=-1 Enqueue(12) Enqueue(14) Enqueue(16)

rear=2 rear=1 rear=0 Front=0


12 14 12

16

14

Front=0

12

Front=0

Queue enqueue operations


Enqueue(18) Enqueue(20)
20

Enqueue(22)

rear=4
18

rear=3

18

rear=5 and rear>size queue overflow

16

16

14

14

12

12

Front=0

Front=0

Queue dequeue operations


dequeue() dequeue() rear=4
20

rear=4

20

rear=4

20

18

18

18

16

16

Front=2

16

14

Front=1

14

Front=0

12

Queue dequeue operations


dequeue() dequeue() dequeue() Front=5
20

dequeue()

rear=4
Front=4

20

rear=4

Front>rear queue underflow

Front=3

18

Bubble Sort
Given Array of size 5 5 4 3 2 1

Iteration 1: 5>4 (a) 4 5>3 (b) 4 5>2 (c) 4 5>1 (d) 4

5 3 3 3

3 5 2 2

2 2 5 1

1 1 1 5 For first iteration (n-1) comparisons will occur i.e 5-1=4 comparisons

Bubble Sort
Now Array 4 3 2 1 5

Iteration 2: 4>3 (a) 3 4 4>2 (b) 3 2 4>1 (c) 3 2

4
1

1
4

5
5

For second iteration (n-2) comparisons will occur i.e. 5-2=3 comparisons Like that highest element will be set first Next, the 2nd highest element.

Bubble Sort
Now Array 3 2 1 4 5

Iteration 3: 3>2 (a) 2 3 3>1 (b) 2 1

1 3

4 4

5 5

Now Array 2 1

Iteration 4: 2>1 (a) 1 2

Insertion Sort
Given Array of size 5 5 4 3 2 1

Iteration 1: 5 4 3 2 1 Sorted by itself

You might also like