100% found this document useful (1 vote)
44 views

Unit 5: Linear Data Structure - Queues

The document discusses queues as a linear data structure. It defines queues as first-in, first-out collections where elements are added to the rear and removed from the front. The queue abstract data type supports operations like enqueue, dequeue, front, size, and isEmpty. Arrays and linked lists can represent queues. Applications of queues include waiting lists, data transfer buffers, playlists, and round robin scheduling.

Uploaded by

Rithik
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
44 views

Unit 5: Linear Data Structure - Queues

The document discusses queues as a linear data structure. It defines queues as first-in, first-out collections where elements are added to the rear and removed from the front. The queue abstract data type supports operations like enqueue, dequeue, front, size, and isEmpty. Arrays and linked lists can represent queues. Applications of queues include waiting lists, data transfer buffers, playlists, and round robin scheduling.

Uploaded by

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

Unit 5_Linear Data Structure: Queue 27-10-2021

UNIT 5

LINEAR DATA STRUCTURE – QUEUES

6 HOURS

DR. NEEPA SHAH 1

SYLLABUS CONTENT

 Queue as an ADT
 Operations
 Array and Linked List representation of Queue with corresponding analysis
 Linear Queue
 Circular Queue
 Double Ended Queue (DEQUE)
 Priority Queue
 Applications of Queue

DR. NEEPA SHAH 2

Dr. Neepa Shah 1


Unit 5_Linear Data Structure: Queue 27-10-2021

QUEUES

DR. NEEPA SHAH 3

QUEUES

 Queue: a collection whose elements are added at one end (the rear or tail of the
queue) and removed from the other end (the front or head of the queue)
 A queue is a FIFO (first in, first out) data structure
 Any waiting line is a queue:
 The check-out line at a grocery store
 The cars at a stop light
 An assembly line

DR. NEEPA SHAH 4

Dr. Neepa Shah 2


Unit 5_Linear Data Structure: Queue 27-10-2021

CONCEPTUAL VIEW OF A QUEUE

Adding an element Front of queue

New element is added


to the rear of the queue

DR. NEEPA SHAH 5

CONCEPTUAL VIEW OF A QUEUE

Removing an element
New front element of queue

Element is removed
from the front of the
DR. NEEPA SHAH queue 6

Dr. Neepa Shah 3


Unit 5_Linear Data Structure: Queue 27-10-2021

THE QUEUE ADT

 Main queue operations:

 enqueue(int): inserts an element at the end of the queue

 int dequeue(): removes and returns the element at the front of the queue

 Auxiliary queue operations:

 int front(): returns the element at the front without removing it

 int size(): returns the number of elements stored

 int isEmpty(): indicates whether no elements are stored

DR. NEEPA SHAH 7

QUEUE EXAMPLE
Operation Output
enqueue(5) – (5)
enqueue(3) – (5, 3)
dequeue() 5 (3)
enqueue(7) – (3, 7)
dequeue() 3 (7)
front() 7 (7)
dequeue() 7 ()
dequeue() “error” ()
isEmpty() true
enqueue(9) – (9)
enqueue(7) – (9, 7)
size() 2 (9, 7)
enqueue(3) – (9, 7, 3)
enqueue(5) – (9, 7, 3, 5)
dequeue() 9 (7, 3, 5)
DR. NEEPA SHAH 8

Dr. Neepa Shah 4


Unit 5_Linear Data Structure: Queue 27-10-2021

APPLICATIONS OF QUEUE

1. As waiting lists for a single shared resource like printer, disk, CPU. (Web, file, ftp, database, mail, etc.)

2. To transfer data asynchronously e.g., pipes, file IO, sockets.

3. As buffers on MP3 players and portable CD players, iPod playlist.

4. In Playlist for jukebox to add songs to the end, play from the front of the list.

5. Queues are used in OS for handling interrupts.


When programming a real-time system that can be interrupted, for ex, by a mouse click, it is necessary to process
the interrupts immediately before proceeding with the current job. If the interrupts have to be handled in the
order of arrival, then a FIFO queue is the appropriate data structure.

DR. NEEPA SHAH 9

APPLICATIONS OF QUEUE CONTD.

6. As buffer for Keyboard input


7. The SSH Secure Shell and SFTP clients
8. Josephus Problem
9. In simulation studies, where the goal is to reduce waiting times:
 Optimize the flow of traffic at a traffic light
 Determine number of cashiers to have on duty at a grocery store at different times of day

DR. NEEPA SHAH 10

Dr. Neepa Shah 5


Unit 5_Linear Data Structure: Queue 27-10-2021

APPLICATION OF QUEUE: ROUND ROBIN SCHEDULERS

We can implement a round robin scheduler using a queue, Q, by repeatedly performing


the following steps:
1. e=dequeue()
The Queue
2. Service element e
3. enqueue(e) 1. Deque the
next element
2. Service the
next element
3. Enqueue the
serviced element

Shared
Service

DR. NEEPA SHAH 11

TYPES OF QUEUE

 There are many different way to implement a Queue:


 Simple Queue/Linear Queue
 Circular Queue
 Double Ended Queue (Deque)
 Priority Queue

DR. NEEPA SHAH 12

Dr. Neepa Shah 6


Unit 5_Linear Data Structure: Queue 27-10-2021

ARRAY-BASED QUEUE

 Two variables keep track of the front and rear


front index of the front element
rear index immediately past the rear element
 Array location rear is kept empty

normal configuration
Q
0 1 2 f r

DR. NEEPA SHAH 13

ARRAY-BASED QUEUE CONTD.


 Queues can be easily represented using arrays
 Every queue has front and rear variables that point to the position from where deletions and insertions can be
done, respectively.

12 9 7 18 14 36

0 1 2 3 4 5 6 7 8 9

 Here, front = 0 and rear = 5


 If we want to add one more value in the list say with value 45, then rear would be incremented by 1 and the value
would be stored at the position pointed by rear.

12 9 7 18 14 36 45

0 1 2 3 4 5 6 7 8 9
DR. NEEPA SHAH 14

Dr. Neepa Shah 7


Unit 5_Linear Data Structure: Queue 27-10-2021

ARRAY-BASED QUEUE CONTD.

 Now, front = 0 and rear = 6

 Now, if we want to delete an element from the queue, then the value of front will be incremented.

Deletions are done from only this end of the queue.

9 7 18 14 36 45

0 1 2 3 4 5 6 7 8 9

 Now, front = 1 and rear = 6.

DR. NEEPA SHAH 15

ARRAY-BASED QUEUE CONTD.

 Before inserting an element in the queue we must check for overflow conditions. (i.e.
when rear = MAX – 1)
 Similarly, before deleting an element from the queue, we must check for underflow
condition. (If front = -1 and rear = -1)

 ONE SOLUTION TO USE FRONT LOCATIONS (IF FREE: FRONT is Not


ZERO): SHIFT THE ELEMENTS TO LEFT ONCE REAR END IS FULL

DR. NEEPA SHAH 16

Dr. Neepa Shah 8


Unit 5_Linear Data Structure: Queue 27-10-2021

ALGORITHM FOR INSERTION OPERATION

Step 1: IF REAR=MAX-1, then;


Write OVERFLOW
Goto Step 4
[END OF IF]
Step 2: IF FRONT == -1 and REAR == -1, then
SET FRONT = REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = NUM
Step 4: Exit

DR. NEEPA SHAH 17

ALGORITHM FOR DELETION OPERATION

Step 1: IF FRONT = -1, then;


Write UNDERFLOW
Goto Step 3
[END OF IF]
Step 2: IF FRONT == REAR, then
SET VAL = QUEUE[FRONT]
SET FRONT = REAR = -1
ELSE
SET VAL = QUEUE[FRONT]
SET FRONT = FRONT + 1
[END OF IF]
Step 3: Exit

DR. NEEPA SHAH 18

Dr. Neepa Shah 9


Unit 5_Linear Data Structure: Queue 27-10-2021

CIRCULAR QUEUE CONCEPT


 Use an array of size N in a circular fashion
 Two variables keep track of the front and rear
front index of the front element
rear index immediately past the rear element
 Array location rear is kept empty

normal configuration
Q
0 1 2 f r
wrapped-around configuration
Q
DR. NEEPA SHAH 0 1 2 r f 19

CIRCULAR QUEUE CONTD.


 If we don't fix one end of the queue at index 0, we won't
have to shift elements
 Circular array is an array that conceptually loops around
on itself
 The last index is thought to “precede” index 0
 In an array whose last index is n, the location “before” index 0 is index n; the location “after” index
n is index 0

 Need to keep track of where the front as well as the rear


of the queue are at any given time

DR. NEEPA SHAH 20

Dr. Neepa Shah 10


Unit 5_Linear Data Structure: Queue 27-10-2021

CIRCULAR QUEUE CONTD.

 Example: In this queue, front = 2 and rear = 9.

7 18 14 36 45 21 99 72

0 1 2 3 4 5 6 7 8 9

 If rear = MAX – 1, then OVERFLOW condition exists.


 This is the major drawback of an linear queue. (Even if space is available, no insertions can be done once rear is
equal to MAX – 1. )

 In a circular queue, the first index comes right after the last index.
 A circular queue is full, only when front=0 and rear = Max – 1.

DR. NEEPA SHAH 21

CIRCULAR QUEUE: INSERTION

▪ If front=0 and rear= MAX – 1, then the circular queue is full.


front=0 1 2 3 4 5 6 7 8 rear = 9

90 49 7 18 14 36 45 21 99 72

▪ If rear != MAX – 1, then the rear will be incremented and value will be inserted

front=0 1 2 3 4 5 6 7 rear= 8 9

90 49 7 18 14 36 45 21 99

▪ If front!=0 and rear=MAX -1, then it means that the queue is not full. So, set rear = 0 and insert the new element.

front=1 2 3 4 5 6 7 8 rear= 9

49 7 18 14 36 45 21 99 72

DR. NEEPA SHAH 22

Dr. Neepa Shah 11


Unit 5_Linear Data Structure: Queue 27-10-2021

CIRCULAR QUEUE : INSERTION CONTD.

 rear = front -1 overflow

9 10 7 18 14 36 45 21 99 72

rear=1 front=2 3 4 5 6 7 8 9

DR. NEEPA SHAH 23

CIRCULAR QUEUE : INSERTION ALGORITHM


Step 1: IF FRONT=0 and REAR=MAX–1, or REAR = FRONT – 1 then
Write “OVERFLOW”
Goto Step 4
[END OF IF]

Step 2: IF FRONT = -1 and REAR = -1, then;


SET FRONT = REAR = 0
ELSE IF REAR = MAX – 1 and FRONT != 0
SET REAR = 0
ELSE
SET REAR = REAR + 1
[END OF IF]
Step 3: SET QUEUE[REAR] = VAL
Step 4: Exit

DR. NEEPA SHAH 24

Dr. Neepa Shah 12


Unit 5_Linear Data Structure: Queue 27-10-2021

CIRCULAR QUEUE: DELETION

▪ If front = -1, then it means there are no elements in the queue. So an underflow condition will be reported.

0 1 2 3 4 5 6 7 8 9

 If the queue is not empty and after returning the value on front, if front = rear, then it means now the queue has
become empty and so front and rear are set to -1.
Delete this element and set
81
rear = front = -1

0 1 2 3 4 5 6 7 8 front=rear= 9

 If the queue is not empty and after returning the value on front, if front = MAX -1, then front is set to 0.

72 63 9 18 27 39 81

0 1 2 3 4 rear= 5 6 7 8 front= 9
DR. NEEPA SHAH 25

CIRCULAR QUEUE: DELETION ALGORITHM

Step 1: IF FRONT = -1, then


Write “Underflow”
Goto Step 4
[END OF IF]
Step 2: SET VAL = QUEUE[FRONT]
Step 3: IF FRONT = REAR
SET FRONT = REAR = -1
ELSE
IF FRONT = MAX -1
SET FRONT = 0
ELSE
SET FRONT = FRONT + 1
[END OF IF]
[END OF IF]
Step 4: EXIT

DR. NEEPA SHAH 26

Dr. Neepa Shah 13


Unit 5_Linear Data Structure: Queue 27-10-2021

CONCEPTUAL EXAMPLE OF A CIRCULAR QUEUE


front
After 5
1 After 7 enqueues 1 dequeues
0 0

front
12 12
11 11
rear rear
10 10

rear
1
0

front
12
11
DR. NEEPA SHAH After 8 more enqueues 27

10

CIRCULAR ARRAY IMPLEMENTATION OF A QUEUE

3 2 3
1 4
front queue 5
cq 0
8 5 n-1 6
rear count n-2 7

n-3 8
9

DR. NEEPA SHAH ... 10


28

Dr. Neepa Shah 14


Unit 5_Linear Data Structure: Queue 27-10-2021

A QUEUE STRADDLING THE END OF A CIRCULAR ARRAY

98 2 3
1 4
front queue 5
cq 0
2 4 99 6
rear count 98 7

97 8
9

DR. NEEPA SHAH


... 10

29

CIRCULAR QUEUE DRAWN LINEARLY

98
cq front queue
0 1 2 3 4 96 97 98 99
2 4 …
rear count

DR. NEEPA SHAH 30

Dr. Neepa Shah 15


Unit 5_Linear Data Structure: Queue 27-10-2021

CIRCULAR ARRAY IMPLEMENTATION

 When an element is enqueued, the value of rear is incremented


 But it must take into account the need to loop back to index 0:

rear = (rear+1) % queue.length;

 Can this array implementation also reach capacity?

DR. NEEPA SHAH 31

CIRCULAR QUEUE OPERATIONS (WITH MOD)


Algorithm size()
return (N - f + r) mod N

Algorithm isEmpty()
return (size = 0)
 We use the modulo operator (remainder of division)
Q
0 1 2 f r

Q
0 1 2 r f

DR. NEEPA SHAH 32

Dr. Neepa Shah 16


Unit 5_Linear Data Structure: Queue 27-10-2021

CIRCULAR QUEUE OPERATIONS (WITH MOD)

Algorithm enqueue(o)
if size() = N - 1 then
error
else
Q[r]  o
r  (r + 1) mod N
Q
0 1 2 f r

Q
0 1 2 r f

DR. NEEPA SHAH 33

CIRCULAR QUEUE OPERATIONS (WITH MOD)

Algorithm dequeue()
if isEmpty() then
Algorithm front()
error
if isEmpty() then
else
error
temp  Q[f]
return Q[f]
f  (f + 1) mod N
return temp

DR. NEEPA SHAH 34

Dr. Neepa Shah 17


Unit 5_Linear Data Structure: Queue 27-10-2021

DOUBLE ENDED QUEUE

 Fundamental Methods:
 addFirst(e)
 addLast(e)
 removeFirst()
 removeLast()

 Support methods:
 getFirst()
 getLast()
 size(): rear-front+1
 isEmpty()

DR. NEEPA SHAH 35

INSERTION IN DEQ

 addFirst / addFront/ addBegin  addLast / addRear / addEnd


Queue Full: front <= 0 (OVERFLOW) Queue Full: rear == MAX-1 (OVERFLOW)

First Insertion: front=rear=-1:: front=rear = 0 First Insertion: front=rear=-1:: front=rear = 0

Front -- Rear++
Queue[front] = element Queue[rear] = element

DR. NEEPA SHAH 36

Dr. Neepa Shah 18


Unit 5_Linear Data Structure: Queue 27-10-2021

DELETION IN DEQ

 delFirst / delFront/ delBegin  delLast / delRear / delEnd

 front=rear=-1(UNDERFLOW)  front=rear=-1(UNDERFLOW)

Last element: front==rear :: front=rear=-1 Last element: front==rear :: front=rear=-1

element = Queue[front] Element= Queue[rear]

Front ++ Rear--

DR. NEEPA SHAH 37

DOUBLE ENDED QUEUE CONTD.


 There are:
 Input-restricted Deque: An input restricted Deque restricts the insertion of the elements at one end only, the deletion of
elements can be done at both the end of a queue.

deletion
10 20 30 40 50 insertion
deletion
Q[0] Q[1] Q[2] Q[3] Q[4]

 Output-restricted Deque: restricts the deletion of elements at one end only, and allows insertion to be done at both the
ends of a Deque.

insertion
deletion
10 20 30 40 50 insertion

Q[0] Q[1] Q[2] Q[3] Q[4]

DR. NEEPA SHAH 38

Dr. Neepa Shah 19


Unit 5_Linear Data Structure: Queue 27-10-2021

INTERFACE

 1. input-restricted Q
 2. output-restricted Q
 3. Exit

 1: addLast(e), removeFirst(), removeLast(), getFirst(), getLast(), size(), isEmpty(), exit


 2: addFirst(e), addLast(e), removeFirst(), getFirst(), getLast(), size(), isEmpty(), exit

DR. NEEPA SHAH 39

PRIORITY QUEUE

DR. NEEPA SHAH 40

Dr. Neepa Shah 20


Unit 5_Linear Data Structure: Queue 27-10-2021

QUEUES THAT ALLOW LINE JUMPING

 Need a new ADT


 Operations: Insert an Item, Remove the “Best” Item

6 2
15 23
insert deleteMin
12 18
45 3 7

 PQueue property: for two elements in the queue, x and y, if x has a lower priority value
than y, x will be deleted before y
DR. NEEPA SHAH 41

A DIFFERENT KIND OF STRUCTURE

 A Priority Queue – a different kind of queue


 Items in priority queue are ordered by some key
 Item with the lowest key / highest key is always at the front, from where they are removed.

 Idea behind the Priority Queue is simple:


 Is a queue
 But the items are ordered by a key.
 Implies your ‘position’ in the queue may be changed by the arrival of a new item.
 A priority queue is no longer FIFO: You will still remove from front of queue, but insertions are governed by a
priority OR vice versa.

DR. NEEPA SHAH 42


42

Dr. Neepa Shah 21


Unit 5_Linear Data Structure: Queue 27-10-2021

TYPES OF PRIORITY QUEUE

 Min Priority Queue: In min priority Queue minimum number of value gets the highest priority and maximum
number of element gets the lowest priority.
 Max Priority Queue: Max priority Queue is the opposite of min priority Queue. Maximum number value gets
the highest priority and minimum number value gets the minimum priority.

DR. NEEPA SHAH 43

APPLICATIONS OF THE PRIORITY QUEUE

DR. NEEPA SHAH 44

Dr. Neepa Shah 22


Unit 5_Linear Data Structure: Queue 27-10-2021

APPLICATIONS OF THE PRIORITY QUEUE

 Select print jobs in order of decreasing length


 Forward packets on routers in order of urgency
 Select most frequent symbols for compression
 Sort numbers, picking minimum first
 Auctions
 Stock Market
 Scheduling queues for a processor, print queues, transmit queues, backlogs, etc.….

DR. NEEPA SHAH 45

PRIORITY QUEUE ADT

 A priority queue stores a collection of entries


 Each entry is a pair (key, value)

 Main methods of the Priority Queue ADT


 void insert(k, x)
 int removeMin()

 Additional methods
 int min()
 int size()
 int isEmpty()

DR. NEEPA SHAH

Dr. Neepa Shah 23


Unit 5_Linear Data Structure: Queue 27-10-2021

EXAMPLE
Operation Output Priority Queue
insert(5, A) [= (5, A)] {(5, A)}
Insert(9, C) [= (9, C)] {(5, A), (9, C)}
Insert(3, B) [= (3, B)] {(3, B), (5, A), (9, C)}
Insert(7, D) [= (7, D)] {(3, B), (5, A), (7, D), (9, C)}
min() e3 {(3, B), (5, A), (7, D), (9, C)}
removeMin() e3 {(5, A), (7, D), (9, C)}
size() 3 {(5, A), (7, D), (9, C)}
removeMin() e1 {(7, D), (9, C)}
removeMin() e4 {(9, C)}
removeMin() e2 {}
removeMin() “error” {}
isEmpty()
DR. NEEPA SHAH true {} 47

PRIORITY QUEUE SORTING

 We can use a priority queue to sort a set of comparable elements


1. Insert the elements one by one with a series of insert operations
2. Remove the elements in sorted order with a series of removeMin operations

 The running time of this sorting method depends on the priority queue
implementation

DR. NEEPA SHAH 48

Dr. Neepa Shah 24


Unit 5_Linear Data Structure: Queue 27-10-2021

IMPLEMENTING A PRIORITY QUEUE WITH A LIST

 Implementation with an unsorted list


 Fast insertions and slow removals

 Implementation with a sorted list


 Slow insertions and fast removals

 Binary Search Trees


 Binary Heaps

DR. NEEPA SHAH 49

SELECTION SORT

 We repeatedly find the next largest (or smallest) element in the array and
move it to its final position in the sorted array.

 We begin by selecting the largest element and moving it to the highest index
position. We can do this by swapping the element at the highest index and the
largest element. The process stops when the effective size of the array
becomes 1

DR. NEEPA SHAH 50

Dr. Neepa Shah 25


Unit 5_Linear Data Structure: Queue 27-10-2021

INSERTION SORT

 Insertion sort keeps making the left side of the array

sorted until the whole array is sorted. It sorts the values


seen so far and repeatedly inserts unseen values in the
array into the left sorted array.

DR. NEEPA SHAH 51

SEQUENCE-BASED PRIORITY QUEUE

 Implementation with an unsorted list


 Implementation with a sorted list
4 5 2 3 1 1 2 3 4 5

 Performance:
 insert takes O(1) time since we can insert
 Performance:
the item at the beginning or end of the  insert takes O(n) time since we have to find
sequence the place where to insert the item
 removeMin and min take O(n) time since we  removeMin and min take O(1) time, since
have to traverse the entire sequence to find
the smallest key the smallest key is at the beginning

DR. NEEPA SHAH 52

Dr. Neepa Shah 26


Unit 5_Linear Data Structure: Queue 27-10-2021

COMPARISON

Method Unsorted list Sorted list


Size, isEmpty O(1) O(1)
insert O(1) O(n)
min, removeMin O(n) O(1)

DR. NEEPA SHAH 53

SELECTION-SORT

 Selection-sort is the variation of PQ-sort where the priority queue is implemented


with an unsorted sequence
 Running time of Selection-sort:
1. Inserting the elements into the priority queue with n insert operations takes O(n) time
2. Removing the elements in sorted order from the priority queue with n removeMin operations
takes time proportional to

1 + 2 + …+ n
 Selection-sort runs in O(n2) time

DR. NEEPA SHAH 54

Dr. Neepa Shah 27


Unit 5_Linear Data Structure: Queue 27-10-2021

SELECTION-SORT EXAMPLE
Sequence S Priority Queue P
Input: (7,4,8,2,5,3,9) ()

Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (7,4)
.. .. ..
. . .
(g) () (7,4,8,2,5,3,9)

Phase 2
(a) (2) (7,4,8,5,3,9)
(b) (2,3) (7,4,8,5,9)
(c) (2,3,4) (7,8,5,9)
(d) (2,3,4,5) (7,8,9)
(e) (2,3,4,5,7) (8,9)
(f) (2,3,4,5,7,8) (9)
DR. NEEPA SHAH 55
(g) (2,3,4,5,7,8,9) ()

INSERTION-SORT

 Insertion-sort is the variation of PQ-sort where the priority queue is implemented


with a sorted sequence
 Running time of Insertion-sort:
1. Inserting the elements into the priority queue with n insert operations takes time proportional to
1 + 2 + …+ n
2. Removing the elements in sorted order from the priority queue with a series of n removeMin
operations takes O(n) time
 Insertion-sort runs in O(n2) time

DR. NEEPA SHAH 56

Dr. Neepa Shah 28


Unit 5_Linear Data Structure: Queue 27-10-2021

INSERTION-SORT EXAMPLE
Sequence S Priority queue P
Input: (7,4,8,2,5,3,9) ()

Phase 1
(a) (4,8,2,5,3,9) (7)
(b) (8,2,5,3,9) (4,7)
(c) (2,5,3,9) (4,7,8)
(d) (5,3,9) (2,4,7,8)
(e) (3,9) (2,4,5,7,8)
(f) (9) (2,3,4,5,7,8)
(g) () (2,3,4,5,7,8,9)

Phase 2
(a) (2) (3,4,5,7,8,9)
(b) (2,3) (4,5,7,8,9)
.. .. ..
. . .
(g) (2,3,4,5,7,8,9) ()
DR. NEEPA SHAH 57

IN-PLACE INSERTION-SORT
 Instead of using an external data structure, 5 4 2 3 1
we can implement selection-sort and
insertion-sort in-place 5 4 2 3 1
 A portion of the input sequence itself
serves as the priority queue 4 5 2 3 1
 For in-place insertion-sort
 We keep sorting the initial portion of the
2 4 5 3 1
sequence
 We can use swaps instead of modifying the 2 3 4 5 1
sequence
1 2 3 4 5
DR. NEEPA SHAH 58

1 2 3 4 5

Dr. Neepa Shah 29


Unit 5_Linear Data Structure: Queue 27-10-2021

AN INSERTION SORT OF AN ARRAY OF FIVE INTEGERS

DR. NEEPA SHAH 59

INSERTION SORT ALGORITHM


void insertionSort(int[] a) {
for (int i = 1; i < SIZE; i++) {
int ai = a[i], j;

// Shuffle up all sorted items > a[i]


for (j = i; j > 0 && a[j-1] > ai; j--) {
a[j] = a[j–1];
}
// Insert the current item
a[j] = ai;
}
}

DR. NEEPA SHAH 60

Dr. Neepa Shah 30


Unit 5_Linear Data Structure: Queue 27-10-2021

INSERTION AND DELETION OF ENTRIES

DR. NEEPA SHAH 61

MORE PRIORITY QUEUE OPERATIONS

 decreaseKey: For an entry in the queue, reduce its priority value

 increaseKey: For an entry in the queue, increase its priority value

 remove(entry e): Remove an entry from the queue

 findMax

DR. NEEPA SHAH 62

Dr. Neepa Shah 31

You might also like