0% found this document useful (0 votes)
41 views9 pages

Name - Aryan Gupta Reg. No. 199301088 Section - B

The document contains a student's name, registration number, and section for a class.

Uploaded by

bruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views9 pages

Name - Aryan Gupta Reg. No. 199301088 Section - B

The document contains a student's name, registration number, and section for a class.

Uploaded by

bruh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Name – Aryan Gupta

Reg. No. 199301088


Section – B

Ans.1)

Code –

// Heap Sort in C

#include <stdio.h>

// Function to swap the the position of two elements


void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}

void heapify(int arr[], int n, int i) {


// Find largest among root, left child and right child
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;

if (right < n && arr[right] > arr[largest])


largest = right;

// Swap and continue heapifying if root is not largest


if (largest != i) {
swap(&arr[i], &arr[largest]);
heapify(arr, n, largest);
}
}

// Main function to do heap sort


void heapSort(int arr[], int n) {
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// Heap sort
for (int i = n - 1; i >= 0; i--) {
swap(&arr[0], &arr[i]);

// Heapify root element to get highest element at root again


heapify(arr, i, 0);
}
}
// Print an array
void printArray(int arr[], int n) {
for (int i = 0; i < n; ++i)
printf("%d ", arr[i]);
printf("\n");
}

// Driver code
int main() {
int n;
printf("Enter array size: ");
scanf("%d",&n);
printf("Enter array elements:\n");
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
printf("Entered array:\n");
printArray(a,n);
heapSort(a, n);

printf("Heap sort: \n");


printArray(a, n);
}
Output –

Ans.2)

Code –

#include<stdio.h>
#include<malloc.h>
void insert();
void del();
void display();

struct node
{
int priority;
int info;
struct node *next;
}*start=NULL,*q,*temp,*new;

typedef struct node N;


int main()
{
int ch;
do
{
printf("\nEnter 1 for insertion 2 for deletion 3 for display and 4 for exit: ");
scanf("%d",&ch);
switch(ch)
{
case 1:insert();
break;
case 2:del();
break;
case 3:display();
break;
case 4:
break;
}
}
while(ch<4);
}
void insert()
{
int item,itprio;
new=(N*)malloc(sizeof(N));
printf("Enter element to be inserted : ");
scanf("%d",&item);
printf("Enter priority : ");
scanf("%d",&itprio);
new->info=item;
new->priority=itprio;
new->next=NULL;
if(start==NULL )
{
//new->next=start;
start=new;
}
else if(start!=NULL&&itprio<=start->priority)
{ new->next=start;
start=new;
}
else
{
q=start;
while(q->next != NULL && q->next->priority<=itprio)
{q=q->next;}
new->next=q->next;
q->next=new;
}
}

void del()
{
if(start==NULL)
{
printf("\nUnderflow\n");

}
else
{
new=start;
printf("\nDeleted element - %d\n",new->info);
start=start->next;
//free(start);
}
}

void display()
{
temp=start;
if(start==NULL)
printf("Empty queue\n");
else
{
if(temp!=NULL)
for(temp=start;temp!=NULL;temp=temp->next)
{
printf("\n%d priority =%d\n",temp->info,temp->priority);
//temp=temp->next;
}
}
}

Output –

You might also like