Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C Programming Language Code below runs fine, but runs into time limit error. Methods used are quick search and binary sort. Please provide more efficient

C Programming Language

image text in transcribed

Code below runs fine, but runs into time limit error. Methods used are quick search and binary sort. Please provide more efficient methods or optimize the code below to make it run faster. Linear methods are not fast enough.

#include

void inArr(int n, int arr[]) //initialize array { for (int i = 0; i

void printArr(int n, int arr[]) //print array {

for (int q = 0; q

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

*a = *b;

*b = t;

}

int partition(int arr[], int low, int high)

{

int pivot = arr[high]; // pivot

int i = (low - 1); // Index of smaller element

for (int j = low; j

{

// If current element is smaller than or

// equal to pivot

if (arr[j]

{

i++; // increment index of smaller element

swap(&arr[i], &arr[j]);

}

}

swap(&arr[i + 1], &arr[high]);

return (i + 1);

}

void quickSort(int arr[], int low, int high)

{

if (low

{

int pi = partition(arr, low, high);

quickSort(arr, low, pi - 1);

quickSort(arr, pi + 1, high);

}

}

int count(int array[], int search, int n)

{

int c, first, last, middle;

first = 0;

last = n - 1;

middle = first+(last - first) / 2;

while (first

{

if(array[middle]==search)

return middle+1;

else if (array[middle]

first = middle + 1;

}

else{ last = middle - 1;

} middle = first+(last - first) / 2; }

return middle;

}

int main()

{

int n; /umber of people

int q; /umber of questions

scanf("%d %d", &n, &q);

int arr[120000]; //contains the people

inArr(n, arr);

quickSort(arr, 0, n - 1);

// printArr(n, arr);

int ele; /umber of people to kill

int pow; //power needed to kill;

for (int i = 0; i

{

scanf(" %d", &pow);

ele = count(arr, pow, n);

printf("%d ", ele);

}

}

An evil dragon just wakes up from his slumber, he is so powerful that if he uses all his might, all the kingdom will perish. Of course, he won't do it because he wants to toy with mankind first. He knows that if he uses Y power, then al life Y meter around him will turn to dust in an instant. He wonders how much power should he use, then he offers you that he will spare you if you help him to calculate how many life will fall victim to his power. Because you don't want to die, you choose to help him. Format Input The first line will contain N and M, each denoting the number of people in the surrounding area and the number of question that the dragon will ask. The next line will consist of N integers a, each denoting the distance between them and the dragon. The next M lines consist of an integer Y, denoting the dragon's question "If I use Y power, how many people will fall victim?" Format Output For each question, print the answer of the dragon's question Constraints 1 <>

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2019 Wurzburg Germany September 16 20 2019 Proceedings Part 2 Lnai 11907

Authors: Ulf Brefeld ,Elisa Fromont ,Andreas Hotho ,Arno Knobbe ,Marloes Maathuis ,Celine Robardet

1st Edition

3030461467, 978-3030461461

More Books

Students also viewed these Databases questions

Question

P ( 1.23 Answered: 1 week ago

Answered: 1 week ago