Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Coding Forum


You are currently viewing our Coding Forum as a guest. Please register to participate.
Login



Closed Thread
Old 04-18-2007, 04:53 PM C++ Bubble sort
mat
Junior Talker

Posts: 1
Trades: 0
Hello,

I found the code for bubble sort in C++.
With the code below the print screen will be:
23
8
7
6
6
5
2
2
1

Is it possible that sorts the numbers so the print screen will show all changes. In example:

9,6,5,23,2,6,2,7,1,8
9,6,23,5,2,6,2,7,1,8
....
....
....
23,9,8,7,6,6,5,2,2,1


Thanking in advance

Code:
#include <stdio.h>
#include <iostream.h>

void bubbleSort(int *array,int length)//Bubble sort function 
{
    int i,j;
    for(i=0;i<10;i++)
    {
        for(j=0;j<i;j++)
        {
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap 
                array[i]=array[j];
                array[j]=temp;
            }

        }

    }

}

void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
        cout<<array[i]<<endl;
}


void main()
{

    int a[]={9,6,5,23,2,6,2,7,1,8};   // array to sort 
    bubbleSort(a,10);                 //call to bubble sort  
    printElements(a,10);               // print elements
}
mat is offline
View Public Profile
 
 
Register now for full access!
Old 04-18-2007, 09:07 PM Re: C++ Bubble sort
Ultra Talker

Posts: 483
Trades: 0
Two changes you would need to make.

1) Change your printElements function to display all of the items on the same line.
Change:
Code:
void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
        cout<<array[i]<<endl;
}
to:
Code:
void printElements(int *array,int length) //print array elements
{
    int i=0;
    for(i=0;i<10;i++)
        cout<<array[i]<<", ";
    cout << endl;
}
2) Call printElements after you do the swap code in the bubbleSort function.

Change:
Code:
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap 
                array[i]=array[j];
                array[j]=temp;
            }
to:
Code:
            if(array[i]>array[j])
            {
                int temp=array[i]; //swap 
                array[i]=array[j];
                array[j]=temp;

                printElements(array, length);
            }
All of that's off the top of my head, so no guarantees as to the correctness of code, but the ideas should be sound. By the way, you're passing in a length parameter to both printElements and bubbleSort, but have '10' hardcoded in your for loops: I'm guessing you really want to change that to the length parameter.

Hope that helps...
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
View Public Profile
 
Old 04-18-2007, 09:17 PM Re: C++ Bubble sort
Average Talker

Posts: 25
Trades: 0
Just a suggestion, even though BubbleSort is the easiest type of sorting, it's also the slowest, really slow. I suggest you go on Wikipedia and look through the many sorting algorithms there are (i.e. Shear sort) along with the pseudocode, which can be easily translated to c++
ryogo is offline
View Public Profile
 
Old 04-19-2007, 06:23 PM Re: C++ Bubble sort
NullPointer's Avatar
Will Code for Food

Posts: 2,883
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by ryogo View Post
Just a suggestion, even though BubbleSort is the easiest type of sorting, it's also the slowest, really slow. I suggest you go on Wikipedia and look through the many sorting algorithms there are (i.e. Shear sort) along with the pseudocode, which can be easily translated to c++
If you're just sorting a small array or something like that the cost of implementing a more advanced algorithm such as merge sort or a binary insertion sort would be too high. You would waste time to shave miliseconds off your run time and you might actually slow it down because of contruction time. But if you're going to be sorting a huge database or something consider a different method.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is offline
View Public Profile Visit NullPointer's homepage!
 
Closed Thread     « Reply to C++ Bubble sort
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15485 seconds with 11 queries