473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Sort 3 numbers in a single line.

Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

is it possible without using an external function to swap two values?

Jul 22 '05 #1
9 9345

"Hasan Ammar" <am****@gmail.com> schrieb im Newsbeitrag
news:ci********@odak26.prod.google.com...
Lets suppose we have a function to sort 3 integers, is it possible
to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}
na = max(a, max(b,c));
nc = min(a, min(b,c));
nb = (na==a)?(nc==c ? b : c) : (nc==c ? b : a);
is it possible without using an external function to swap two
values?


for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);}
--
-Gernot
int main(int argc, char** argv) {printf
("%silto%c%cf%cgl%ssic%ccom%c", "ma", 58, 'g', 64, "ba", 46, 10);}

________________________________________
Looking for a good game? Do it yourself!
GLBasic - you can do
www.GLBasic.com
Jul 22 '05 #2
> for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);}


Holy smokes Batman!
Jul 22 '05 #3
Hasan Ammar wrote:
Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}
I think you need to think a bit more about what you want to do..

for a start, your function returns bool. You can't return a list of 3
sorted integers via a bool.

Do you want something like void sort(int &a,int &b,int &c) {..}, where
sort(x,y,z) will change the actual values of x,y and z, or do you want
something more like vector<int> sort(int a,int b,int c), where you
return a vector with the variables sorted?


is it possible without using an external function to swap two values?


Do you want to do this for any particular reason? Don't bother for
efficency reasons, as any decent compiler will optimise away a call to a
swapping function if the swapping function is sufficently small.
Jul 22 '05 #4
> for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^

(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);
....and that's just because I've declared war on macros.

ALL MACROS WILL SUFFER AND DIE A VERY PAINFUL DEATH, I WILL
SEE TO IT.

-JKop
Jul 22 '05 #5

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:b7******************@news.indigo.ie...
for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^

(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);
...and that's just because I've declared war on macros.

ALL MACROS WILL SUFFER AND DIE A VERY PAINFUL DEATH, I WILL
SEE TO IT.


Ah, c'mon. It's faster to write one that doing a function, but OK,
you're totally right.

template<class T> inline void swap(T&1, T&2)
{
(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);
}
Jul 22 '05 #6
In message <b7******************@news.indigo.ie>, JKop <NU**@NULL.NULL>
writes
for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^

(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);


swap(a, a);

--
Richard Herring
Jul 22 '05 #7
In message <2q*************@uni-berlin.de>, Gernot Frisch
<Me@Privacy.net> writes

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:b7******************@news.indigo.ie...
for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^

(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);
...and that's just because I've declared war on macros.

ALL MACROS WILL SUFFER AND DIE A VERY PAINFUL DEATH, I WILL
SEE TO IT.


Ah, c'mon. It's faster to write one that doing a function, but OK,
you're totally right.

template<class T> inline void swap(T&1, T&2)
{
(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);
}


swap(a, a);
--
Richard Herring
Jul 22 '05 #8
Gernot Frisch wrote:

"JKop" <NU**@NULL.NULL> schrieb im Newsbeitrag
news:b7******************@news.indigo.ie...
for integers only:
#define swap(a,b) {(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^

(a);}

I know this argument has been done to death but I'm going
to say it anyway:

swap(++i,k+=2);
...and that's just because I've declared war on macros.

ALL MACROS WILL SUFFER AND DIE A VERY PAINFUL DEATH, I WILL
SEE TO IT.


Ah, c'mon. It's faster to write one that doing a function, but OK,
you're totally right.

template<class T> inline void swap(T&1, T&2)
{
(a)=(b)^(a); (b)=(a)^(b); (a)=(b)^(a);
}


int i = 5;
swap( i, i );

-> i has a value of 0

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #9
Hasan Ammar wrote:
Lets suppose we have a function to sort 3 integers, is it possible to
sort and return them in a single statement?

example:

bool sort(int a, int b, int c) {
return // this is where the code goes.... ;
}

is it possible without using an external function to swap two values?

What does it suppose to mean to "sort three integers, int a, int b, int c"?

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Nova's Taylor | last post by:
Hi folks, I am a newbie to Python and am hoping that someone can get me started on a log parser that I am trying to write. The log is an ASCII file that contains a process identifier (PID),...
1
by: Kamilche | last post by:
I've written a generic sort routine that will sort dictionaries, lists, or tuples, either by a specified key or by value. Comments welcome! import types def sort(container, key = None,...
9
by: freddy | last post by:
how can I display a set of numbers like this: 1 2 3 4 5 6 ... instead of this 1 2 3 4 5 ....
9
by: Steve Wasser | last post by:
I need to sort a two-dimensional array. Each day I process a file with 9 comma-delimited fields, and varying amount of records (lines). I want to pull in each line, split it according to the comma...
7
by: Steve Crawford | last post by:
I am suffering some sort order confusion. Given a database, "foo", with a single character(4) column of data left padded with spaces I get: select * from foo order by somechars; somechars...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: Tony WONG | last post by:
i have a number of forms with fax numbers to come up into arrays and then combine to string. after that i design the flow 1. break the string to array now the string looks like this...
2
MMcCarthy
by: MMcCarthy | last post by:
The Bubble Sort is a very slow algorithm but it is one of the simplest which is why it is often used to introduce students to the concept of sorting. Imagine you are looking at numbers on a...
20
by: Bill Waddington | last post by:
This must be a FAQ - or several FAQs - but I can't quite seem to pin it down. I need to read a string from stdin which I will then process as digits with sscanf. I need to limit the # of chars...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.