鱼C论坛

 找回密码
 立即注册
查看: 1479|回复: 8

插入排序中,把数组元素赋值给整形

[复制链接]
发表于 2016-8-21 16:45:54 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1.   1 #include "iostream"
  2.   2 using namespace std;
  3.   3 int main()
  4.   4 {
  5.   5         int quote[20];
  6.   6         int a=0;
  7.   7         for(int i=0;i<20;i++)
  8.   8
  9.   9
  10. 10         {
  11. 11
  12. 12         if(cin>>quote[i])
  13. 13                 a++;
  14. 14                 else
  15. 15         {
  16. 16                         cin.clear();
  17. 17                         cin.sync();
  18. 18         break;
  19. 19         }}
  20. 20         for(int j=1;j<a;j++)
  21. 21         {
  22. 22                 int key;
  23. 23                    key=quote[j];
  24. 24                 int i=j-1;
  25. 25                 while(i>0&&quote[i]>key){
  26. 26                         quote[i+1]=quote[i];
  27. 27                         i-=1;
  28. 28                 }
  29. 29                 quote[i+1]=key;
  30. 30         }
  31. 31         int i=0;
  32. 32         while(i<=a){
  33. 33                 cout<<quote[i]<<endl;
  34. 34                 i++;
  35. 35         }
  36. 36
  37. 37 }
复制代码

求大神帮我看一下这段代码,是一段简单的插入排序问题。
问题出在23行给key赋值上,给key的第一次赋值总是出错,在调试中确认了quote数组元素正确,j值正确,但是int key=quote[j]却赋出来一个溢出值(大概是4196592
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2016-8-21 16:48:39 | 显示全部楼层
  1. #include "iostream"
  2. using namespace std;
  3. int main()
  4. {
  5.         int quote[20];
  6.         int a=0;
  7.         for(int i=0;i<20;i++)
  8.       

  9.         {   

  10.         if(cin>>quote[i])
  11.                 a++;
  12.                 else
  13.         {   
  14.                         cin.clear();
  15.                         cin.sync();
  16.         break;
  17.         }}  
  18.         for(int j=1;j<a;j++)
  19.         {   
  20.                 int key;
  21.                    key=quote[j];
  22.                 int i=j-1;
  23.                 while(i>0&&quote[i]>key){
  24.                         quote[i+1]=quote[i];
  25.                         i-=1;
  26.                 }
  27.                 quote[i+1]=key;
  28.         }   
  29.         int i=0;
  30.         while(i<=a){
  31.                 cout<<quote[i]<<endl;
  32.                 i++;
  33.         }   
  34.       
  35. }
复制代码

更正一下代码,这段没有序号
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-21 17:45:01 | 显示全部楼层
  1. #include <iostream>
  2. using namespace std;

  3. int main()
  4. {
  5.         int quote[20];
  6.         int a=0;
  7.         for(int i=0;i<4;i++)
  8.         {   

  9.                 if(cin>>quote[i])
  10.                         a++;
  11.                 else
  12.                 {   
  13.                         cin.clear();
  14.                         cin.sync();
  15.                         break;
  16.                 }
  17.         }

  18.         for(int j=1;j<a;j++)
  19.         {   
  20.                 int key;
  21.                 key=quote[j];
  22.                 int i=j-1;
  23.                 while(i>=0&&quote[i]>key){
  24.                         quote[i+1]=quote[i];
  25.                         i-=1;
  26.                 }
  27.                 quote[i+1]=key;
  28.         }   

  29.         printf("a = %d\n", a);
  30.         int i=0;
  31.         while(i < a){
  32.                 cout<<quote[i]<<" ";
  33.                 i++;
  34.         }   

  35.         getchar();getchar();
  36.         return 0;
  37. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-21 17:49:46 | 显示全部楼层
  1. //ai[10] = {1,2,3...,8,9,10}; iStart=0, iEnd=10;
  2. void vInsertionSort(int ai[], int iStart, int iEnd)
  3. {
  4.     int i = 0;
  5.     int j = 0;
  6.     int iTemp = 0;
  7.     for (i=iStart+1; i<iEnd; i++)
  8.     {
  9.         for (j=i; j>iStart; j--)
  10.         {
  11.             if(ai[j] < ai[j-1])
  12.             {
  13.                 iTemp = ai[j];
  14.                 ai[j] = ai[j-1];
  15.                 ai[j-1] = iTemp;
  16.             }
  17.         }
  18.     }
  19. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-21 18:05:12 | 显示全部楼层
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.         int quote[20];
  6.         int a=0;
  7.         for(int i=0;i<20;i++)
  8.                 {   

  9.                         if(cin>>quote[i])
  10.                 a++;
  11.             else
  12.                         {
  13.                                 cin.clear();
  14.                 cin.sync();
  15.                                 break;
  16.                         }
  17.                 }  
  18.         for(int j=0;j<a;j++)
  19.         {   
  20.                 int key;
  21.                 key=quote[j];
  22.                 int i=j-1;
  23.                 while(i>0 && quote[i]>key)
  24.                                 {
  25.                         quote[i+1]=quote[i];
  26.                         i-=1;
  27.                 }
  28.                 quote[i+1]=key;
  29.         }   
  30.         int i=0;
  31.         while(i<a)
  32.                 {
  33.                 cout<<quote[i]<<endl;
  34.                 i++;
  35.         }   
  36.    return 0;   
  37. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-21 18:28:58 | 显示全部楼层
上面发的有点问题,这下应该没问题了。可以复制运行一下。
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.         int quote[20];
  6.         int a=0;
  7.         for(int i=0;i<20;i++)
  8.                 {   

  9.                         if(cin>>quote[i])
  10.                 a++;
  11.             else
  12.                         {
  13.                                 cin.clear();
  14.                 cin.sync();
  15.                                 break;
  16.                         }
  17.                 }  
  18.         for(int j=0;j<a;j++)
  19.         {   
  20.                 int key;
  21.                 key=quote[j];
  22.                 int i=j-1;
  23.                 while(i>=0 && quote[i]>key)
  24.                                 {
  25.                         quote[i+1]=quote[i];
  26.                         i-=1;
  27.                 }
  28.                 quote[i+1]=key;
  29.         }   
  30.         int i=0;
  31.         while(i<a)
  32.                 {
  33.                 cout<<quote[i]<<endl;
  34.                 i++;
  35.         }   
  36.    return 0;   
  37. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-21 18:30:10 | 显示全部楼层

我想知道为什么第一步时把quote[j]赋给key时会出错?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2016-8-21 18:37:38 | 显示全部楼层
已解决,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2016-8-21 18:53:11 | 显示全部楼层
学习一下
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-13 21:30

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表