0

I am trying to implement a bubble sort algorithm, but with 3 functions, a gen_array to generate the array given a size and a maximum for the values and a sort to sort the values in a bubble sort way. I can't get the sort part to work. However, the gen_array funcion is doing what it is supposed to and working when I call it and print its values on main().

Here is the code.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int gen_array(int ** list, int size, int max) {

    *list = (int *)malloc(size * sizeof(int));

        if (*list == NULL){
        printf("ERROR: out of memory\n");
        return 1;
    }

    srand( (unsigned)time( NULL ) );
    for (int i = 0; i < size; ++i) {
        (*list)[i] = rand() % max;
    }
    return 0;
}

int sort(int * a, int s) {
    int *temp;

    for (int i = 1; i < s; ++i) {
        for (int j = 0; j < i - 1; ++j) {
            if (*(a + i) > *(a + i + 1)) {
                *(a + i) = temp;
                *(a + i) = *(a + i + 1);
                *(a + i + 1) = temp;
            }
        }

    }
    return 0;
}

int main() {

    int size;
    printf("array Size --> ");
    scanf("%d", &size);
    int * a;

    gen_array(&a ,size, 100);
    for (int i = 0; i < size; ++i) {
        printf("%d ", a[i]);
    }

    printf("\n");
    sort(a, size);
    for (int j = 0; j < size; ++j) {
        printf("%d", a[j]);
    }

    free(a);
    getchar();
    return 0;
}

After following @mch's comment I got this output,

Array Size --> 5

1 4 46 4 51

1 4 4 0 0

Process finished with exit code 0

So, the list is generating with 5 elements, but the sort part isn't working, I believe the sort code is right (the bubble algorithm), but the pointers aren't being used correctly? Perhaps I should copy the contents of the array with realloc() ?

THingamagick
  • 95
  • 1
  • 2
  • 13
  • 2
    "Cannot get to work" is not an explanation. – OldProgrammer Apr 02 '17 at 20:37
  • 3
    `sort(&a, size);` ==> `sort(a, size);`, here are some compiler warnings and errors: http://ideone.com/bP9uLs – mch Apr 02 '17 at 20:38
  • follow @mch's comment. This is the right answer. – alDiablo Apr 02 '17 at 20:40
  • 1
    Please don't cast `malloc()`. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – dtell Apr 02 '17 at 20:50
  • 2
    In function `sort()` the `*(a + i + 1)` will break the memory bounds. The outer loop should be `i < s-1`, but that pair of nested loops is wrong anyway. Compounded by using the uninitialised `temp` which swaps in the wrong sequence. That whole double loop is cack-handed. – Weather Vane Apr 02 '17 at 20:59
  • Your swap code is broken. You assign to the temporary first, then fix the other two values. And for pity's sake, use `a[i+1]` notation; the `*(a + i + 1)` looks … like non-idiomatic C. And you should probably be use `a[i]` and `a[j]`, too. – Jonathan Leffler Apr 02 '17 at 21:56

1 Answers1

1

There are several problematic errors in your code.

First, temp should be int, not int*. Remember, a is a pointer to the start of an array in the heap. *(a + i) is the value of the ith element of that array.

Second, temp should be set to *(a + i), not the other way around.

Also, as Weather Vane pointed out in the comments, the loops are wrong. You need to check array elements j and j + 1, not i. There are some other changes needed as part of that, but you can see those below.

Here's the sort method with these changes (I think the rest is all right except that you need to add spaces the second time you list the items):

int sort(int *a, int s) {
    int temp;

    for (int i = 0; i < s; ++i) {
        for (int j = 0; j < s - i - 1; ++j) {
            if (*(a + j) > *(a + j + 1)) {
                temp = *(a + j);
                *(a + j) = *(a + j + 1);
                *(a + j + 1) = temp;
            }
        }

    }
    return 0;
}
anonymoose
  • 819
  • 2
  • 11
  • 25