Question

In: Computer Science

Write a program to sort the following using HeapSort. Use any language you want. A=[10, 8,...

Write a program to sort the following using HeapSort. Use any language you want.

A=[10, 8, 7, 9,16, 14, 3, 2, 4, 1]

Solutions

Expert Solution

#include<stdio.h>
#include <conio.h>
void Adjust(int Heap_of_Numbers[],int i) /*Function to arrange the elements in the heap*/
{
int j;
int copy;
int Number;
int Reference = 1;
Number=Heap_of_Numbers[0];
while(2*i<=Number && Reference==1)
{
j=2*i;
if(j+1<=Number && Heap_of_Numbers[j+1] > Heap_of_Numbers[j])
j=j+1;
if( Heap_of_Numbers[j] < Heap_of_Numbers[i])
Reference=0;
else
{
copy=Heap_of_Numbers[i];
Heap_of_Numbers[i]=Heap_of_Numbers[j];
Heap_of_Numbers[j]=copy;
i=j;
}
}
}
void Make_Heap(int heap[])
{
int i;
int Number_of_Elements;
Number_of_Elements=heap[0];
for(i=Number_of_Elements/2;i>=1;i--)
Adjust(heap,i);
}
int main()
{
int heap[30];
int NumberofElements;
int i;
int LastElement;
int CopyVariable;
printf("Enter the number of elements present in the unsorted Array:");
scanf("%d",&NumberofElements);
printf("nEnter the members of the array one by one:"); /* Asking for the elements of the unsorted array*/
for(i=1;i<=NumberofElements;i++)
scanf("%d",&heap[i]);
heap[0]=NumberofElements;
Make_Heap(heap);
while(heap[0] > 1) /*Loop for the Sorting process*/
{
LastElement=heap[0];
CopyVariable=heap[1];
heap[1]=heap[LastElement];
heap[LastElement]=CopyVariable;
heap[0]--;
Adjust(heap,1);
}
printf("nSorted Array:n");/*Printing the sorted Array*/
for(i=1;i<=NumberofElements;i++)
printf("%d ",heap[i]);
return 0;
}

Related Solutions

Write a program, using any language you want, to sort the following using QuickSort: {3, 4,...
Write a program, using any language you want, to sort the following using QuickSort: {3, 4, 5,1, 2, 6, 7, 8}
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6,...
java 2. Write a program to implement heapsort. Sort the following elements using your program. 6, 12, 34, 29, 28, 11, 23, 7, 0, 33, 30, 45
Write a program, using any language you want, and any sorting algorithm discussed in this unit...
Write a program, using any language you want, and any sorting algorithm discussed in this unit to sort the following : 243, 1, 4, 6, 234, 33, 674, 32, 3333 Note: Can you write it in quick sort
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using...
Java language 1. Sort any 10 keys using Min Heap. 2. Sort any 10 keys using Max Heap.
Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what...
Write a program, using any language you want, that uses a Randomized-Select algorithm. Briefly explain what it does, how it does. Include code and show output.
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
Using C# programming language, Write a program that sort three numbers entered by the user using...
Using C# programming language, Write a program that sort three numbers entered by the user using only if and nested if statements. If for instance the user entered 5, 2, and 7; the program should display 2,5,7.
Develop an assembly language program for 89c51 to do following {Marks 10} Store any 8 values...
Develop an assembly language program for 89c51 to do following {Marks 10} Store any 8 values of one byte each anywhere in Scratch Pad area of RAM. You are bound to use loop to store values. Find Mean of these 8 values (use shift operator for division) and send it to port P2 (formula for to find mean is given below) Find the lowest number among the numbers saved in part (1), take its 2’s complement and send it to...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT