Heap Sort Algorithm
Heap Sort Algorithm
5th Semester
Submitted to: Sir Mansoor
ALGORITHM ANALYSIS
ASS # 2
Working and complexity of Heap sort, Heap sort vs Quick sort and
Pseudo code
Then a sorted array is created by repeatedly removing the largest/smallest element from the
heap, and inserting it into the array. The heap is reconstructed after each removal.
What is a Heap?
Heap is a special tree-based data structure, that satisfies the following special heap properties:
1.
Shape Property: Heap data structure is always a Complete Binary Tree, which means all
levels of the tree are fully filled.
2.
Heap Property: All nodes are either [greater than or equal to] or [less than or equal to] each
of its children. If the parent nodes are greater than their children, heap is called a Max-Heap,
and if the parent nodes are smaller than their child nodes, heap is called Min-Heap.
void main()
{
int a[10], i, size;
cout << "Enter size of list";
}
for( i=0; i < length; i++)
{
cout << "\t" << a[i];
}
}
}
else
{
largest = i;
}
if( r <= heapsize && a[r] > a[largest])
{
largest = r;
}
if(largest != i)
{
temp = a[i];
a[i] = a[largest];
a[largest] = temp;
satisfyheap(a, largest, heapsize);
}
}
Heap sort is not a Stable sort, and requires a constant space for sorting a list.
Pseudo code
Pseudo code is a simple way of writing programming code in English. Pseudo code is not
actual programming language. It uses short phrases to write code for programs before you
actually create it in a specific language. Once you know what the program is about and how
it will function, then you can use pseudo code to create statements to achieve the required
results for your program.
Pseudo code makes creating programs easier. Programs can be complex and long;
preparation is the key. For years, flowcharts were used to map out programs before writing
one line of code in a language. However, they were difficult to modify and with the
advancement of programming languages, it was difficult to display all parts of a program with
a flowchart. It is challenging to find a mistake without understanding the complete flow of a
Examples of Pseudocode
Let's review an example of pseudocode to create a program to add 2 numbers together and
End Program
Compare that pseudocode to an example of a flowchart to add two numbers
Now, let's look at a few more simple examples of pseudocode. Here is a pseudocode
and understand the flow of a program. This becomes cost effective and there is less time
spent finding and correcting errors.
dangerous materials, control for aircraft, defence, etc) software will generally have a
response time as part of the system specifications. In all such systems, it is not
acceptable to design based on average performance, you must always allow for the
worst case, and thus treat quicksort as O(n2).
So far, our best sorting algorithm has O(nlog n) performance: can we do any better?
In general, the answer is no.
However, if we know something about the items to be sorted, then we may be able to do
better.
But first, we should look at squeezing the last drop of performance out of quicksort.