Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Doubt about comparison function in sorting array of structures.

Правка en4, от rtanmay, 2018-08-01 22:37:26

Hello,

I was solving this problem. In this I have sorted the array of structures.

//This is my structure:
typedef struct node
{
	int a,b,c;
} node;
//This gets runtime error on test-32.
//Error: comparison doesn't meet irreflexive requirements, assert(!(a < a)).
bool compare(node n1, node n2)
{
	if(n1.c > n2.c) return false;
	else return true;
}
//However this gets accepted, only change is in >=
bool compare(node n1, node n2)
{
	if(n1.c >= n2.c) return false;
	else return true;
}

Why is this error coming? I thought even if we give only > then for equal value it will return true, so there should not be any problem between comparison of equal elements.

Thank you

Теги #sorting, comparison function

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en6 Английский rtanmay 2018-08-01 22:41:04 4
en5 Английский rtanmay 2018-08-01 22:40:19 138
en4 Английский rtanmay 2018-08-01 22:37:26 4 Tiny change: 'ge is in >**=**\nbool com' -> 'ge is in >=\nbool com'
en3 Английский rtanmay 2018-08-01 22:36:34 50
en2 Английский rtanmay 2018-08-01 22:33:24 8
en1 Английский rtanmay 2018-08-01 22:32:50 817 Initial revision (published)