# pset3 implementing bubblesort, how should i test what is most likely garbage?

i am a total beginner and i tried to implement bubble sort as follows:

``````void swap(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}

void sort(int values[], int n)
{
do
int counter=0;
for (i=0;i<n; i++)
{
if (values[i]>values[i+1])
{
swap(values[i], values[i+1]);
counter=1;
}

}
while (counter==1)
}
``````

since there are no instructions to see if what i did was correct, i have no idea what to do now with it. I know it is most likely false and the do/while construction seems stupid, but i had no other idea how to implement that i sort and switch severeal "rounds"

Any help or suggestion appreciated

## 1 Answer

You've written an implementation. Now you need to test it. How you should test it is basically the same as check50 would but you do it manually.

1. create array of unsorted ints (ex. [3,4,1,5,2])
2. pass the array into your sort function.
3. print out the array.
4. If your function works you'll print 1,2,3,4,5 if not fiddle and rerun until it works
5. Vary the input to make sure the array works for different edge cases (only one value in the array, or multiple values that are equivalent.

This is how I do it. You have access to all the functions in helpers.c, so it's pretty easy to create a separate program to test a function you create in it. This is my preferred method because it let's me test the function in isolation before adding more complexity to it.

Quick and dirty example:

// ~/Dropbox/pset3/find/testing.c

``````
#include "helpers.h"
#include <stdio.h>
int main(void)
{
//set values
int n = 5;
int values[] = {3,4,1,5,2};
//test function
sort(values,n);
//print out result
for (int i = 0; i < n; i++)
{
printf("%d, ", values[i]);
}
printf("\n");
}
``````

terminal: cd ~/Dropbox/pset3/find terminal: clang testing.c helpers.c -o testing terminal: /.testing

Should print 1, 2, 3, 4, 5, - so if it doesn't then it's time for some troubleshooting.

• Sorry i dont understand how i do it. I know i can use generate to generate random numbers, but my function is stored in helper.c which as far as i can tell i use with find ? so how can i test it ? Feb 1 '15 at 19:27
• I tried this and it said "undefined reference to 'sort'" Aug 22 '15 at 19:19
• @user7489 have you written a sort function? Aug 22 '15 at 19:22
• @AntoineWood yes it is written in helpers.c Aug 22 '15 at 19:37
• Which is defined in helpers.h with the same function name and perimeters as the test function? Aug 22 '15 at 19:44