SlideShare a Scribd company logo
1 of 28
Priority Queue A priority queue is a data structure for maintaining a set S of elements, each with an associated value called a key.
Heap and Priority Queue Heap can be used to implement a priority queue.
Priority Queue ,[object Object],[object Object],[object Object]
Priority Queue ,[object Object],[object Object],[object Object]
Priority Queue A max-priority queue supports the following operations INSERT(S,x), MAXIMUM(S) EXTRACT-MAX(S), INCREASE-KEY(S,x,k)
Priority Queue HEAP-MAXIMUM(A) return A[1]
Priority Queue HEAP-EXTRACT-MAX(A) if heap-size[A] < 1 then error “heap underflow” max    A[1] A[1]    A[heap-size[A]] heap-size[A]    heap-size[A]-1 MAX-HEAPIFY(A,1) return max
Priority Queue HEAP-INCREASE-KEY(A, i, key) if key < A[i] then error “new key is smaller than current key” A[i]    key while i > 1 and A[PARENT(i)] < A[i] do exchange A[i]    A[PARENT(i)] i    PARENT(i)
Priority Queue i (a) 16 2 9 8 3 10 7 14 4 1
Priority Queue i (b) 16 2 9 8 3 10 7 14 15 1
Priority Queue i (c) 16 2 9 15 3 10 7 14 8 1
Priority Queue i (d) 16 2 9 14 3 10 7 15 8 1
Priority Queue MAX-HEAP-INSERT(A, key) heap-size[A]    heap-size[A]+1 A[heap-size[A]]    - ∞ HEAP-INCREASE-KEY (A, heap-size[A], key)
Quick Sort Divide: Partition the array into two sub-arrays A[p . . q-1]  and A[q+1 . . r] such that each element of  A[p . . q-1] is less than or equal to A[q], which in turn less than or equal to each element of A[q+1 . . r]
Quick Sort Conquer: Sort the two sub-arrays A[p . . q-1]  and A[q+1 . . r] by recursive calls to quick sort.
Quick Sort Combine: Since the sub-arrays are sorted in place, no work is needed to combine them.
Quick Sort QUICKSORT(A, p, r) if p< r  then q    PARTITION(A, p, r) QUICKSORT(A, p, q-1) QUICKSORT(A, q+1, r)
Quick Sort PARTITION(A, p, r) x    A[r] i    p-1
Quick Sort for j    p to r-1 do if A[j] <= x then i   i+1 exchange A[i]       A[j] exchange A[i+1]       A[r] return i+1
Quick Sort (a) i 4 6 5 3 1 7 8 2 p, j r
Quick Sort (b) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (c) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (d) 4 6 5 3 1 7 8 2 j p, i r
Quick Sort (e) 4 6 5 3 8 7 1 2 j i p r
Quick Sort (f) 4 6 5 7 8 3 1 2 i p r j
Quick Sort (g) 4 6 5 7 8 3 1 2 i p r j
Quick Sort (h) 4 6 5 7 8 3 1 2 i p r
Quick Sort (i) 8 6 5 7 4 3 1 2 i p r

More Related Content

What's hot

Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - GraphMadhu Bala
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation Anil Kumar Prajapati
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structureSajid Marwat
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREESTABISH HAMID
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 
Queue implementation
Queue implementationQueue implementation
Queue implementationRajendran
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data StructureZidny Nafan
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTUREArchie Jamwal
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionHAMID-50
 
Stack & Queue using Linked List in Data Structure
Stack & Queue using Linked List in Data StructureStack & Queue using Linked List in Data Structure
Stack & Queue using Linked List in Data StructureMeghaj Mallick
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 

What's hot (20)

Stack project
Stack projectStack project
Stack project
 
Data structure - Graph
Data structure - GraphData structure - Graph
Data structure - Graph
 
Data structure power point presentation
Data structure power point presentation Data structure power point presentation
Data structure power point presentation
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
b+ tree
b+ treeb+ tree
b+ tree
 
Lec 17 heap data structure
Lec 17 heap data structureLec 17 heap data structure
Lec 17 heap data structure
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
stack presentation
stack presentationstack presentation
stack presentation
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
Array data structure
Array data structureArray data structure
Array data structure
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Queue implementation
Queue implementationQueue implementation
Queue implementation
 
Quick sort
Quick sortQuick sort
Quick sort
 
Multi ways trees
Multi ways treesMulti ways trees
Multi ways trees
 
Queue Data Structure
Queue Data StructureQueue Data Structure
Queue Data Structure
 
STACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURESTACKS IN DATASTRUCTURE
STACKS IN DATASTRUCTURE
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
B+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletionB+ tree intro,uses,insertion and deletion
B+ tree intro,uses,insertion and deletion
 
Stack & Queue using Linked List in Data Structure
Stack & Queue using Linked List in Data StructureStack & Queue using Linked List in Data Structure
Stack & Queue using Linked List in Data Structure
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 

Viewers also liked

Viewers also liked (20)

Priority queues
Priority queuesPriority queues
Priority queues
 
Priority queue
Priority queuePriority queue
Priority queue
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Queue data structure
Queue data structureQueue data structure
Queue data structure
 
My lectures circular queue
My lectures circular queueMy lectures circular queue
My lectures circular queue
 
deque and it applications
deque and it applicationsdeque and it applications
deque and it applications
 
Ppt presentation of queues
Ppt presentation of queuesPpt presentation of queues
Ppt presentation of queues
 
Priority Queue
Priority QueuePriority Queue
Priority Queue
 
computer notes - Priority queue
computer notes -  Priority queuecomputer notes -  Priority queue
computer notes - Priority queue
 
23 priority queue
23 priority queue23 priority queue
23 priority queue
 
Applications of queues ii
Applications of queues   iiApplications of queues   ii
Applications of queues ii
 
Circular queues
Circular queuesCircular queues
Circular queues
 
Queue
QueueQueue
Queue
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Queue as data_structure
Queue as data_structureQueue as data_structure
Queue as data_structure
 
Stack
StackStack
Stack
 
Linked list
Linked listLinked list
Linked list
 
Notes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queueNotes DATA STRUCTURE - queue
Notes DATA STRUCTURE - queue
 
Heaps
HeapsHeaps
Heaps
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 

Similar to Algorithm: priority queue

lecture 6
lecture 6lecture 6
lecture 6sajinsc
 
Max priority queue
Max priority queueMax priority queue
Max priority queue9854098540
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxDeepakM509554
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-SortTareq Hasan
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Kotlinify Your Project!
Kotlinify Your Project!Kotlinify Your Project!
Kotlinify Your Project!OrNoyman
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptxDeepakM509554
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)mailmerk
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sortKrish_ver2
 
Otter 2016-11-28-01-ss
Otter 2016-11-28-01-ssOtter 2016-11-28-01-ss
Otter 2016-11-28-01-ssRuo Ando
 

Similar to Algorithm: priority queue (20)

Unit6 C
Unit6 C Unit6 C
Unit6 C
 
lecture 6
lecture 6lecture 6
lecture 6
 
Algorithms - "heap sort"
Algorithms - "heap sort"Algorithms - "heap sort"
Algorithms - "heap sort"
 
Max priority queue
Max priority queueMax priority queue
Max priority queue
 
Quick sort
Quick sortQuick sort
Quick sort
 
CSE680-07QuickSort.pptx
CSE680-07QuickSort.pptxCSE680-07QuickSort.pptx
CSE680-07QuickSort.pptx
 
Algorithm: Quick-Sort
Algorithm: Quick-SortAlgorithm: Quick-Sort
Algorithm: Quick-Sort
 
sorting1.pptx
sorting1.pptxsorting1.pptx
sorting1.pptx
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Kotlinify Your Project!
Kotlinify Your Project!Kotlinify Your Project!
Kotlinify Your Project!
 
Ada file
Ada fileAda file
Ada file
 
quick sort by deepak.pptx
quick sort by deepak.pptxquick sort by deepak.pptx
quick sort by deepak.pptx
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Chap06alg
Chap06algChap06alg
Chap06alg
 
Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)Data structure and algorithm.(dsa)
Data structure and algorithm.(dsa)
 
3.8 quick sort
3.8 quick sort3.8 quick sort
3.8 quick sort
 
06-2402.pdf
06-2402.pdf06-2402.pdf
06-2402.pdf
 
Otter 2016-11-28-01-ss
Otter 2016-11-28-01-ssOtter 2016-11-28-01-ss
Otter 2016-11-28-01-ss
 
Unit 7 sorting
Unit 7   sortingUnit 7   sorting
Unit 7 sorting
 
quick and merge.pptx
quick and merge.pptxquick and merge.pptx
quick and merge.pptx
 

More from Tareq Hasan

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPressTareq Hasan
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPressTareq Hasan
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryTareq Hasan
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHPTareq Hasan
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011Tareq Hasan
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array PointerTareq Hasan
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.pptTareq Hasan
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: InheritanceTareq Hasan
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to ArraysTareq Hasan
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design ExamplesTareq Hasan
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 

More from Tareq Hasan (20)

Grow Your Career with WordPress
Grow Your Career with WordPressGrow Your Career with WordPress
Grow Your Career with WordPress
 
Caching in WordPress
Caching in WordPressCaching in WordPress
Caching in WordPress
 
How to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org RepositoryHow to Submit a plugin to WordPress.org Repository
How to Submit a plugin to WordPress.org Repository
 
Composer - The missing package manager for PHP
Composer - The missing package manager for PHPComposer - The missing package manager for PHP
Composer - The missing package manager for PHP
 
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011WordPress Theme & Plugin development best practices - phpXperts seminar 2011
WordPress Theme & Plugin development best practices - phpXperts seminar 2011
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
02 c++ Array Pointer
02 c++ Array Pointer02 c++ Array Pointer
02 c++ Array Pointer
 
01 c++ Intro.ppt
01 c++ Intro.ppt01 c++ Intro.ppt
01 c++ Intro.ppt
 
chapter22.ppt
chapter22.pptchapter22.ppt
chapter22.ppt
 
chapter - 6.ppt
chapter - 6.pptchapter - 6.ppt
chapter - 6.ppt
 
Algorithm.ppt
Algorithm.pptAlgorithm.ppt
Algorithm.ppt
 
chapter-8.ppt
chapter-8.pptchapter-8.ppt
chapter-8.ppt
 
chapter23.ppt
chapter23.pptchapter23.ppt
chapter23.ppt
 
chapter24.ppt
chapter24.pptchapter24.ppt
chapter24.ppt
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java: Inheritance
Java: InheritanceJava: Inheritance
Java: Inheritance
 
Java: Exception
Java: ExceptionJava: Exception
Java: Exception
 
Java: Introduction to Arrays
Java: Introduction to ArraysJava: Introduction to Arrays
Java: Introduction to Arrays
 
Java: Class Design Examples
Java: Class Design ExamplesJava: Class Design Examples
Java: Class Design Examples
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 

Algorithm: priority queue