From: Kamal on
How Icomparer works ?
I want to know the fundamental of its working ..
for 3 object in a array it does comparision 9 times so how the comparisation
is going on?
can any one help me.

From: sloan on

You can look at an example I did here:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!141.entry

..............

The gist of it is this:

internal sealed class EmployeeComparer : IComparer

{

public int Compare( Employee x , Employee y )

{

//////Write your own logic here that takes 2 Employees (x and y) and then
compares them ANYWAY YOU CHOOSE.

// You write the code which makes 1 Employee come before the other
Employee.........whether it would be LastName, HireDate or whatever
Property(ies) you pick.

}



}


That example is 1.1'ish, but you can learn the basic concept.





"Kamal" <Kamal(a)discussions.microsoft.com> wrote in message
news:5EBEBF1C-CDF6-45DD-91F4-C6D8F4F9B258(a)microsoft.com...
> How Icomparer works ?
> I want to know the fundamental of its working ..
> for 3 object in a array it does comparision 9 times so how the
> comparisation
> is going on?
> can any one help me.
>


From: Peter Duniho on
Kamal wrote:
> How Icomparer works ?
> I want to know the fundamental of its working ..
> for 3 object in a array it does comparision 9 times so how the comparisation
> is going on?
> can any one help me.

If you can be more specific about your question, I'm sure we can.

The IComparer interface does not dictate when it's called at all, never
mind how many times. And the way it works is the way any interface
works: it defines specific members that must be implemented by
implementors, and then a reference to implementors can be passed as the
interface type itself so that other code can access those members.

If you are using IComparer in a particular context — for example, by
passing it a Sort() method of a type like Array — then the real question
is how the Sort() method works, which is independent of which interface
is used for comparisons. In general, .NET sorting methods use
quicksort, which will call the comparison function being used
(IComparer.Compare, if you're using IComparer) each time the algorithm
needs to compare two objects.

The quicksort algorithm has a "big-O" complexity of "O(n log n)", where
n is the number of elements being sorted. That means that the number of
iterations in the algorithm will be roughly proportional to n log n.
But it's not exact. It just happens that for the input data you're
using, the 3-element array requires comparisons between the elements of
the array 9 times.

That actually seems high to me; after all, a plain bubble sort could
reliably sort a 3-element array with 3 comparisons (it's an O(n^2)
algorithm, but for very small values of 'n', the big-O notation is very
inaccurate).

I would expect a quicksort algorithm to do about the same. Even if the
implementation picks the pivot randomly and so has to compare the pivot
element with itself, you get something like this:

• iteration 1: three comparisons to build two partitions
• iteration 2: two comparisons to build two more partitions (one of
the partitions is already only length 1)
• iteration 3: all partitions are length 1, no more comparisons needed

So not as good as bubble sort, but still only five comparisons, not
nine. So I couldn't explain why the .NET quicksort implementation does
9 comparisons, without taking the time to actually look at the exact
implementation. I'll leave doing that as an exercise for the reader. :)

Pete