views:

220

answers:

3

It does not work.

#include<stdio.h>

void bubble_sort(int m, int a[100000]);

void main()
{
int a[100000], i, m;
FILE * be;

be=fopen("be.txt","r");
    // IMPORTANT! i pressed enter after last number in be.txt!!!
for (i=0; !(feof(be)); ++i)
    fscanf(be,"%i", a+i);
m=i-1;
bubble_sort(m ,a);
fclose(be);

}



void bubble_sort(int m, int a[100000])
{
int i, ok, v, n=m;;
for (;!ok;ok=1)
{
    ok=0;
    for (i=0;i<=m-1;++i)
    {
        if (*(a+i)>*(a+i+1)) { v=*(a+i); *(a+i)=*(a+i+1); *(a+i+1)=v; ok=0;}
    }
    m=m-1;
}

for (i=0; i<n; ++i)
    printf("%i ", a[i]);
}

The pseudo code: (I think it`s right)

Bubblesort2( A )
m:=length( A ) - 1
repeat
    OK := true
    for i := 0 to m-1 do
        if Ai > Ai+1 then
            Ai <=>Ai+1
            OK := false
    m := m - 1
until OK
+1  A: 

You aren't initialize the value of ok, so the behaviour is undefined.

You seem to be setting the value of ok to zero no matter what.

Also, there's no point optimizing bubble sort. Just get it simple and working. If you want good performance then you shouldn't be using this algorithm at all.

I'd suggest that you read the algorithm description on Wikipedia, try to write it in pseudo-code in your own words, make sure you understand it, and then implement it.

Mark Byers
Decrementing m is fine: at each step, the largest element will reach its final position, so there's no point checking it at the next step.
IVlad
@IVlad: Yeah, just worked that out. Unfortunately it's still O(n*n) even with this optimisation.
Mark Byers
+1 for the wiki suggestion
Shaihi
I don`t need other algorithm, i just want to programming the pseudo code.
Kalozka1
Bubblesort is always quadratic, however for random data that optimization helps a bit.
IVlad
Yes, I need 100K arrays. The bubble sort works with 100k.AndiDog cause it`s a pointer and i need it in other exercises.
Kalozka1
@Kalozka: I mean you should either implement bubble sort in the simplest way as it is described in text books, or else implement a different, faster algorithm. Trying to optimize bubble sort to be faster is a bit pointless, especially when you don't even have it working yet.
Mark Byers
Yes it`s an optimisation. but i need to programming this anlgorithm not other.
Kalozka1
Bubblesort works with 100k numbers if they are given in random order. Try the input 100000 99999 99998 ... 1 and see if it still finishes this year :)
IVlad
Mark Byers.I know it. But it`s a homework to programming that algorithm not else.
Kalozka1
+2  A: 

Try this:

void bubble_sort(int m, int a[100000])
{
    int i, ok = 0, v, n = m;
    while ( !ok )
    {
        ok = 1;
        for ( i=0; i < m-1; ++i )
        {
            if ( *(a+i) > *(a+i+1) ) 
            { 
                v = *(a+i); 
                *(a+i) = *(a+i+1); 
                *(a+i+1) = v; 
                ok = 0;
            }
        }

        m--;
    }

    for ( i = 0; i < n; ++i )
        printf("%i ", a[i]);
}

Basically, initialize ok (local variables are initialized with garbage values). You also must set ok = 1 when entering the loop and ok = 0 if a swap took place. There's no point in using a for loop, a while loop is more readable. m = m - 1 can be replaced with m--, it's the same thing and you write less code.

IVlad
Providing complete code for homework questions is generally frowned upon here. It encourages people to copy and paste without understanding the issues.
Mark Byers
for ( i = 1; i < n+1; ++i ) printf("%i ", a[i]);
Kalozka1
@Mark Byers: I explained the issues and the OP showed an attempt at solving the problem himself. I'm sorry if I shouldn't have posted complete code, I thought it would be ok in this case as an (almost working) attempt was made.
IVlad
@Kalozka1: what do you mean? if your array is indexed starting from 1, you should modify your first for loop to `for ( i=1; i <= m-1; ++i )`as well. Otherwise it should actually be `for ( i=0; i < m-1; ++i )`and the printing loop should be like in my posted code if indexing starts from 0. If it starts from 1, you are correct about the printing, but you need to modify the first for loop.
IVlad
@IVlad: The printf is right. I got the problem for ( i=0; i <= m-2; ++i ) <<<< m-2 cause i=0; m=nr. of numbers.
Kalozka1
@Ivlad: The correct way is to give pseudo-code or working code in a different language, and let the poster work out the details themselves, helping with hints if necessary. It's allowed to come back later and edit the post with full code. Don't worry, I made *exactly* the same mistake once: http://stackoverflow.com/questions/2210928/how-i-do-fibonaci-sequence-under-1000/2210949#2210949 - the best answer was this http://stackoverflow.com/questions/2210928/how-i-do-fibonaci-sequence-under-1000/2210954#2210954. Yes, the rules and traditions of the SO community are cryptic and confusing!
Mark Byers
@IVlad: I completed the code. i think...
Kalozka1
Okay, I will only post pseudocode and instructions from now on.
IVlad
+2  A: 
   void bubble_sort(int m, int a[])
    {
    int i, ok, v, n=m;
    for (ok = 0;!ok;)
    {
        ok=1;
        for (i=0;i<=m-1;++i)
        {
            if (*(a+i)>*(a+i+1)) { v=*(a+i); *(a+i)=*(a+i+1); *(a+i+1)=v; ok=0; }
        }
        m=m-1;
    }
    }

Two problems with your code:

  1. ok was undefined at start so its not clear whether the condition on the for loop will be satisfied or not.
  2. the 3rd clause (update clause) in the for statement is executed after the the code in the brackets is iterated, but before checking the condition again. So you were setting ok to 1 then checking that ok = 0. Thus it was only doing 1 iteration.

Still this program is a mess. The outer for loop should be a while loop. You should use proper array accessors a[i] rather than a+i. Also spacing and linebreaks in your source code cost nothing.

Il-Bhima