Is it possible to override the comparator < in C#?

  • Context: C# 
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Comparator
Click For Summary

Discussion Overview

The discussion centers around the possibility of overriding the less-than (<) operator in C#, with implications for sorting algorithms. Participants explore the theoretical and practical aspects of operator overriding and its relationship to sorting efficiency, particularly in the context of an O(1) sorting algorithm.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants question whether it is possible to override the < operator in C#, noting that C# does not support virtual operators for primitive types.
  • One participant expresses skepticism about the feasibility of an O(1) sorting algorithm, arguing that any sorting algorithm must read each element at least once.
  • Another participant suggests that redefining the < operator could allow sorting without examining the elements, although this claim raises further questions about the practicality of such an approach.
  • Concerns are raised about relying on compiler features if the < operator is overridden, suggesting that this would not achieve true O(1) complexity.
  • A later reply questions the original poster's intentions, suggesting that they may be trolling based on previous admissions in another thread.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the possibility of overriding the < operator or the validity of the proposed O(1) sorting algorithm. Multiple competing views remain, particularly regarding the relationship between operator overriding and sorting efficiency.

Contextual Notes

Participants express uncertainty about the definitions and implications of sorting algorithms and operator behavior in C#. There are unresolved questions about the assumptions underlying the claims made regarding sorting without examining elements.

SlurrerOfSpeech
Messages
141
Reaction score
11
Is it possible to override the < operator in C#? Note: I'm saying override, not overload.

I ask because, if there is, then I think I have discovered an O(1) sorting algorithm.
 
Technology news on Phys.org
SlurrerOfSpeech said:
Is it possible to override the < operator in C#? Note: I'm saying override, not overload.

I ask because, if there is, then I think I have discovered an O(1) sorting algorithm.
I'm not certain, but I don't think it is possible.According to the C# docs here, the base class would need to have a virtual < operator that you would override in your derived class. If you are comparing ordinary number values, the C# types for Int16, Int32, and Int64 don't have any operators defined on these structs. The Single (32-bit floating point) and Double (64-bit floating point) types do have operators defined for <, >, and so on, but these are public static operators, not virtual.
 
I am a bit puzzled about how you can image a sorting algorithm in general (that is, not only in a narrow special case, like if we know all elements are already sorted) can perform as O(1) considering it has to read each of the N element at least once. I am also puzzled why you think the existence of such an algorithm should relate to whether or not it is possible to override the less-than operator in C#. Can you perhaps explain what you mean in more detail?
 
Filip Larsen said:
I am a bit puzzled about how you can image a sorting algorithm in general (that is, not only in a narrow special case, like if we know all elements are already sorted) can perform as O(1) considering it has to read each of the N element at least once.

If I can redefine the meaning of < then I can ensure that an array is sorted without looking at any of its elements
 
SlurrerOfSpeech said:
If I can redefine the meaning of < then I can ensure that an array is sorted without looking at any of its elements
Maybe your code doesn't have to look at the elements of the array. But then you rely on some feature of the compiler with the overridden < to sort the array. And that will not be O(1).
 
  • Like
Likes   Reactions: FactChecker
SlurrerOfSpeech said:
If I can redefine the meaning of < then I can ensure that an array is sorted without looking at any of its elements

Can you please explain how you plan to sort an array of elements without looking at the elements themselves? And by "sort" I am referring to its usual meaning, like explained at [1].

[1] https://en.wikipedia.org/wiki/Sorting_algorithm
 
Folks, I am afraid the OP is trolling us. (He's admitted this in another thread)
 
Nothing more to say here. Thread closed.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
Replies
86
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
Replies
9
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K