sort

Go4Expert Member
5Nov2010,16:09   #1
wdliming's Avatar
Complete the sorting of three numbers from small to big:
Code:
#include <stdio.h>
#include <stdlib.h>
void Exchg(int *px, int *py)
{
    int tmp;
    tmp = *px;
    *px = *py;
    *py = tmp;    
}
int main(int argc, char *argv[])
{
    int x,y,z,t;
    while(scanf("%d%d%d",&x,&y,&z))
    {
        if(x > y)
            Exchg(&x,&y);      /*change x,y*/
        if(x > z)
            Exchg(&x,&z);      /*change x,z*/
        if(y > z)
            Exchg(&y,&z);      /*change z,y*/
        printf("small to big: %d %d %d\n",x,y,z);
    }    
    system("PAUSE"); 
    return 0;
}
compile successfully in dev c++

Last edited by shabbir; 5Nov2010 at 18:47.. Reason: Code blocks
Go4Expert Member
7Nov2010,04:53   #2
ihatec's Avatar
You don't use variable t, while looks like an infinite loop, swapping is ok. If you want to sort a few numbers, you may use array or some ohter data structure and use some sorting algorithm.
Quote:
Originally Posted by wdliming View Post
Complete the sorting of three numbers from small to big:
Code:
#include <stdio.h>
#include <stdlib.h>
void Exchg(int *px, int *py)
{
    int tmp;
    tmp = *px;
    *px = *py;
    *py = tmp;    
}
int main(int argc, char *argv[])
{
    int x,y,z,t;
    while(scanf("%d%d%d",&x,&y,&z))
    {
        if(x > y)
            Exchg(&x,&y);      /*change x,y*/
        if(x > z)
            Exchg(&x,&z);      /*change x,z*/
        if(y > z)
            Exchg(&y,&z);      /*change z,y*/
        printf("small to big: %d %d %d\n",x,y,z);
    }    
    system("PAUSE"); 
    return 0;
}
compile successfully in dev c++
shabbir like this
Go4Expert Member
7Nov2010,07:07   #3
wdliming's Avatar
Thank you for your advices !!I get it!