Bubble sort

This is a discussion on Bubble sort within the C Programming forums, part of the General Programming Boards category; Hello. I have a doubt about this code
Code:
#include <stdio.h>
void bubble_sort(int []);
#define LEN 5
int main( void ...

I think the i>=1 it does one more passing that is unnecessary because the array is already sorted (after 3rd passing ).... I think i > 1 is the right. What is your opinion about that ? I had an argue with a friend about this.

well i ran your code and it printed 2 5s at the end of the sorting looking at the algorithm i think the mixup comes when j goes up to 4 and i comes down to 1 and there is the case when i and j are equal causing a problem.. let me edit and run..

I won't pretend to have gotten enough sleep last night to settle this myself, but don't forget that as far as a sorting algorithm knows, the array might already be sorted before it even starts. Just because the array you happen to be testing with IS sorted at that point, it doesn't mean your algorithm can assume that yet.

This is a better solution, in this way if there aren't swaps(so it means that is ordered) it stops.
Said formally, in the best case the time complexity of this implementation in 0(n) instead of O(n^2) of the classic one.

The easiest way to see if you have the boundary conditions correct is to see what happens if there are exactly two items. There should be only one compare, and if they were out of order then one swap.
If you include the change I just mentioned above, then the original code will do exactly that. However, your i>1 idea would instead mean that zero comparisons are made and it would fail to sort.
Therefore the outer loop is correct as-is; your friend was right, and you were wrong. But you both fail on missing the buffer overrun.

The easiest way to see if you have the boundary conditions correct is to see what happens if there are exactly two items. There should be only one compare, and if they were out of order then one swap.
If you include the change I just mentioned above, then the original code will do exactly that. However, your i>1 idea would instead mean that zero comparisons are made and it would fail to sort.
Therefore the outer loop is correct as-is; your friend was right, and you were wrong. But you both fail on missing the buffer overrun.

I face problems in understanding the algorithm. Not problems in C syntax or something about the language.

I want to discuss with someone about it... I don't want to take some algorithm from web in order to give it in some exercise. Thank you for your time....

On the topic now.... if we have 10 elements 0....9 for example... we really need 8 comparisons (inner loop) among the elements. (This technique is not the improvement of the algorithm of course).

Generally if we have n elements we really need n-1 comparisons among the elements...... so inner loop would be concerned about that.

Now... I can't understand how will be the outer loop . I think the number of outer passings is something different from comparisons into inner loop.

That's the problem with Bubble sort. It moves just one value, one index over, every time it iterates through the inner loop. It's fast on sorting small sets, but Insertion sort is faster. Once you start adding in the optimizations to Bubble sort to make it competitive with Insertion sort, you need more code - so you've gained nothing. It's still slower than Insertion sort, it's more code, and it's not as easy to remember.

If you miss out on understanding Bubble sort's "secrets", imo, you haven't missed much.

Thre are modifications to bubble sort such that subsequent passes can be significantly shorter in some cases, or the algorithm may even terminate after any given pass if the sorting is detected as complete. But in all cases, the first pass contains N-1 comparisons.

Think about how many comparisons are needed to determine if N items are in order. Because bubble sort can be made to exit after the first pass, no matter what, it still needs to do that much work in the first pass.Psst, check out my homepage, link below