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

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




This piece of code is wrong but I dont know why

 
Reply to this topicStart new topic

This piece of code is wrong but I dont know why, shuttle sort algorithm

TheMagnitude
12 Jan, 2008 - 03:53 PM
Post #1

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
CODE
#include <iostream>
using namespace std;

void shuttle(int mydata[])
{
    int sort;
    unsigned short u=sizeof(mydata)-1, h, k, j, o, s;
    bool breakout;
    for (h=0;h<u;++h)
    {
        for (k=0;k<u;++k)
            cout << mydata[k] << "  ";
        cout << endl;
        sort=mydata[h+1];
        for (j=0;j<h+1;++j)
        {
            breakout=false;
            if (sort<mydata[j])
            {
                breakout=true;
                o=j;
            }
            if (breakout==true)
                break;
        }
        if (breakout==false)
        {
            for (s=h;s>=o;--s)
                mydata[s+1]=mydata[s];
            mydata[o]=sort;
        }
    }
    for (k=0;k<u;++k)
        cout << mydata[k] << "  ";
    cout << endl;
}

int main()
{
    unsigned short u, h;
    while (1)
    {
        cout << "Enter number of items to shuttle sort" <<endl;
        cin >> u;
        if (u==0)
            continue;
        int mydata[u];
        for (h=0;h<u-1;++h)
        {
            cout << "Enter item number " << h+1 << endl;
            cin >> mydata[h];
        }
        shuttle(mydata);
    }
    return 0;
}


This is what I want to happen:
CODE
Enter number of items to shuttle sort
5
Enter item number 1
3
Enter item number 2
10
Enter item number 3
5
Enter item number 4
1
Enter item number 5
4
3  10  5  1  4
3  10  5  1  4
3  5  10  1  4
1  3  5  10  4
1  3  4  5  10


What actually happened was:
CODE
Enter number of items to shuttle sort
5
Enter item number 1
3
Enter item number 2
10
Enter item number 3
5
Enter item number 4
1
3  10  5


Im very new to c++ and have been trying to find out why this isnt working, but its driving me crazy, if you can help me thanks.

Adam
User is offlineProfile CardPM
+Quote Post

baavgai
RE: This Piece Of Code Is Wrong But I Dont Know Why
12 Jan, 2008 - 05:05 PM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
The term "shuttle" sort is, unfortunately, horribly imprecise. It can refer to a selection sort, an insertion sort, or a bidirectional bubble sort. Maybe more. From your code, I'm guessing insertion sort.

You have a number of problems, but I'll stick to the C++ gotchas. The statement u=sizeof(mydata)-1 may be wishful thinking. You're much better off to just passing the size of the array along with the array itself.

Here's a little bit of code you should ultimately find useful.

CODE

void showData(int mydata[], int size) {
    for (int i=0; i<size; i++) {
        cout << mydata[i] << "  ";
    }
    cout << endl;
}


Take a look about for working sorts to get an idea what you're aiming for. That bool breakout is kind of quirky.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 01:25 AM
Post #3

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
Heres my new code:
CODE
#include <iostream>
using namespace std;
void showData (int mydata[], int size)
{
    for (int k=0;k<size;k++)
        cout << mydata[k] << "  ";
    cout << endl;
}

void shuttle(int mydata[], int size)
{
    int sort;
    unsigned short element;
    bool breakout;
    for (int h=0;h<size;++h)
    {
        showData(mydata, size);
        sort=mydata[h+1];
        breakout=false;
        for (int j=0;(j<h+1 && breakout==false);++j)
        {
            if (sort<mydata[j])
            {
                breakout=true;
                element=j;
            }
            //if (breakout==true)
            //    break;
        }
        if (breakout==false)
        {
            for (int s=h;s>=element;--s)
                mydata[s+1]=mydata[s];
            mydata[element]=sort;
        }
    }
    showData(mydata, size);
}

int main()
{
    unsigned short u;
    while (1)
    {
        cout << "Enter number of items to shuttle sort" <<endl;
        cin >> u;
        if (u==0)
            continue;
        int mydata[u];
        for (int h=0;h<u;++h)
        {
            cout << "Enter item number " << h+1 << endl;
            cin >> mydata[h];
        }
        shuttle(mydata, u);
    }
    return 0;
}


Which gives more or less the same result.

Also I forgot to mention that the program encounters an error when the actual sorting takes place and needs to close.

Am I doing anything illegal in the code?
User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 02:35 AM
Post #4

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
Ok Ive fixed it now, it was encounting an error because I was giving a funtion a unsigned short instead of an int (it stopped giving me errors when i changed it anyway). Thanks for your help baavgai.

This post has been edited by TheMagnitude: 13 Jan, 2008 - 02:35 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 03:15 AM
Post #5

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
This is still a sloppy sort, kind of a worst implementation of an insertion sort. Still, I got something like what you're offering to work:

CODE

void shuttle(int mydata[], int size) {
    int sort;
    unsigned short element;
    // you don't want to look at the last element it will be in order when done
    for (int h=0;h<size-1;++h) {
        showData(mydata, size);
        sort=mydata[h+1];
        bool breakout=false;
        for (int j=0; j<h+1; ++j) {
            if (sort<mydata[j]) {
                breakout=true;
                element=j;
                break;
            }
        }
        // you need to move data around if breakout
        // otherwise, you're not doing anything
        if (breakout) {
            for (int s=h;s>=element;--s)
                mydata[s+1]=mydata[s];
            mydata[element]=sort;
        }
    }
    showData(mydata, size);
}


Hope this helps.

User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 03:47 AM
Post #6

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
My end code was:
CODE
#include <iostream>
using namespace std;
void showData (int mydata[], int size)
{
    for (int k=0;k<size;k++)
        cout << mydata[k] << "  ";
    cout << endl;
}

void shuttle(int mydata[], int size)
{
    int element;
    int sort;
    cout << "Original data:" << endl;
    showData(mydata,size);
    cout << "Sorting:" << endl;
    for (int h=0;h<size-1;++h)
    {
        sort=mydata[h+1];
        bool breakout=false;
        for (int j=0;(j<h+1 && breakout==false);++j)
        {
            if (sort<mydata[j])
            {
                breakout=true;
                element=j;
            }
        }
        if (breakout==true)
        {
            for (int s=h;s>=element;--s)
                mydata[s+1]=mydata[s];
            mydata[element]=sort;
        }
        showData(mydata, size);
    }
    showData(mydata, size);
}

int main()
{
    int u;
    while (1)
    {
        cout << "Enter number of items to shuttle sort" <<endl;
        cin >> u;
        if (u==0)
            continue;
        int mydata[u];
        for (int h=0;h<u;++h)
        {
            cout << "Enter item number " << h+1 << endl;
            cin >> mydata[h];
        }
        shuttle(mydata, u);
    }
    return 0;
}


Also I have a question. in your code:
CODE
if (sort<mydata[j]) {
                breakout=true;
                element=j;
                break;
            }

Doesnt the break; statement just break out of the if statement but doesnt break out of the for statement?
User is offlineProfile CardPM
+Quote Post

baavgai
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 05:05 AM
Post #7

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(TheMagnitude @ 13 Jan, 2008 - 06:47 AM) *

Doesnt the break; statement just break out of the if statement but doesnt break out of the for statement?


The break statement exists to exit loops. Specifically, to avoid the flag system you're using. You don't need to break from a conditional block, more conditional logic can take care of that. Early loop exits are a special case, however; thus the break.

Also, since the break you're using is inside your if, and you set flag to true and then process on true, you actually don't need it at all.

Here's a slight variation of that final code.

CODE

void shuttle(int mydata[], int size) {
    cout << "Original data:" << endl;
    showData(mydata,size);
    cout << "Sorting:" << endl;
    for (int h=0;h<size-1;++h) {
        int sort=mydata[h+1];
        for (int j=0; j<h+1;++j) {
            if (sort<mydata[j]) {
                for (int s=h;s>=j;--s)
                    mydata[s+1]=mydata[s];
                mydata[j]=sort;
                break;
            }
        }
        showData(mydata, size);
    }
    showData(mydata, size);
}


User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 05:43 AM
Post #8

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
Nice one! Thanks alot now my code looks very neat and tidy and i have learned a bit aswell.

Im going to try and do the sorting algorithms: shuttle, bubble, shell, and quicksort.

Then do the pathfinding algorithms: chinese postman, dijkstra (or however you spell it XD), prim, kruskal, nearest nieghbour etc.

Im new to C++ so im trying to do all the algorithms from my Desicion 1 module of my A levels to get into the swing of things.

Also I was wondering why I can't use the PM system?

User is offlineProfile CardPM
+Quote Post

baavgai
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 07:32 AM
Post #9

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,047



Thanked: 106 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(TheMagnitude @ 13 Jan, 2008 - 08:43 AM) *

Also I was wondering why I can't use the PM system?


No clue, never used it. tongue.gif

As for problems to play with; make your own! Those little algorithms implementations come from the bad old days of math professors being clueless as to what a computer can do. They're entertaining in there own way, but don't really do it for some folks.

Nothing will get you more inspired than having a project that you want to make. First tries can suck, but that's not the point, you'll always learn something.

On this site people post their homework all the time. Don't give them the answer, but try your hand at the homework. A lot of the problems are fairly entertaining. You can learn from any code, good or bad; you just learn different stuff. wink2.gif

User is offlineProfile CardPM
+Quote Post

jjhaag
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 07:12 PM
Post #10

me editor am smartastic
Group Icon

Joined: 18 Sep, 2007
Posts: 1,789



Thanked: 2 times
Dream Kudos: 775
Expert In: C,C++

My Contributions
QUOTE(TheMagnitude @ 13 Jan, 2008 - 06:43 AM) *

Also I was wondering why I can't use the PM system?


You need to have at least 10 posts to PM other users - it's a feature to prevent abuse and spam.
User is offlineProfile CardPM
+Quote Post

TheMagnitude
RE: This Piece Of Code Is Wrong But I Dont Know Why
13 Jan, 2008 - 11:24 PM
Post #11

D.I.C Head
Group Icon

Joined: 12 Jan, 2008
Posts: 88


Dream Kudos: 125
My Contributions
QUOTE(baavgai @ 13 Jan, 2008 - 08:32 AM) *

QUOTE(TheMagnitude @ 13 Jan, 2008 - 08:43 AM) *

Also I was wondering why I can't use the PM system?


No clue, never used it. tongue.gif

As for problems to play with; make your own! Those little algorithms implementations come from the bad old days of math professors being clueless as to what a computer can do. They're entertaining in there own way, but don't really do it for some folks.

Nothing will get you more inspired than having a project that you want to make. First tries can suck, but that's not the point, you'll always learn something.

On this site people post their homework all the time. Don't give them the answer, but try your hand at the homework. A lot of the problems are fairly entertaining. You can learn from any code, good or bad; you just learn different stuff. wink2.gif

Thanks for your advice.

What I'd like to do when I am pretty fluent in C++ is make a 2D physics engine. Im really looking forward to doing that smile.gif I think thats a good project, may be a bit hard though but I always aim to high when programming tongue.gif

What Id like to know is where people post there projects in progress or completed for other people to download and comment on (im not on about code snippets and tutorials but compiled exes and whatnot)?

QUOTE(jjhaag @ 13 Jan, 2008 - 08:12 PM) *

QUOTE(TheMagnitude @ 13 Jan, 2008 - 06:43 AM) *

Also I was wondering why I can't use the PM system?


You need to have at least 10 posts to PM other users - it's a feature to prevent abuse and spam.

Oh ok fair enough tongue.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/5/08 04:22AM

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