天天看點

冒泡排序c語言代碼測試,冒泡排序C語言代碼

冒泡排序c語言代碼測試,冒泡排序C語言代碼

終究會走-

回答時間:2019-12-05

向TA提問

#include

#define max 100

void sort(int a[],int n);

int insert(int a[],int n,int x);

void show(int a[],int n);

int main()

{

int a[max],i,n,x;

scanf("%d",&n); //input the number of data;

for(i=0;i

scanf("%d",a+i);

sort(a,n); //sort the data

printf("after the sort:\n");

show(a,n);

putchar('\n');

scanf("%d",&x); //input the data that you want to insert the array;

n=insert(a,n,x);

show(a,n);

return 0;

}

void sort(int a[],int n)

{

int temp,i,j;

for(i=0;i

{

for(j=n-1;j>i;j--)

{

if(a[j-1]>a[j])

{

temp=a[j-1];

a[j-1]=a[j];

a[j]=temp;

}

}

}

}

int insert(int a[],int n,int x)

{

int i,j;

if(a[0]>x) i=0;

else if(a[n-1]

{

a[n]=x;

return ++n;

}

else

{

for(i=1;i

{

if(a[i-1]x)

break;

}

}

for(j=n-1;j>=i;j--)

{

a[j+1]=a[j];

}

a[i]=x;

n++;

return n;

}

void show(int a[],int n)

{

int i;

for(i=0;i

printf("%d\t",a[i]);

}

收起

冒泡排序c語言代碼測試,冒泡排序C語言代碼

閱讀全文

冒泡排序c語言代碼測試,冒泡排序C語言代碼