# Thread: Help understanding bubblesort hw?

1. Originally Posted by beretta
didn't work for me
That post??? I tested it. What seems wrong?

2. set n = sizeof(a) / sizeof(a[0] if you want to use sizeof().

It stinks though, because you may (and frequently will), make your array larger than you need, and just use part of it for your data.

Then sizeof() just sucks eggs, and you'll need n (some variable), to tell your program how many numbers, or strings or whatever, you need to have sorted, in your array. Sizeof() can never do that.

OK, off my soapbox, down to business:

1) If you're going to have the user input the values into a[], then remove the numbers now being put into a, by the program, at initialization.

2) set n to *something* that you want, that tells bubblesort what size the array is (or what number of elements it needs to sort).

3)Put the user input inside a while loop.

3. I've got to hit the sack for some zzzz's.

Good luck, Baretta.

I've got to hit the sack for some zzzz's.
Good idea Adak, you usually seem more lucid than this

5. =( )

6. Code:
```#include <stdio.h>
#include <stdlib.h>

/* The actual method of sorting */

void bubblesort(int a[],int n);

int main()
{
int n;
int list = 0;
int a[10] = { 4, 8, 2, 1, 6, 5, 3, 9, 8, 1};
scanf("%d", &n);
printf("Before: ");
while(list < sizeof(a) / sizeof(a[0]))
printf("%d", a[list++]);
printf("\n");
bubblesort(a, sizeof(a) / sizeof(int));
list = 0;
printf("After: ");
while(list < sizeof(a) / sizeof(a[0]))
printf("%d", a[list++]);
printf("\n");
system("pause");
return 0;
}

/*Defining the bubblesort*/
void bubblesort(int a[],int n)
{
int cmp, pass, tmp, swch;
pass = 0;
cmp = 0;
swch = 1;
while(swch)
{
swch = 0;
cmp = 1;
while(cmp < n - pass)
{
if(a[cmp-1] > a[cmp])
{
swch = 1;
tmp = a[cmp-1];
a[cmp-1] = a[cmp];
a[cmp] = tmp;
}
cmp++;
}
pass++;
}
printf("Our Sort had %d comparisons, and %d passes\n", cmp, pass);
}```
ok apparently i am suppose to make the n a variable that hte user types in:
-then you type in the numbers you would like to sort

so if n = 6 there will be six numbers to sort.

any ideas, ill reply when i get back from work

7. Originally Posted by beretta
ok apparently i am suppose to make the n a variable that hte user types in:
-then you type in the numbers you would like to sort
That is exactly what I was showing you in post #58! You do not have to do it that way, of course.

8. Yea i understand what you are saying, i'm workin on it right now

9. ## I see it a different way, tell me if i'm too far off

Code:
```#include <stdio.h>
#include <stdlib.h>

int BubbleSort(int info, int next);//returns the lesser of the 2.

void main(void)
{

int *ptr = malloc(10 * sizeof (int));// will hold entire input array
int next=0;int info;

int i;

for (i=0;i<<*ptr;i++)
{

scanf("%i",&info);
if (info<=next)
{
*ptr=info;

}
else *ptr=BubbleSort(info,next);
printf("%i, ",*ptr);
ptr++;
}
}
int BubbleSort(int info, int next)
{
int tempInfo;
tempInfo=info;
info=next;
next=tempInfo;
return info;
}```
only I haven't figured out why nothing is being output by the printf("%i, ", *ptr);

Am I close?

- Edward