Bubble sort

This is a discussion on Bubble sort within the C++ Programming forums, part of the General Programming Boards category; Hey guys i'm trying to get the bubble sort to work in my if(choice==2) statement but it's not working right ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    6

    Bubble sort

    Hey guys i'm trying to get the bubble sort to work in my if(choice==2) statement but it's not working right at all. Any ideas?
    Thanks!

    Code:
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
        int i,num[20],n,j,choice,tmp;
    
        cout<< "Please enter 20 integers";
        for (i=0; i<20; i++)
        {
            cout<<"\nEnter next value:";
            cin>>num[i];
    
        }
    
        cout<<"\n1.Display original data.\n";
        cout<<"2.Sort the data into descending order\n";
        cout<<"3.Display the sorted data (Only if you've already sorted)\n";
        cout<<"4.Get the address of the first element of array.\n\n";
        cin>>choice;
    	
        if (choice==1)
        {
    
            for (i=0; i<20; i++)
            {
                cout<<"\n"<<num[i]<<endl;
            }
    		return 0;
    	}
    	else if (choice==2)
            {
    			n=20;
                for (i=0; i<n; i++)
                {
                    for (j=0; j<n-i; j++)
    				{
                        if (num[j+1] < num[j])    /* compare the two neighbors */
                        {
                            tmp = num[j];         /* swap a[j] and a[j+1]      */
                            num[j] = num[j+1];
                            num[j+1] = tmp;
    
                            cout<<"Here are your numbers:"<<tmp<<endl;
    
                        }
    				}
                }
    		}
        
    }

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,169
    Overflow. (j=i=19+1)

    Soma

  3. #3
    Registered User guesst's Avatar
    Join Date
    Feb 2008
    Location
    Lehi, UT
    Posts
    179
    Quote Originally Posted by Trckst3 View Post
    Hey guys i'm trying to get the bubble sort to work in my if(choice==2) statement but it's not working right at all. Any ideas?
    Thanks!

    Code:
    #include <iostream>
    #include <iomanip>
    #include <algorithm>
    
    using namespace std;
    
    int main() {
        int i,num[20],n,j,choice,tmp;
    
        cout<< "Please enter 20 integers";
        for (i=0; i<20; i++) {
            cout<<"\nEnter next value:";
            cin>>num[i];
    
        }
    
        cout<<"\n1.Display original data.\n";
        cout<<"2.Sort the data into descending order\n";
        cout<<"3.Display the sorted data (Only if you've already sorted)\n";
        cout<<"4.Get the address of the first element of array.\n\n";
        cin>>choice;
    
        if (choice==1) {
    
            for (i=0; i<20; i++) {
                cout<<"\n"<<num[i]<<endl;
            }
            return 0;
        } else if (choice==2) {
            n=20;
            for (i=0; i<=n; i++) {
                for (j=i; j<=n; j++) {
                    if (num[j+1] < num[j]) {  /* compare the two neighbors */
                        tmp = num[j];         /* swap a[j] and a[j+1]      */
                        num[j] = num[j+1];
                        num[j+1] = tmp;
    
                        cout<<"Here are your numbers:"<<tmp<<endl;
    
                    }
                }
            }
        }
    }
    Just cleaning up your formatting there a bit.

    The problem is in your two loops. You have to check the extremes and you'll find that you're checking data outside the loop. Changing the '<'s to '<='s should fix the problem. But I haven't run it so I may have gone the wrong direction. That is to say I maybe should have suggested the 'n's in your for statements be 'n-1's. I'm kinda groggy right now, so I can't think.
    Type-ins are back! Visit Cymon's Games at http://www.cymonsgames.com for a new game every week!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,294
    Also:
    You are missing a main loop. How do you expect to enter 1 and then enter 2? That return statement wont play nicely with a loop either.
    Case 3 is the same as case 1.
    What do you expect number 4 to do? It seems pretty pointless to me.

    This should not be there, you need to remove it.
    Code:
                            cout<<"Here are your numbers:"<<tmp<<endl;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bubble sort not working... wats d prob?
    By Huskar in forum C Programming
    Replies: 8
    Last Post: 03-31-2009, 11:59 PM
  2. My bubble sort only sorts once
    By Muller in forum C Programming
    Replies: 8
    Last Post: 03-27-2009, 04:36 PM
  3. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 01:30 AM
  4. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM
  5. Bubble Sort, Qucik Sort
    By insomniak in forum C Programming
    Replies: 2
    Last Post: 03-15-2003, 03:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21