# Thread: Help understanding bubblesort hw?

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

/* The actual method of sorting */

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

int main()
{
int a[10];
bubblesort(a, sizeof(a) / sizeof(int));
system("pause");
return 0;
}

/*Defining the bubblesort*/
void bubblesort(int a[],int n)
{
scanf("%d", &n);
int cmp, pass, tmp;
pass = 1;
cmp = 1;
while(cmp <= n)
{
printf("Inside inner loop.  cmp=%d, n-pass=%d\n",cmp,n-pass);
if(a[cmp-1] > a[cmp])
{
tmp = a[cmp-1];
a[cmp-1] = a[cmp];
a[cmp] = tmp;
}
cmp = cmp + 1;
pass = pass + 1;
}
}```
that seems to work better

2. bithub, we do not use the for function in our class

3. That code is just to help you see what is going on with your program. Anyways, the same thing can be implemented with a while loop.
Code:
```int main(void)
{
int i = 0;
int a[4] = {4, 8, 2, 1};
printf("Before calling bubblesort()...\n");
while(i < sizeof(a) / sizeof(a[0])
printf("%d ", a[i++]);
printf("\n");
bubblesort(a, sizeof(a) / sizeof(int));
...
}```

4. just mentioning it to you sir, I understand what you are saying though. thank you.

5. hmm im not understanding how to view after.

6. Only while loops, eh?

Code:
```while( i < n)
print a[i]```
This is NOT code, just pseudo-code.

7. i got that, now i need to run a loop to where it finishes the sort

8. Yeah, I had to make some changes to get this version to sort. I'm not sure it's everything your teacher wants, however.

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

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

int main()
{
int a[10] = { 3, 1, 7, 6, 2, 8, 9, 5, 4, 0 };
int n = 10;

bubblesort(a, n);
showIt(a, n);
n = getchar();
return 0;
}

/*Defining the bubblesort*/
void bubblesort(int a[],int n)
{
int cmp, pass, tmp, swapped;

pass = cmp = 0;
swapped = 1;
while(swapped)
{
swapped = 0;
cmp = 1;
while(cmp < n - pass)
{
if(a[cmp-1] > a[cmp])
{
swapped = 1;
tmp = a[cmp-1];
a[cmp-1] = a[cmp];
a[cmp] = tmp;
}
cmp++;
}
pass++;
}
printf("\n Our Sort had %d comparisons, and %d passes", cmp, pass);
}
void showIt(int a[], int n)  {
int i;
printf("\n\n\n");

for(i = 0; i < n; i++)
printf("%d ", a[i]);
}```
I don't like the cmp being reset to 1, but it appears part and parcel of this version of the bubblesort.

9. hmmm

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

/* The actual method of sorting */

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

int main()
{
int list = 0;
int a[4] = { 4, 8, 2, 1};
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)
{
scanf("%d", &n);
int cmp, pass, tmp;
pass = 1;
cmp = 1;
while(cmp <= n)
{
printf("Inside inner loop.  cmp=%d, n-pass=%d\n",cmp,n-pass);
if(a[cmp-1] > a[cmp])
{
tmp = a[cmp-1];
a[cmp-1] = a[cmp];
a[cmp] = tmp;
}
cmp++;
pass++;
}
}```
this is what i have so far.

10. The output is in sorted order, and all array values are present.

I am receiving no warning or errors, of any kind, and have run this numerous times.

What's not working?

11. As bithub noted, you must have two nested loops to run a bubblesort. (actually, any sort I ever heard of).

You only have 1.

You can either keep the one loop, and have it not sort, or add another loop to it, and have it sort.

13. No, showIt just prints out the array, it sorts nothing.

Look in bubblesort(), and see the two while loops, nested inside each other?

That's what's doing the sorting.

14. how would i loop with

Code:
``` printf("Please enter an integer: ");
scanf("%d", &n);
int cmp, pass, tmp, swch;
pass = 1;
cmp = 1;
swch = 0;
while(swch)
{
swch = 1;
cmp = 0;
while(cmp < n - pass)
{
if(a[cmp-1] > a[cmp])
{
swch = 0;
tmp = a[cmp-1];
a[cmp-1] = a[cmp];
a[cmp] = tmp;
}
cmp++;
}
pass++;
}```
cause that does nothing.

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

/* The actual method of sorting */

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

int main()
{
int list = 0;
int a[4] = { 4, 8, 2, 1};
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)
{
scanf("%d", &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);
}```
this works. now i need it to stop when it is finished sorting.