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);
printf("\n\n\t\t\t press enter when ready");
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.