Welcome to Dream.In.Code
Become a C++ Expert!

Join 150,016 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,469 people online right now. Registration is fast and FREE... Join Now!




ascending order without arrays

 
Reply to this topicStart new topic

ascending order without arrays

livefromyadkin
29 Nov, 2006 - 04:49 PM
Post #1

New D.I.C Head
*

Joined: 22 Sep, 2006
Posts: 24


My Contributions
hello again,

I've been searching and searching and can't find the answer to my question. "How do you sort in ascending order without using arrays?"

CODE


#include <iostream>
using namespace std;

int main()
{

    void pointerSort(int *, int *, int *, int *, int *, int *);

    int num1 = 349;
    int num2 = 493;
    int num3 = 943;
    int num4 = 394;
    int num5 = 439;
    int num6 = 934;


    cout <<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<endl<<num6<<endl;

    return 0;
}


Thanks!


User is offlineProfile CardPM
+Quote Post

okyup
RE: Ascending Order Without Arrays
29 Nov, 2006 - 05:04 PM
Post #2

D.I.C Head
Group Icon

Joined: 6 Nov, 2006
Posts: 207


Dream Kudos: 175
My Contributions
How about you send the numbers to a function. The function goes through and compares the first two. Use a temporary int, and put the larger of the values in int1, smaller in int2. Then compare int2 and int3 in the same manner. Loops this through enough that you switch all the variables into the right order.
User is offlineProfile CardPM
+Quote Post

msg555
RE: Ascending Order Without Arrays
29 Nov, 2006 - 06:04 PM
Post #3

D.I.C Head
Group Icon

Joined: 4 May, 2006
Posts: 136



Thanked: 5 times
Dream Kudos: 25
My Contributions
You can't really sort without arrays. It doesn't really make sense what you're askign. You might be able to implement a cheap hack but I'm doubt it would be guaranteed to work by the c++ standard.

Then best you could do is to case out each permutation. There are N! permuations of a set of N elements. You don't really want to do that.

This post has been edited by msg555: 29 Nov, 2006 - 06:06 PM
User is offlineProfile CardPM
+Quote Post

livefromyadkin
RE: Ascending Order Without Arrays
4 Dec, 2006 - 10:33 AM
Post #4

New D.I.C Head
*

Joined: 22 Sep, 2006
Posts: 24


My Contributions
What about a swap? something like this...

CODE

#include <iostream>
using namespace std;

int main()
{
    void pointerSort(int, int, int, int, int, int);

    int num1 = 349;
    int num2 = 493;
    int num3 = 943;
    int num4 = 394;
    int num5 = 439;
    int num6 = 934;

    pointerSort (int&num1, int&num2, int&num3, int&num4, int&num5, int&num6)
    cout <<num1<<endl<<num2<<endl<<num3<<endl<<num4<<endl<<num5<<endl<<num6<<endl;
    
    return 0;
}

void pointerSort (int&num1, int&num2, int&num3, int&num4, int &num5, int &num6)
{
   int temp;
   temp=num1;
   num1=num2;
   num2=temp;
}


I get errors but is this the right idea?

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 09:04PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month