Quantcast
Channel: C - Codecademy Forums
Viewing all articles
Browse latest Browse all 25

So my bubblesort doesn't like me

$
0
0

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;

}

7 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 25

Trending Articles