Implement in C ONLY the heapSort algorithm discussed in class. For the BUILDHEAP procedure use the bottom-up approach. Verify the correctness of your heapsort program on arrays of small size (10’s of elements) and compare the running time of your heapsort program with your earlier implementation of the quicksort program on large arrays (100’s of elements)
#include<stdio.h>
#include <time.h>
#include<stdlib.h>
void heapsort(int[],int);
void heapify(int[],int);
void adjust(int[],int);
//driver program
main()
{
int n,i,a[500];
clock_t start, end;
double time_used=0;
printf("\nEnter the number of elements to sort:");
scanf("%d",&n);
printf("\nEnter %d elements:",n);
for (i=0;i<n;i++)
{
printf("\n Enter %d element",i+1);
scanf("%d",&a[i]);
}
/* Recording the starting clock tick.*/
start = clock();
heapsort(a,n);
// Recording the end clock tick.
end = clock();
// Calculating total time taken by the program.
time_used = (double)(end - start) / (double)(CLOCKS_PER_SEC);
printf("Time required for sort %d elements is %lf
secs.",n,time_used);
printf("\nThe Sorted Elements Are:\n");
for (i=0;i<n;i++)
printf(" %d ",a[i]);
//loop to generate 100 numbers and sort them using heapsort
for(i=0;i<100;i++)
{
a[i]= rand() % 500 +1;
}
time_used=0;
/* Recording the starting clock tick.*/
start = clock();
heapsort(a,100);
// Recording the end clock tick.
end = clock();
// Calculating total time taken by the program.
time_used = (double)(end - start) / (double)(CLOCKS_PER_SEC);
printf("\nTime required for sort 100 elements is %lf
secs.",time_used);
printf("\nThe Sorted Elements Are:\n");
for (i=0;i<100;i++)
printf("\t%d",a[i]);
printf("\n");
}
//heapsort() method
void heapsort(int a[],int n)
{
int i,t;
heapify(a,n); //call to heapify method
for (i=n-1;i>0;i--) {
t = a[0]; //perform swap operation
a[0] = a[i];
a[i] = t;
adjust(a,i);
}
}
//heapify() method
void heapify(int a[],int n) {
int k,i,j,item;
for (k=1;k<n;k++) {
item = a[k];
i = k;
j = (i-1)/2;
while((i>0)&&(item>a[j])) {
a[i] = a[j];
i = j;
j = (i-1)/2;
}
a[i] = item;
}
}
//adjust() methjod
void adjust(int a[],int n) {
int i,j,item;
j = 0;
item = a[j];
i = 2*j+1;
while(i<=n-1) {
if(i+1 <= n-1)
if(a[i] <a[i+1])
i++;
if(item<a[i]) {
a[j] = a[i];
j = i;
i = 2*j+1;
} else
break;
}
a[j] = item;
}
OUTPUT

Implement in C ONLY the heapSort algorithm discussed in class. For the BUILDHEAP procedure use the...
Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker to 1 loop (walker < size) reheapUp (heap, walker) increment walker end loop Write a program to test your program using a random integer array of size 30. Print the arrays before and after building the heap.
• Implement the algorithm Build Heap in C (or C++). Algorithm buildHeap (heap, size) set walker to 1 loop (walker < size) reheapUp (heap, walker) increment walker end loop • Write a program to test your program using a random integer array of size 30. Print the arrays before and after building the heap.
Write a java class, MaxHeap, to implement a max-heap of values of type double. Use an array and be prepared to grow the array. The array implementation will probably be more efficient. Next, write three java sorting methods: a) One should be the heapsort algorithm. b) the second should sort the array by inserting all the elements from the array into a heap defined by the MaxHeap class, and then removing all the items from the heap and putting them...
1. Write a Java program to implement Counting Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In...
Problem: Implement (in C) the dynamic program algorithm for the coin-change algorithm, discussed in class. Assume that the coins with which you make change are quarters, dimes, nickels and pennies. Thus you are going to set n = 4 in your program. The amount k for which you have to make change will be provided by the user and your program will return the minimum number of coins needed and also the break-up of the change in terms of the...
2. Write a Java program to implement Bucket Sort and write a driver to test it. Note: use random number generator to generate your input with n = 10, 50, and 100. Verify that the running time is O(n). 3. In this assignment you will investigate three different variants of algorithms that we have discussed in the class to solve the selection problem. Version 1. “Simultaneous minimum and maximum” selection algorithm. Version 2. Randomized selection algorithm. Version 3. “Median of...
Write a C++ program to implement Mergesort Algorithm discussed in class. Use the data file named random.data 1. Program description, Comments, Indentation and style etc. 2. Read data file, Load the array and print the array formatted 10 numbers in each line. 3. Implementation of Algorithm and correctness . 3. Print the sorted array formatted 10 numbers on each line. random.data file 43.72 60.30 60.21 30.41 75.77 38.38 92.03 27.37 0.52 3.43 80.52 40.45 3.84 68.44 66.70 54.14 52.88 32.04...
1.Dijkstra's Algorithm [10 pt] In class, we have discussed an implementation of Dijkstra's Algorithm using min-heap. Analyze the worst-case running time of an implementation of this algorithm using unordered linked-list (as the data structure for d(v), the upper bound on the shortest distance from source s to v). Give your answer in e. Justify your answer (and state any assumptions you make).
Implement quicksort and bucket sort. Use the code in your book to help; the partition in quicksort is tricky. Make sure your implementations are correct — it is easy to gain some confidence in the correctness of your code by writing a program which creates arrays filled with random numbers, sorts them, and then checks that they are sorted. Then time your code (using clock() or similar methods) on both methods for arrays filled with random integers of the following...
3. (50%) Use a programming language you familiar with to implement the brute force method and the branch and bound algorithm, both of which were already introduced in the class, for solving the traveling salesperson problem (35%). Compare their running time when the input size n is from 5 to 15 in steps of 1 (15%). Note that for each n, you should generate three problem instances and average the running time of solving these three instances. To verify the...