高手请帮小弟我看看这个程序哪里错了

高手请帮我看看这个程序错哪了?
#include <stdio.h>
#define N 5
void bubblesort(int [],int);
void main()
{
int a[N]={12,22,10,3,9};
// printf("input Array a[]:\n");
// for(int i=0;i<N;i++)
// scanf("%d ",a[i]);
printf("\nAfter sort......\n");
bubblesort(a,N);
}

void bubblesort(int a[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
for(i=0;i<n;i++)
printf("%d ",a[i]);
printf("\n");
}
可以运行,可结构总是内存出错!我的目的是要达到排序的效果。

------解决方案--------------------
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
if(a[j]<a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}

当i=0;j<n-0=n j=n-1 ,j+1==n a[j+1]溢出
------解决方案--------------------
C/C++ code

for(i=0;i<n;i++)
for(j=0;j<n-i;j++)// j = i+1; j<n-i;j++
if(a[j]<a[j+1])// a[i]<a[j]
{
temp=a[j];//
a[j]=a[j+1];//
a[j+1]=temp;// 冒泡排序你写错了。。
}

------解决方案--------------------
让我看了半天,还调试了好长时间,终于发现,你的错误居然。。。。。。

C/C++ code

#include <stdio.h>

#define N 5

void bubblesort(int [],int);

void main()
{
    int a[N]={12,22,10,3,9};
    // printf("input Array a[]:\n");
    // for(int i=0;i<N;i++)
    // scanf("%d ",a[i]);
    printf("\nAfter sort......\n");
    bubblesort(a,N);
}

void bubblesort(int a[],int n)
{
    int i,j,temp;

    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1;j++)  //你原来的代码写成了for(j=0;j<n-i;j++)
        {
            if(a[j]<a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }

    for(i=0;i<n;i++)
    {
        printf("%d ",a[i]);
    }

    printf("\n");
}