Computer Science‎ > ‎

### Arrays and Sorting: Heap Sort ( with C Program source code, a tutorial and an MCQ Quiz )

A few recommendations for Data Structures and Algorithms:
 Data Structures, Algorithm Design and Analysis of Complexity Introduction to Algorithms 3r...List Price: Rs.525Our Price: Rs.357   Introduction to Algorithms (CLRS) Algorithms 4th EditionOur Price: Rs.599 Algorithms (By Robert Sedgewick) An Introduction to the Analys...List Price: Rs.550Our Price: Rs.500Analysis of Algorithms (By Robert Sedgewick)

Basic Sorting and Searching Algorithms for Arrays, at a glance
 Bubble Sort Insertion Sort Selection Sort Shell Sort Merge Sort Quick Sort Heap Sort Binary Search Algorithm

### Heap Sort

Heapsort uses the property of Heaps to sort an array. The Heap data structure is an array object that can be viewed as a complete and balanced binary tree. Min (Max)-Heap has a property that for every node other than the root, the value of the node is at least (at most) the value of its parent. Thus, the smallest (largest) element in a heap is stored at the root, and the subtrees rooted at a node contain larger (smaller) values than does the node itself.

### Algorithm:

It starts with building a heap by inserting the elements entered by the user, in its place.
1. Increase the size of the heap as a value is inserted.
2. Insert the entered value in the last place.
3. Compare the inserted value with its parent, until it satisfies the heap property and then
place it at its right position.

Now once the heap is built remove the elements from top to bottom, while maintaining the heap property to obtain the sorted list of entered values.

1. heap[1] is the minimum element. So we remove heap[1]. Size of the heap is decreased.
2. Now heap[1] has to be filled. We put the last element in its place and see if it fits. If it does not fit, take minimum element among both its children and replaces parent with it.
3. Again see if the last element fits in that place.

### Property:

Best case performance – when the input array is already sorted O(nlogn).
Worst case performance – when the input array is in reverse order O(nlogn).
Average case performance – O(nlogn)
It does not require any extra space for sorting, hence O(1) extra space.
It is not stable.

## Heap Sort - C Program Source Code

`#include<stdio.h>#include<limits.h>/*Declaring heap globally so that we do not need to pass it as an argument every time*//* Heap used here is Min Heap */int heap[1000000],heapSize;/*Initialize Heap*/void Init(){        heapSize = 0;        heap[0] = -INT_MAX;}/*Insert an element into the heap */void Insert(int element){        heapSize++;        heap[heapSize] = element; /*Insert in the last place*/        /*Adjust its position*/        int now = heapSize;        while(heap[now/2] > element)         {                heap[now] = heap[now/2];                now /= 2;        }        heap[now] = element;}int DeleteMin(){        /* heap[1] is the minimum element. So we remove heap[1]. Size of the heap is decreased.            Now heap[1] has to be filled. We put the last element in its place and see if it fits.           If it does not fit, take minimum element among both its children and replaces parent with it.           Again See if the last element fits in that place.*/        int minElement,lastElement,child,now;        minElement = heap[1];        lastElement = heap[heapSize--];        /* now refers to the index at which we are now */        for(now = 1; now*2 <= heapSize ;now = child)        {                /* child is the index of the element which is minimum among both the children */                 /* Indexes of children are i*2 and i*2 + 1*/                child = now*2;                /*child!=heapSize beacuse heap[heapSize+1] does not exist, which means it has only one                   child */                if(child != heapSize && heap[child+1] < heap[child] )                 {                        child++;                }                /* To check if the last element fits ot not it suffices to check if the last element                   is less than the minimum element among both the children*/                if(lastElement > heap[child])                {                        heap[now] = heap[child];                }                else /* It fits there */                {                        break;                }        }        heap[now] = lastElement;        return minElement;}int main(){        int number_of_elements;        scanf("%d",&number_of_elements);        int iter, element;        Init();        for(iter = 0;iter < number_of_elements;iter++)        {                scanf("%d",&element);                Insert(element);        }        for(iter = 0;iter < number_of_elements;iter++)        {                printf("%d ",DeleteMin());        }        printf("\n");        return 0;}`

`MCQ Quiz: Efficient Sorting Algorithms- Quick sort, Merge Sort, Heap Sort- Check how much you can score!Your score will be e-mailed to the address filled up by you.MCQ Quiz- More efficient sorting algorithms `
Related Tutorials :

 Bubble Sort One of the most elementary sorting algorithms to implement - and also very inefficient. Runs in quadratic time. A good starting point to understand sorting in general, before moving on to more advanced techniques and algorithms. A general idea of how the algorithm works and a the code for a C program. Insertion Sort Another quadratic time sorting algorithm - an example of dynamic programming. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs insertion sort. Selection Sort Another quadratic time sorting algorithm - an example of a greedy algorithm. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs selection sort. Shell Sort An inefficient but interesting algorithm, the complexity of which is not exactly known. Merge Sort An example of a Divide and Conquer algorithm. Works in O(n log n) time. The memory complexity for this is a bit of a disadvantage. Quick Sort In the average case, this works in O(n log n) time. No additional memory overhead - so this is better than merge sort in this regard. A partition element is selected, the array is restructured such that all elements greater or less than the partition are on opposite sides of the partition. These two parts of the array are then sorted recursively. Heap Sort Efficient sorting algorithm which runs in O(n log n) time. Uses the Heap data structure. Binary Search Algorithm Commonly used algorithm used to find the position of an element in a sorted array. Runs in O(log n) time.

## Recommended books for learning Computer Science, learning high quality programming, and preparing for programming interviews:

These are the standard sources of the knowledge expected from candidates interviewing at Google, Microsoft, Facebook, Amazon and other startups and top-tier technology companies. The books by Cormen or Sedgewick (a standard part of the undergraduate curriculum) are sufficient for this part of the preparation. Google specially, loves to focus on algorithmic questions.

 1. A good knowledge of Data Structures, Algorithm Design and Analysis of ComplexityThe books by Cormen or Sedgewick (a standard part of the undergraduate curriculum) are sufficient for this part of the preparation. Google specially, loves to focus on algorithmic questions. Introduction to Algorithms 3r...List Price: Rs.525Our Price: Rs.357   Introduction to Algorithms (CLRS) Algorithms 4th EditionOur Price: Rs.599 Algorithms (By Robert Sedgewick) An Introduction to the Analys...List Price: Rs.550Our Price: Rs.500Analysis of Algorithms (By Robert Sedgewick) 2. Standard and established sources of Programming Interview QuestionsA lot of interview questions are re-used and re-cycled by interviews again and again. Chances are, that question you're going to be asked, has been asked a hundred times during the last week, across a number of companies!Google, Facebook, Microsoft, Amazon- all of them frequently ask questions on the lines of those in these books. Cracking The Coding Interview...List Price: Rs.2293Our Price: Rs.2178Cracking the Coding Interview: 150 Programming Questions and Solutions [Paperback] Programming Interviews ExposedList Price: Rs.299Our Price: Rs.284Programming Interviews Exposed Ace the Programming InterviewOur Price: Rs.1733 Ace the Programming Interview Ace the IT InterviewOur Price: Rs.1300 Ace the IT Interview 3. Puzzles and Brainteasers (often asked, good to be prepared)Questions from Mount Fuji are specially popular with Microsoft recruiters. How Would You Move Mount Fuji?List Price: Rs.860Our Price: Rs.800How would you move Mount Fuji? How to Ace the Brain Teaser Interview 4. Good Software Engineering practices. Writing clean and secure code, an awareness about Design Patterns and Object Oriented Programming techniques. Some of these books are frequently recommended by Microsoft recruiters. Take care of buffer overflows!Code Complete is a recommended book for Microsoft Interviews. Code CompleteList Price: Rs.629Our Price: Rs.611 Code Complete 2 Writing Secure Code 2nd EditionOur Price: Rs.699 Writing Secure Code Design PatternsList Price: Rs.499Our Price: Rs.409 Design Patterns In case your primary language is C++, make sure you know its "ins-and-outs". You should know about destructors and constructors, virtual destructors, polymorphism, function overloading. Some of these books might help you revise C++. The C++ Programming Language...List Price: Rs.775Our Price: Rs.736The C++ Programming Language (Bjarne Stroustrup) Beginning C++ Through Game Pr...Our Price: Rs.2008 Beginning C++ through Game Programming C++ How to ProgramList Price: Rs.695Our Price: Rs.632 C++: How to Program (Deitel and Deitel) In case your primary language is C, make sure to be well versed with pointers, system calls and how they work. How does Malloc work, how does calloc work? All of these are questions which some interviewers could potentially ask, to test the depth of your knowledge and your understanding of the system "under the hood". The C Programming LanguageList Price: Rs.175Our Price: Rs.137The C Programming Language (Kernighan) C How To Program 6th EditionList Price: Rs.650Our Price: Rs.624C How to Program (Deitel and Deitel) Here's a great book for the Java lovers as well.It normally doesn't matter which programming language you know, but make sure to know about it in detail. JavaList Price: Rs.595Our Price: Rs.541 Java: A Beginner's Guide (Herb Schildt)

Tutorials on Sorting- at a glance

 Bubble Sort - One of the most elementary sorting algorithms to implement - and also very inefficient. Runs in quadratic time. A good starting point to understand sorting in general, before moving on to more advanced techniques and algorithms. A general idea of how the algorithm works and a the code for a C program.Insertion Sort - Another quadratic time sorting algorithm - an example of dynamic programming. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs insertion sort.Selection Sort - Another quadratic time sorting algorithm - an example of a greedy algorithm. An explanation and step through of how the algorithm works, as well as the source code for a C program which performs selection sort.Shell Sort- An inefficient but interesting algorithm, the complexity of which is not exactly known.Merge Sort An example of a Divide and Conquer algorithm. Works in O(n log n) time. The memory complexity for this is a bit of a disadvantage.Quick Sort In the average case, this works in O(n log n) time. No additional memory overhead - so this is better than merge sort in this regard. A partition element is selected, the array is restructured such that all elements greater or less than the partition are on opposite sides of the partition. These two parts of the array are then sorted recursively.Heap Sort- Efficient sorting algorithm which runs in O(n log n) time. Uses the Heap data structure.Binary Search Algorithm- Commonly used algorithm used to find the position of an element in a sorted array. Runs in O(log n) time.