#include "stdio.h"
void bubble(int *,int,int(*)(int,int));
int ascending(int,int);
void swap(int *,int *);
int main()
{
int size = 9;
int a[9]={112,39,51,123,82,41,38,71,1};
int i;
for(i=0;i<=size-1;i++)
{
printf("%d ",a[i]);
}
printf("\n");
bubble(a,size,ascending);
for(i=0;i<=size-1;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
void bubble(int *result , int size , int (*func)(int a,int b))
{
int i=0,j=0;
for(i=0; i
for(j=0;j
if((*func)(result[j],result[j+1]))
{
swap(&result[j],&result[j+1]);
}
}
}
}
void swap(int *a,int *b)
{
int temp=0;
temp=*a;
*a=*b;
*b=temp;
}
int ascending(int x,int y)
{
return y < x;
}
其它注意:
112 address=0xbf9e2468
39 address=0xbf9e246c
51 address=0xbf9e2470
123 address=0xbf9e2474
82 address=0xbf9e2478
41 address=0xbf9e247c
38 address=0xbf9e2480
71 address=0xbf9e2484
1 address=0xbf9e2488
size address=0xbf9e248c
i address=0xbf9e2464
在寫這個範例的時候,size填太大了,因此讓swap存取到size的位址,
因此造成size的位址也被抓來bubble sort = =" 所以用指標時一定要小心...
compiler看來把區域變數放在記憶體相鄰的位址 = ="
沒有留言:
張貼留言