where n is the size of array
On Sat, Sep 10, 2011 at 9:16 PM, sukran dhawan wrote:
>
>
> large1 = a[0];
> large2 = a[1];
>
>
> if(large1 < large2)
> swap(large1,large2)
>
> while(i < n)
> {
> if(a[i] > large1)
>{
> large2 = large1
> large1 = a[i]
> }
> else if(a[i] > large2)
> l
large1 = a[0];
large2 = a[1];
if(large1 < large2)
swap(large1,large2)
while(i < n)
{
if(a[i] > large1)
{
large2 = large1
large1 = a[i]
}
else if(a[i] > large2)
large2 = a[i]
}
// test the case if no of elements is 1 :)
On Sat, Sep 10, 2011 at 8:54 PM, Dave wrote:
> @Abhinav:
Well you can avoid that condition by comparing the number by:
1. Keeping two numbers, largest and second largest.
2. Comparing with the second largest. If it is greater than the second
largest, set second_largest = num. Else continue.
3. If second_largest > largest, swap(largest,second_largest).
O
sort it in quicksort (descending order)...den take arr[1] -->second largest
On Sat, Sep 10, 2011 at 8:34 AM, Akhilesh Vedhera wrote:
> Then the complexity will be nlogn not n and if it is the worst case
> then it would be O(n^2)...
>
> On Sat, Sep 10, 2011 at 8:58 PM, abhinav gupta
> wrote:
Then the complexity will be nlogn not n and if it is the worst case then
it would be O(n^2)...
On Sat, Sep 10, 2011 at 8:58 PM, abhinav gupta wrote:
> Oops ..no u hav to quicksort it.
>
>
> On Sat, Sep 10, 2011 at 8:24 AM, Dave wrote:
>
>> @Abhinav: Does it work correctly on {1, 3, 2}, or, f
Oops ..no u hav to quicksort it.
On Sat, Sep 10, 2011 at 8:24 AM, Dave wrote:
> @Abhinav: Does it work correctly on {1, 3, 2}, or, for that matter, on
> any array where the second largest comes after the largest?
>
> Dave
>
> On Sep 10, 10:16 am, abhinav gupta wrote:
> > temp2 is second largest
@Abhinav: Does it work correctly on {1, 3, 2}, or, for that matter, on
any array where the second largest comes after the largest?
Dave
On Sep 10, 10:16 am, abhinav gupta wrote:
> temp2 is second largest element.
>
> On Sat, Sep 10, 2011 at 8:00 AM, abhinav gupta
> wrote:
>
>
>
>
>
> > I can so
temp2 is second largest element.
On Sat, Sep 10, 2011 at 8:00 AM, abhinav gupta wrote:
> I can solve this problem in O(n)
> i=0;
> temp1=arr[0];
>
> while(i != len)
> {
> if(arr[i] > temp1)
> {
> temp2=temp1;
> temp1=arr[i]
> }
> i++;
> }
>
> On Sat, Sep 10, 2011 at 7:42 AM, Dave wrote:
>
>> @Re
I can solve this problem in O(n)
i=0;
temp1=arr[0];
while(i != len)
{
if(arr[i] > temp1)
{
temp2=temp1;
temp1=arr[i]
}
i++;
}
On Sat, Sep 10, 2011 at 7:42 AM, Dave wrote:
> @Replying to my own posting: remove the words "one of the numbers that
> lost to", so that the explanation reads
>
> The q
@Replying to my own posting: remove the words "one of the numbers that
lost to", so that the explanation reads
The question should be "How can we find the second largest element in
an array in n + ceiling(log_2(n)) - 2 comparisons?" The answer is to
use a tournament to select the largest number. T
+1.
On Sat, Sep 10, 2011 at 7:58 PM, Dave wrote:
> @Praveen: The question should be "How can we find the second largest
> element in an array in n + ceiling(log_2(n)) - 2 comparisons?" The
> answer is to use a tournament to select the largest number. The second
> largest number will have lost to
@Praveen: The question should be "How can we find the second largest
element in an array in n + ceiling(log_2(n)) - 2 comparisons?" The
answer is to use a tournament to select the largest number. The second
largest number will have lost to one of the numbers that lost to the
largest. It takes n - 1
12 matches
Mail list logo