Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions lab5q1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<bits/stdc++.h>
using namespace std;
void Max_heapify(int arr2[],int f,int size){
int max=f,l=f*2,r=f*2+1,t;
if(l<=size && arr2[l]>arr2[max]) {max=l;}
if(r<=size && arr2[r]>arr2[max]) {max=r;}
if(f!=max){
t=arr2[f];
arr2[f]=arr2[max];
arr2[max]=t;
Max_heapify(arr2,max,size);
}
}
void Max_heap (int arr1[],int start,int end){
for(int i=(end)/2;i>=start;i--){
Max_heapify(arr1,i,end);
}
}

void level_ordered(int arr[] ,int size){
int g=true;
int i=2,k,m=1,ct=0;
cout<<"Level order traversal of array is : "<<endl;
while(g){
cout<<"Level --> "<<ct<<" : ";
for(k=m;k<i && k<=size;k++){
cout<<arr[k]<<" ";
}
cout<<endl;
m=k;
ct++;
if(i>size) g=false;
i=i+pow(2,ct);

}
}
void heapsort(int arr[],int k){
int i,t;
for(int i=k;i>=2;i--){
t=arr[1];
arr[1]=arr[i];
arr[i]=t;
Max_heapify(arr,1,i-1);
}
}
int main(){
int n;
cin>>n;
int arr[n+1];
for(int i=1;i<n+1;i++){cin>>arr[i];}
Max_heap(arr,1,n);
heapsort(arr,n);
cout<<"Sorted Array using heapsort is : "<<endl;
for(int i=1;i<n+1;i++){cout<<arr[i]<<" ";}
cout<<endl;
int V[n+1];
for(int i=1;i<n+1;i++){V[i]=arr[n+1-i];}
level_ordered(V,n);
return 0;
}