Like this presentation? Why not share!

# Computer notes - Heap Sort

## by ecomputernotes on Dec 28, 2011

• 590 views

If k = N, and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements. We will refine this idea to obtain a fast sorting algorithm called heapsort

If k = N, and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements. We will refine this idea to obtain a fast sorting algorithm called heapsort

### Views

Total Views
590
Views on SlideShare
590
Embed Views
0

Likes
0
28
0

No embeds

### Categories

Uploaded via SlideShare as Microsoft PowerPoint

### Report content

• Start of lecture 33
• buildHeap is O ( N ) operation. Each deleteMin is O (log 2 N ). The total running time is thus O ( N + k log 2 N ). If k is small, or O ( N / log 2 N ), then the time required is O ( N ).
• End of lecture 33, Start of lecture 34

## Computer notes - Heap SortPresentation Transcript

• Class No.27 Data Structures http://ecomputernotes.com
• Priority Queue Using Heap
• #include “Event.cpp”
• #include “Heap.cpp”
• #define PQMAX 30
• class PriorityQueue
• {
• public:
• PriorityQueue() {
• heap = new Heap<Event>( PQMAX );
• };
• ~PriorityQueue() {
• delete heap;
• };
http://ecomputernotes.com
• Priority Queue Using Heap
• Event* remove()
• {
• if( !heap->isEmpty() ) {
• Event* e;
• heap->deleteMin( e );
• return e;
• }
• return (Event*)NULL;
• cout << &quot;remove - queue is empty.&quot; << endl;
• };
http://ecomputernotes.com
• Priority Queue Using Heap
• int insert(Event* e)
• {
• if( !heap->isFull() ) {
• heap->insert( e );
• return 1;
• }
• cout << &quot;insert queue is full.&quot; << endl;
• return 0;
• };
• int full(void){
• return heap->isFull();
• };
• int length() { return heap->getSize(); };
• };
http://ecomputernotes.com
• The Selection Problem
• Given a list of N elements (numbers, names etc.), which can be totally ordered, and an integer k , find the k th smallest (or largest) element.
• One way is to put these N elements in an array an sort it. The k th smallest of these is at the k th position.
http://ecomputernotes.com
• The Selection Problem
• A faster way is to put the N elements into an array and apply the buildHeap algorithm on this array.
• Finally, we perform k deleteMin operations. The last element extracted from the heap is our answer.
• The interesting case is k =  N / 2  , since this is known as the median .
http://ecomputernotes.com
• HeapSort
• If k = N , and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements.
• Later in the course, we will refine this idea to obtain a fast sorting algorithm called heapsort .
http://ecomputernotes.com
• Suppose we have a database of people.
• We want to figure out who is related to whom.
• Initially, we only have a list of people, and information about relations is gained by updates of the form “Haaris is related to Saad”.
http://ecomputernotes.com
• Once we have relationships information, we would like to answer queries like “Is Haaris related to Ahmad?”
http://ecomputernotes.com
• Blob Coloring
• A well-known low-level computer vision problem for black and white images is the following:
• Gather together all the picture elements (pixels) that belong to the same &quot;blobs&quot;, and give each pixel in each different blob an identical label.
http://ecomputernotes.com
• Blob Coloring
• Thus in the following image, there are five blobs.
• We want to partition the pixels into disjoint sets , one set per blob.
http://ecomputernotes.com
• The image segmentation problem.
http://ecomputernotes.com
• Equivalence Relations
• A binary relation  over a set S is called an equivalence relation if it has following properties
• Reflexivity: for all element x  S, x  x
• Symmetry: for all elements x and y, x  y if and only if y  x
• Transitivity: for all elements x, y and z, if x  y and y  z then x  z
• The relation “is related to” is an equivalence relation over the set of people
http://ecomputernotes.com
• Equivalence Relations
• The  relationship is not an equivalence relation.
• It is reflexive, since x  x,
• and transitive, since x  y and y  z implies x  z,
• it is not symmetric since x  y does not imply y  x.
http://ecomputernotes.com