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

Thanx in advance

Jump to Post> 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.

Jump to Post[edit]

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

Jump to PostYes, 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?

Jump to PostDepends 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 ;)

Jump to PostCleaner?

`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; } } }`

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

Traicey
10
Posting Whiz in Training

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

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());
```

Radical Edward
301
Posting Pro

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.

Ancient Dragon
commented:
you're right :)
+29

Traicey
10
Posting Whiz in Training

`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

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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?

Prabakar
77
Posting Whiz

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

Traicey
10
Posting Whiz in Training

**Edward** I thought I was just being defficult here or was just scared to think out of the box

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

`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.

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

[edit]

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

Ancient Dragon
commented:
Good catch
+29

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

`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.

Traicey
10
Posting Whiz in Training

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

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.

Traicey
10
Posting Whiz in Training

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

Prabakar
77
Posting Whiz

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?

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

Yes, the School Of Hard Knocks.

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

Traicey
10
Posting Whiz in Training

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

Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster

Traicey
commented:
Oh Boy, Im speechless
+1

Traicey
10
Posting Whiz in Training

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

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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 ;)

Traicey
commented:
Thanx a lot hey
+1

Traicey
10
Posting Whiz in Training

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

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;
}
}
}
```

Traicey
commented:
Thanx dude
+1

Traicey
10
Posting Whiz in Training

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; )

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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(;;)`

Traicey
10
Posting Whiz in Training

Nick Evan
4,005
Industrious Poster
Team Colleague
Featured Poster

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.

Traicey
10
Posting Whiz in Training

Be a part of the DaniWeb community

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