您的位置:首页 > 编程语言

我写的排序,中间一小段代码曾出现过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);
}
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐