使用指针实现冒泡排序的数组元素升序排列。
#include<stdio.h>
void bubarray(int a[], int n);
int main(void)
{
int a[] = { 0,-1,-2,4,3,1,20,15 };
int n = (int)sizeof(a) / sizeof(a[0]);
printf("Initial results:\n");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
bubarray(a, n);
return 0;
}
void bubarray(int a[], int n)
{
int i, j, Temp;
for(i=0;i<n;i++)
for (j = n - 1; j >= i; j--)
{
if (a[i] > a[j])
{
Temp = a[j];
a[j] = a[i];
a[i] = Temp;
}
}
printf("The endP:\n");
for (int i = 0; i < n; i++)
printf("%d ", a[i]);
printf("\n");
}