我写的排序,中间一小段代码曾出现过bug,改成功了,不过我还在琢磨为什么会有这bug(第55-63行)
2010-12-09 18:05
309 查看
using System; using System.Collections.Generic; using System.Text; namespace QuickSort { class Program { public static float[] src; public static float[] dest; static void Main(string[] args) { string[] sa; string ss; int i,first,left,right; Console.WriteLine("请输入元素,用逗号分割开"); ss = Console.ReadLine(); sa = ss.Split(','); src = new float[sa.Length]; //待排序的数组 dest = new float[sa.Length]; //用于存放排序之后的数组 for (i = 0; i < sa.Length; i++) { src[i] = Convert.ToSingle(sa[i]); } Console.WriteLine("输入完毕,输出"); while (i>0) { Console.WriteLine(src[i-1].ToString()); i--; } first = 1; //参考点为第一个元素 left = right = 0; dest[0] = src[first]; for (i = 0; i < src.Length; i++) { if (i != first) { sort(left, right, i); right++; } } Console.WriteLine("排序之后:"); while (i > 0) { Console.WriteLine(dest[i - 1].ToString()); i--; } } static void sort(int left,int right,int pos) { int ip,i; if ((left == right)||(right==left+1)) { if (left == right) i = 0; //目的数组dest只有一个元素,i=0 else { if(pos<dest.Length-1) i = pos; else //源数组扫描到了最后一个元素 i=pos-1; } if (src[pos] >= dest[right]) //向末尾添加 { dest[right+1]=src[pos]; } else { if (src[pos] <= dest[left]) //向开头添加 { ip = left-1; } else ip =left; while (i > ip) { dest[i + 1] = dest[i]; i--; } dest[i + 1] = src[pos]; } } else { if (src[pos] >= dest[(left + right) / 2]) { sort((left + right) / 2, right, pos); } else { sort(left, (left + right) / 2, pos); } } } } }
相关文章推荐
- Xcode代码提示出现bug的时候
- 一个正确的c语言链表代码(中间也有些bug)
- 遇到个BUG ,eclipse中的注释了的代码还在执行
- 烧饼排序问题(有待完善的地方,不过一个影响结果的bug被我找出来了,原来是第61行的比较运算符应该从<改成<=)
- JSP代码格式化后在IE6中出现的小BUG
- win7 安装.Net framework 4.0出现 安装不成功,错误代码0x80240037 的解决方法
- 【Bug】VS代码运行出现Stack overflow
- 使用IdHttp获取网页代码出现的问题,只能得到部分代码,为什么,请大家帮忙
- c++为什么代码执行性后出现一个黑框(命令窗口)一闪而过解决办法
- 代码没有任何改动,为什么程序执行会有Bug?
- C#为什么要形成IL中间代码,而不是直接生成机器代码?
- 冲顶大会Python代码执行出现bug
- 代码make后出现的错误----为什么会出现这个错误,怎么改?
- win7 安装.Net framework 4.0出现 安装不成功,错误代码0x80240037 的解决方法
- 为什么国内并未出现类似instagram一样成功的应用?
- 双击文字出现编辑文本框的JS代码,并可编辑成功
- 为什么机器会在少数电脑上出现“ samsung syslsi usb sync 该设备无法启动 (代码 10)”的提示
- 昨天的邮件发送bug修改成功之后,又出现新的大问题
- 指给新手的项目中写代码可能会出现bug的地方,慢慢积累,慢慢避免
- 【代码】【有统计错误,待修改】统计文件中,不同字符出现的次数(未排序)