Min-Max
Write an algorithm that finds both the smallest and
largest numbers in a list of n numbers and with complexity
T(n) is at most about (1.5)n comparisons.
Answers were Sorted based on User's Feedback
Answer / oracle
Simple, first compare all elements pairwise, total n/2
comparisons. Then, you got two sets, first set has all
greater elements, and second all smaller ones. Find largest
in the first set (n/2) and smallest in the second set
(n/2). You got both largest and smallest ones, in total n/2
+ n/2 + n/2 = 3n/2 comparisons.
Is This Answer Correct ? | 54 Yes | 11 No |
Answer / ashekur rahman
Time Complexity T(n) <= 3n/2
Proof:
We will calculate comparison in three steps.
1) compare pairwise will divide two sub array.
2) linear search of first sub array
3) linear search of second sub array
For even number of elements,
step 1 needs n/2 comparisons where both sub array contains
n/2 number of elements. Step 2 or 3 needs (n/2 - 1)
comparisons by linear search.
T(n) = n/2 + 2(n/2 - 1)
= 3n/2 - 1
Again,
For odd number of elements,
step 1 needs (n/2 + 1) comparison.
Now if first sub array contains n/2 number of elements then
second sub array contains (n/2 + 1) number of elements
or if first sub array contains (n/2 + 1) number of elements
then second sub array contains n/2 number of elements
Therefore,
T(n) = (n/2 + 1) + (n/2 + 1 - 1) + (n/2 - 1)
= 3n/2
So, T(n) = 3n/2 - 1, if n is even number
and T(n) = 3n/2, if n is odd number
Is This Answer Correct ? | 29 Yes | 4 No |
Answer / ashekur rahman
Please follow the link below again, where I have written a
complete C# program. Where I printed the time complexity also.
http://ashikmunna.blogspot.com/2009/05/write-algorithm-that-finds-both_28.html
Is This Answer Correct ? | 13 Yes | 1 No |
Answer / ashekur rahman
http://ashikmunna.blogspot.com/2009/05/write-algorithm-that-finds-both.html
Please follow the link to see the answer. Where I
implemented the algorithm and also calculated the time
complexity.
Thanks.
- Ashekur Rahman
Is This Answer Correct ? | 11 Yes | 1 No |
Answer / kush singh
1. Algorithm MaxMin (i,j, max, min)
2. // a[1:n] is a global array Parameter i and j are integers,
3. // 1 „T i „T j < n. The effect is to set max and min to the
4. // largest and smallest values in a [i, j], respectively.
5. {
6. if (i=j) then max:=min:=a[i]; //small(P)
7. else if (i = j ¡V 1) then //Another case of Small (P)
8. {
9. if a [i] < a[j] then
10. {
11. max:=a[j]; min:=a[i];
12. }
13. else
14. {
15. max:=a[i]; min:=a[j]
16. }
17. }
18. else
19. {
20. // If P is not small, divide P into subproblems
21. // Find where to split the set
22. mid:= „¾(i+j)/2„Î;
23. //Solve the subproblems
24. Maxmin (i, mid, max, min);
25. MaxMin (mid+i, j, max1, min1);
26. // Combine the solutions
27. if (max<max 1) then max:=max1;
28. if(min>min1) then min:=min1;
29. }
30. }
Is This Answer Correct ? | 4 Yes | 0 No |
Answer / srinivasan
#include<stdio.h>
#define size 10
void main()
{
int a[size]={5,3,18,7,20,0,2,1,16,13},i,j,count=0,t,max,min;
clrscr();
for(i=0;i<size;i++)
printf("%5d",a[i]);
for(i=0,j=size-1;i<j;i++,j--)
{
count++;
if(a[i]>a[j])
{
t=a[i],a[i]=a[j],a[j]=t;
}
}
printf("\n\n");
for(i=0;i<size;i++)
printf("%5d",a[i]);
printf("\n\n");
min=a[0];
for(i=1;i<size/2;i++)
{
count++;
if(a[i]<min)
min=a[i];
}
max=a[size/2];
for(j=size/2+1;j<size;j++)
{ count++;
if(a[j]>max)
max=a[j];
}
printf("%d\n%d is max,min",max,min);
printf("\n%d\n",count);
getch();
}
Is This Answer Correct ? | 10 Yes | 9 No |
Answer / amirhosain shahsavari
hi
Some of notations in answer 5 is absolutely wrong:
for even number n: T(n)=(3/2)n-2
for odd number n: we ignore the last item of array.Now you
can find Min1 (minimum of the array items ignoring the last
item), Max1 (maximum of the array items ignoring the last
item) (notice that n-1 is even so Min1 and Max1 can be found
at (3/2)(n-1)-2 comparisons). After that you should compare
Min1 and Max1 with the last item (that you ignored in the
first step). so you need at most (3/2)(n-1)-2+2=(3/2)n-(3/2)
comparisons to find the entire array items
Is This Answer Correct ? | 2 Yes | 1 No |
Answer / jeya
in tree the left most elemt is minimum element.the right
most element is max element.
Is This Answer Correct ? | 4 Yes | 7 No |
output for printf("printf");
write a function that allocates memory for a single data type passed as a parameter.the function uses the new operator and return a pointer to the allocated memory.the function must catch and handle any exception during allocation
what is the best algorithm to sort out unique words from a list of more than 10 million words(1 crore+)? we need the best technique in the terms of execution time.
Algorithm in O(2n) Presently we can solve in our hypothetical machine problem instances of size 100 in 1 minute using algorithm A, which is a O(2n). We would like to solve instances of size 200 in 1 minute using algorithm A on a new machine. What is the speed of the new machine should be?
2 Answers ABC, Qatar University,
Display Pattern: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 …
What output does this program generate as shown? Why? class A { A() { cout << "A::A()" << endl; } ~A() { cout << "A::~A()" << endl; throw "A::exception"; } }; class B { B() { cout << "B::B()" << endl; throw "B::exception"; } ~B() { cout << "B::~B()"; } }; int main(int, char**) { try { cout << "Entering try...catch block" << endl; A objectA; B objectB; cout << "Exiting try...catch block" << endl; } catch (char* ex) { cout << ex << endl; } return 0; }
hello friends, given an expression we have to remove the unwanted brackets in that expression. Eg : (a+b) ---> a+b (a+b)*(c)-----> (a+b)*c. Please mail me if you know the logic. My mail id is : saravana6m@gmail.com. Thank you in advance :-)
1 Answers GrapeCity, Microsoft,
U hv to enter a range from a and b and search hw many no. of times a pattern n. occurs between the range a and b. Eg :i/p:enter range :0 100 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 0 to 100:1 Eg :i/p:enter range :100 1000 Enter pattern: 13 o/p: the no. times 13 occurred betwwn 100 to 1000: (in this 13,113,131,132,133…139,213,313,…913 all these will be counted)
write a function -oriented program that generates the Fibonacci, the current numbers of n(as input) and display them (series). In Fibonacci, the current third number is the sum of the previous number.
Wrie a function which returns the most frequent number in a list of integers. Handle the case of more than one number which meets this criterion. public static int[] GetFrequency(int[] list)
#include<iostream.h> //main() //{ class A { friend class B; public: void read(); }; class B { public : int a,b; }; void A::read() { cout<<"welcome"; } main() { A x; B y; y.read(); } In the above program......, as B is a friend of A B can have access to all members,i cant access y.read . could you please tell me the reason and what would i code to execute this program?
How do I store linked list datas into an array?