Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- int swap(int *a,int *b){
- int temp;
- temp=*a;
- *a=*b;
- *b=temp;
- }
- void heapify(int a[],int i,int n){
- int largest=i;
- int left=(2*i)+1;
- int right=(2*i)+2;
- if(left<n && a[left]>a[largest])
- {
- largest=left;
- }
- if(right<n && a[right]>a[largest])
- {
- largest=right;
- }
- if(largest!=i){
- swap(&a[i],&a[largest]);
- heapify(a,largest,n);
- }
- }
- void heapsort(int a[],int n){
- int i;
- for(i=n/2-1;i>=0;i--)
- heapify(a,i,n);
- for(i=n-1;i>0;i--){
- swap(&a[0],&a[i]);
- heapify(a,0,i);
- }
- }
- void main(){
- int a[1000],n,i;
- printf("enter the number:");
- scanf("%d",&n);
- printf("enter the element:");
- for(i=0;i<n;i++){
- scanf("%d",&a[i]);
- }
- heapsort(a,n);
- printf("the sorted elements are");
- for(i=0;i<n;i++){
- printf("%d ",a[i]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment