10. Implement the Heap/Shell sort algorithm implemented in C++/Java demonstrating heap/shell data structure with modularity of programming language.

#include<iostream>
#include<cstring>
using namespace std;
#define size 20
class heap
{
	
public:
	
	void accept()
	{
		int n;
		cout<<".. .. .. .. .. .. .. .. .. .. .. "<<endl;
		cout<<": ENTER NUMBER OF STUDENTS = ";
		cin>>n;
		int a[size];
		cout<<": ENTER MARKS OF EACH STUDENTS  :"<<endl;
		for(int i = 1 ; i <= n ; i++)
		{
			cout<<"Roll NO. "<<i<<" = ";
			cin>>a[i];
		}
		
		heapsort(a,n);
		display(a,n);
	}
	
	void max_heapify(int a[] , int i , int n)
	{
		int temp,left,right,large;
		large = 0;
		left = 2*i;
		right = 2*i+1;
		if( (left <= n) && (a[left] > a[i]))
		
			large = left;
		
		else
			
			large = i;
	
		if( ( right <= n ) && ( a[right] > a[large] ) )
			
			large = right;
		
		if(large != i)
		{
			temp = a[i];
			a[i] = a[large];
			a[large] = temp;
			max_heapify(a,large,n);
		}
	}
			
		
			 
	
	void heapsort(int a[] , int n)
	{
		for(int k = n/2 ; k >= 1 ; k--)
		{
			max_heapify(a,k,n);
		}
		int temp;
		for(int i = n ; i >= 2 ; i--)
		{
			temp = a[1];
			a[1] = a[i];
			a[i] = temp;
			
			max_heapify(a,1,i-1);
		}
	}
	
	void display(int a[] , int n)
	{
		cout<<"\n MINIMUM MARKS = "<<a[1];
		cout<<"\n MAXIMUM MARKS = "<<a[n]<<endl;
	}
		
};

int main()
{
	heap h;
	h.accept();
	return 0;
}		 
			   

Comments

Popular posts from this blog

2. Beginning with an empty binary search tree, Construct binary search tree by inserting the values in the order given. After constructing a binary tree -i.Insert new nodeii.Find number of nodes in longest path iii.Minimum data value found inthe tree iv.Change a tree so that the roles of the left and right pointers are swapped at every node v.Search a value

1. A book consists of chapters, chapters consist of sections and sections consist of subsections. Construct a tree and print the nodes. Find the time and space requirements of your method.

4. Write a function to get the number of vertices in an undirected graph and its edges. You may assume thatno edge is input twice. i.Use adjacency list representation of the graph and find runtime of the functionii.Use adjacency matrix representation of the graph and find runtime of the function