Click here to Skip to main content
15,886,626 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I want to Ascend or descend numbers in the array without using
<
and
>
symbols. And also don't use predefined functions like sort() and so on...

Give me some solutions to solve this problem.

What I have tried:

I tried with different methods, But I couldn't find any way without using < and > symbols.
Posted
Updated 9-Jul-18 21:03pm
Comments
Patrice T 9-Jul-18 14:22pm    
What is your problem with using < or > ?

You can subtract two numbers and then test the sign of the result. A negative value means the second was larger than the first. You can determine the sign by testing the most significant bit - if it is set then it is a negative number. That can be done by masking off all other bits and comparing with zero using '==', so there is no greater than or less than comparison.
 
Share this answer
 
Comments
[no name] 9-Jul-18 14:02pm    
+5. What I think to remember, there is also a std::signbit method finally available
CPallini 10-Jul-18 3:05am    
5.
Here is a quick method that makes no assumptions about the size of int, because the size is implementation dependent. The only assumption is that a byte is 8-bits.

bool isLess(int n1, int n2)
{
	const int mask = 1 << ((sizeof(int)<<3)-1);
	int dif = (n1-n2);
	return ((dif & mask) != 0);
}
 
Share this answer
 
Comments
CPallini 10-Jul-18 3:07am    
5.
C++
#define LESS <

:-D
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900