運用冒泡法對一維數組、二維數組進行快速排序(c++中)

#include<stdio.h>
/*用冒泡法實現,對一維數組進行快速排序(升序)*/
int main(){
int a[5]={4,12,54,45,23};
int i,j,temp;
for(i=0;i<5;i++)
{
for(j=0;j<5-i;j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
for(i=0;i<5;i++)
{
printf("%3d",a[i]);
printf("\n");
}
}
數組



#include<stdio.h>
/*用冒泡法實現,二維數組可當成一維數組訪問*/
int main(){
int a[5][5]={ {4,12,54,45,23},{6,34,32,54,64},{67,34,54,32,43},{12,43,56,43,1},{5,56,78,98,15}};
int i,j,temp,*p;
p=*a;
for(i=0;i<25;i++)
{
for(j=0;j<25-i;j++)
{
if(*(p+j) > *(p+j+1))
{
temp = *(p+j);
*(p+j)=*(p+j+1);
*(p+j+1)=temp;
}
}
}
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
} spa

相關文章
相關標籤/搜索