Click here to Skip to main content
15,890,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 23:13pm

1 solution

To sort numbers (or anything for that matter) you have to compare them first to know where they fit...
As you do not want to use the basic compare-operators (<, >, =) you have to learn bit-wise operations...
Let see a sample
C++
int x, y;

bool xGTy = (((x + y) ^ (x - y)) < 0);

xGTy (x-grater-than-y) is true if (x + y) and (x - y) has different signs, which means x greater than y (assuming positive integers)...
The same - very complicated - way you can built functions that can compare numbers using only bit-wise operations...
There is an interesting paper about it: Bit Twiddling Hacks[^]
 
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