# So my bubblesort doesn't like me

Hello,
I tried writing a bubble sort algorithm but there is always a runtime error. I can’t find the logical error I would be happy if someone found it. everything except 20 is printed instead of 20 a what I persume adress is printed.

Here is the code:
#include <stdio.h>
#include <stdlib.h>

void bubblesort(int* funcPtr, int sizeOfArray)
{
int changer;
int counter = 1;
for(int i = 0; i < sizeOfArray; i++)
{
if (*funcPtr > *(funcPtr + 1))
{
changer = *(funcPtr + 1);
*(funcPtr + 1) = *funcPtr;
*funcPtr = changer;
funcPtr++;
}
else
{
funcPtr++;
counter++;
}
if (counter == (sizeOfArray))
{}
else if (i == sizeOfArray)
{
counter = 1;
i = 0;
}
}
}

void printer(int* iArray, int length)
{
for(int i = 0; i < length; i++)
{
printf(“%i\n”, iArray[i]);
}
}

int main(void)
{
int iArray[10] = {10, 1, 2 ,3 ,5 , 6, 4, 11, 13, 20};
bubblesort(iArray, 10);
printer(iArray, 10);
return 0;

}

Try a the gdb, it’s useful for stuff like this

https://u.osu.edu/cstutorials/2018/09/28/how-to-debug-c-program-using-gdb-in-6-simple-steps/

3 Likes

Thank you will do(need to fill 20 characters)

Your printer function uses the subscript syntax iArray[i]. Why torture yourself with all the pointer plusses in the sort?

I tried simply writing it as an array, but it doesn’t fully capture your first bug:

void bubblesort(int *a, int sizeOfArray) {
int counter = 1;
for (int i = 0; i < sizeOfArray; i++) {
// as a side note, this has overflowed when i == sizeOfArray - 1
if (a[i] > a[i + 1]) {
int changer = a[i + 1];
a[i + 1] = a[i];
a[i] = changer;
} else {
counter++;
}
if (counter == sizeOfArray) {
// perhaps something should be here
// wait, counter is your swap flag?!?  neat
} else if (i == sizeOfArray) {
counter = 1;
i = 0; // time to consider if you really, really want a for loop
}
}
}

To try to highlight the error:

void bubblesort(int *funcPtr, int sizeOfArray) {
// ...
for (int i = 0; i < sizeOfArray; i++) {
// ...
funcPtr++;
if (i == sizeOfArray) {
i = 0;
// funcPtr has left the building
// you're passed the end and toast
}
}
}

If you’re keen to go all pointery, don’t mess with the passed pointer, use a local one:

void bubblesort(int *a, int sizeOfArray) {
int swapped = 1;
while (swapped) {
swapped = 0;
for(int i=0, *p = a; i<sizeOfArray - 1; i++, p++) {
if (*p > *(p + 1)) {
// do your swap
// trip your swapped flag
swapped = 1;
}
}
}
}
1 Like

Thank you,
Your comments just killed me
I did find a short fix by just changing i = 0 to i = 1

for(int i = 1; i < sizeOfArray; i++) // Here (Or just sizeOfArray - 1) { if (*funcPtr > *(funcPtr + 1)) { changer = *(funcPtr + 1); *(funcPtr + 1) = *funcPtr; *funcPtr = changer; funcPtr++; } else { funcPtr++; counter++; } if (counter == (sizeOfArray)) {} else if (i == sizeOfArray) { counter = 1; i = 1; // And here }

As my logic was built upon 1 Index though I was using 0 index.

Ok don’t know how you made your code snippets like that.

Ok don’t know how you made your code snippets like that.

To be fair, I didn’t know how you made yours like that. (I’m new here, Reddit is fubar.) I just type in markdown…

I peeked behind yours to see how you did it. Here. Conversely, you can peek behind mine.

So, your code example is still falling off the ledge, starting at one fixes the problem of going past the tail; on the first pass. If you’re sticking with this, you still need to fix that other bit. You do not want to be doing funcPtr++ unless you’ve saved it first.

As an aside, *funcPtr > *(funcPtr + 1) is the same as *funcPtr > funcPtr[1]… let that sink in.

1 Like