Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, i have this code in C, where I have to work with heaps. I have a menu with choices for each function. The heapSort

Hi, i have this code in C, where I have to work with heaps. I have a menu with choices for each function. The heapSort and the insertNode do not work properly. Thanks for your time!

#include #include

void swap(int x, int y); void heapify(int Array[], int n, int i); void buildHeap(int arr[], int n); void insertNode(int arr[], int n, int Key); void heapsort(int Array[], int n); void PrintArray(int Array[], int n);

/*-------------------------------------------------------------------*/ void swap(int x, int y) { int temp = x; x=y; y=temp; } /*-------------------------------------------------------------------*/ void heapify(int arr[], int n, int i) { int largest = i; int l = 2*i + 1; int r = 2*i + 2; if (l < n && arr[l] > arr[largest]) largest = l; if (r < n && arr[r] > arr[largest]) largest = r; if (largest != i) { swap(arr[i], arr[largest]); heapify(arr, n, largest); } } /*-------------------------------------------------------------------*/ void buildHeap(int arr[], int n) { int i; int startIdx = (n / 2) - 1; for (i = startIdx; i >= 0; i--) { heapify(arr, n, i); } } /*-------------------------------------------------------------------*/ void insertNode(int arr[], int n, int Key) { n = n + 1; arr[n - 1] = Key; heapify(arr, n, n - 1); } /*-------------------------------------------------------------------*/ void heapSort(int arr[], int n) { int i; for (i = n / 2 - 1; i >= 0; i--) heapify(arr, n, i); // Heap sort for (i=n-1; i>=0; i--) { swap(arr[0], arr[i]); heapify(arr, i, 0); } } /*-------------------------------------------------------------------*/ void PrintArray(int Array[], int n) { int i; for (i=0; i

/*-------------------------------------------------------------------*/ int main (){

int i,A[20],ar; int ex=0; puts("|Menu Epilogwn : |"); puts("|1.Create random number array|"); puts("|2.Create heap from the array|"); puts("|3.Add new element in the heap|"); puts("|4.Print heap|"); puts("|5.Order and print ordered array|"); puts("|6.Exit|"); do { scanf("%d", &ex); switch (ex){ case 1: srand(time(0)); for (i=0;i<20;i++) A[i]=rand() % 100 + 1; break; case 2: buildHeap(A,20); break; case 3: puts("Add a number : "); scanf("%d", &ar); insertNode(A,20,ar); break; case 4: PrintArray(A,20); break; case 5: heapSort(A,20); PrintArray(A,20); break; case 6: puts("Exit!"); break;} }while (ex!=6); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

When would you change your will? How can your will be changed?

Answered: 1 week ago

Question

What guides and references exist for the Amazon EC 2 service?

Answered: 1 week ago

Question

What are the role of supervisors ?

Answered: 1 week ago