A positive integer which is only divisible by 1 and itself is a prime number. Write a program to determine whether a positive integer, n, is prime and the worst-case running time of your program should be O(y/n). Please determine the running time of merge sort (a) sorted input (b) reverse-ordered input (c) random input
Expert Answer
int main()
{
int number, i, flag = 0;
printf(“Enter a positive integer: “);
scanf(“%d”,&number);
for(i=2; i<=number/2; ++i)
{
if(number%i==0)
{
flag=1;
break;
}
}
if (flag==0)
printf(“%d is a prime number.”,number);
else
printf(“%d is not a prime number.”,number);
return 0;
}
For given scenarios running time depends on implementation. When they are naively implemented, merge sort indeed uses O(n * log n) time.
Ans: O(n * log n) for all three scenarios.