猿问

求大神解答,C新手一枚,程序有注释,有关插入排序的,麻烦帮忙看看哪里出错了,谢啦

#include<stdio.h>
int main (){
   printf("how many number do you want to          sort?\n ");
     int i,j,k,s;
   scanf("%d",&k);//输入排序数组的数组大小       int sort[k];//定义排序数组  
    printf("please input number\n");
   for(i=0;i<k;i++){       
scanf("%d",sort[i]); }//依次输入排序数组 for(i=0;i<k;i++){
          for(j=i+1;j<=k;j++){           
                    if(sort[i]>sort[j]){                 
                                        s=sort[i];                 
                                      sort[i]=sort[j];                  
                                      sort[j]=s;          
                   }//比较大小并交换数值
         }   
}//从开始遍历整个数组并排序 
  while(i<k){       
            printf("insert_sort over,result is%d:",sort[i]); }//依次输出排序后数组
return 0;
}

孽3
浏览 1074回答 1
1回答

___C___

谢谢采纳 int sort[k];这一句代码有误,需要动态分配内存空间,c++写法为int sort[] = new int[k];scanf("%d",sort[i]);这里有误,改为scanf("%d",&sort[i]);
随时随地看视频慕课网APP
我要回答