Can anyone explain how bubble sort work or give an example or a simple code that is bubble sorting

Thanx in advance

Recommended Answers

All 34 Replies

Took about 10 seconds to find the answer here Here

It's explained very well here
If you know some c++, it won't be too hard to write.
Come back if you have problems (and code)

Does it alwayz work like that, is thats how it is or there are any other ways to do it except the endless list of ununderstandable for statements

The bubble sort is probably the very easiest sort algorithm there is. What is so difficult to understand about this:

for (i=0; i<n-1; i++) {
  for (j=0; j<n-1-i; j++)
    if (a[j+1] < a[j]) {  /* compare the two neighbors */
      tmp = a[j];         /* swap a[j] and a[j+1]      */
      a[j] = a[j+1];
      a[j+1] = tmp;
  }
}

I don't really use the above algorithm, but this one

for(int i = 0; i < n-1; i++)
{
    for(int j = 0; i < n; j++)
    {
          if( a[i] < a[j] )
          {
               int temp = a[i];
               a[i] = a[j];
               a[j] = temp;
           }
       }
}

And in C++ you can do it in just one line: But I doubt this is a satisfactory solution to your assignment.

vector<int> array;
// fill array is not shown
//
// now sort it
std::sort(array.begin(),array.end());

> What is so difficult to understand about this:
Is that a trick question? Of course it's difficult to understand if you haven't learned how it works yet. :-O Edward learned long ago that just because something is easy for her, it's not easy for everyone else, and vice versa.

commented: you're right :) +29

Yaeh!!!! I have seen all that maybe I just dont want to understand it but honestly I would be lieying I I say I understand all o that, I just wish to know why for (i=0; i<n-1; i++) {for (j=0; j<n-1-i; j++) are we substracting 1 and - 1 - i what does that mean, if Dragon U can be kind enough to explain that to me, I would go home and rest


I don't really use the above algorithm, but this one

for(int i = 0; i < n-1; i++)
{
    for(int j = 0; i < n; j++)
    {
          if( a[i] < a[j] )
          {
               int temp = a[i];
               a[i] = a[j];
               a[j] = temp;
           }
       }
}

Correct me if I'm wrong but shouldn't this: for(int j = 0; i < n; j++) be: for(int j = 0; j< n; j++) Or else you will get out of array-boundary => crash?

Excuse me, wont the second code make an infinte loop, I mean, 'i' will be less than n the whole time

Thank you for understanding Edward I thought I was just being defficult here or was just scared to think out of the box

sorry, the second loop was incorrect. for(int j = i+1; j < n; j++) The difference between my first two algorithms is the differences between counting up or counting down. I prefer to count up because it seems more natural.

[edit]
Ah that solution is even better, it'll make less loops.

commented: Good catch +29

Yaeh!!!! I have seen all that maybe I just dont want to understand it but honestly I would be lieying I I say I understand all o that, I just wish to know why for (i=0; i<n-1; i++) {for (j=0; j<n-1-i; j++) are we substracting 1 and - 1 - i what does that mean, if Dragon U can be kind enough to explain that to me, I would go home and rest

The reason the first loop only goes to n-1 is because it isn't necessary to compare a[n-1] with a[n-1], where i and j both equal n-1. Bubble sort is the easiest algorithm to code, but also the slowest (normally). So anything that will reduce the number of comparisons will improve performance.

This is really encouraging, I always wander why are u guys so enthusiastic with your work or rather call it this, anyway when things get tougher I will know that ur just internet and Text away thanx

Correct me if I'm wrong but shouldn't this: for(int j = 0; i < n; j++) be: for(int j = 0; j< n; j++) Or else you will get out of array-boundary => crash?

Yes, and that's what I get for typing without testing.

Guys is there any school U know, where a mind can be expandend or defragged to improve perfomance

Yes, and that's what I get for typing without testing.

I realised that it was a typo error , thats why I did not probe on it much. Any way, Isnt that insertion sort?

Yes, the School Of Hard Knocks.

Yes, the School Of Hard Knocks

LOL!!! Im not kiding, where is that???, cos if there is such school tomoro Im packing all my belongs to there

The School of Hard Knocks is within each of us. Its experience, and millions of hours testing various algorithms to find the most efficient.

commented: Oh Boy, Im speechless +1

But thats time-consuming and probably suitable for people who are not handy-fully

Depends on how much sleep you need.

You can always stick a finger in a powersocket and hope your brain gets somehow rearranged (improved), but my instinct is telling me that the change of succes will be greater if you just practice , practice and practice ;)

commented: Thanx a lot hey +1

Oh well, easier said than done, but I will try

Member Avatar for DigitalPackrat

Cleaner?

for(int sort = 1; sort == 1;) {
        
        sort = 0;
        
        for(int i = 0; i < n - 1; i++) {
        
            if(a[i] < a[i+1]) {
            
                int temp = a[i];
                a[i] = a[i+1];
                a[i+1] = temp;
                
                sort = 1;
            }
        }
    }
commented: Thanx dude +1

I alwayz thought that there is only one way to do for loop statement which is for(int x = 0; x < 10; x++) but what DigitalPackrat did demonstrate something else and it also proves me wrong..... OK now can you please explain to me what that for loop does or this for loop does???

for(int sort = 1; sort == 1; )

a for loop works like this:

for ( init-expression ; cond-expression ; loop-expression )
{ 
    statement 
}

Your example only uses the init-expression amd cond-expression. So if 'sort' isn't changed in the statement-block this is an endless loop (you set 'sort' to 1, then loop while it's 1). This is normally written as: while(1) or for(;;)

So all in all u are saying I cant use for loop the way DigitalPackrat Did, that means what he demonstrated is unacceptable or illigal in C++

Sure you can. DigitalPackrat sets sort to 0 in the loop, so it won't be endless. I was just trying to give you an insite on for-loops .
I prefer another way to write this loop:

int sorted = 0;
while (!sorted)
{
}

It reads a bit easier.

Ok thanx, I just wanted to know coz really I thought there is no other way to do a for loop except the one I alwayz see

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.