数组的插入排序:
1 #include <stdio.h> 2 int main() 3 { 4 5 int a[11]={ 1,4,6,9,13,16,19,28,40,100};// 10 member 6 7 int temp1,temp2,number,end,i,j; 8 9 printf( " original array is:\n "); 10 11 for(i=0;i<11;i++) // output 11 member,if a[10]==0,it indicate it was init already 12 13 printf( " %5d ",a[i]); 14 15 printf( " \n "); 16 17 printf( " insert a new number: "); 18 19 scanf( " %d ",&number); 20 21 end=a[ 9]; 22 23 if(number>end) 24 25 a[ 10]=number; 26 27 else 28 29 { 30 for(i= 0;i< 10;i++) 31 32 { 33 if(a[i]>number) 34 35 { 36 temp1=a[i]; 37 38 a[i]=number; 39 40 for(j=i+ 1;j< 11;j++) 41 42 { 43 temp2=a[j]; 44 45 a[j]=temp1; 46 47 temp1=temp2; 48 49 } 50 51 break; 52 53 } 54 55 } 56 57 } 58 59 for(i= 0;i< 11;i++) 60 printf( " %6d ",a[i]); 61 62 printf( " \n "); 63 64 return 0; 65
66 }
mian@mian:~/Public/code$ ./input_into_array
original array is:
1 4 6 9 13 16 19 28 40 100 0
note that:if define like thiis
优化result: