Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 快速搜索数组的边界和混乱的数组行为_C#_Quicksort_Parallel Arrays - Fatal编程技术网

C# 快速搜索数组的边界和混乱的数组行为

C# 快速搜索数组的边界和混乱的数组行为,c#,quicksort,parallel-arrays,C#,Quicksort,Parallel Arrays,我的程序(C#.NET)有多个问题,不知道是什么导致了这些问题 该程序旨在按姓名、姓氏和生日按升序和降序对姓名和生日列表(格式为名、姓、DD/MM/YYYY)进行排序。它还将具有尚未实现的其他功能 第一个问题是quikSortStr方法。程序在第一个if块中崩溃,说明j超出数组的界限。无论模式==“asc”与否,都会发生这种情况 第二个也是更容易混淆的问题是,当从文本文件加载值时,first和last的每个奇数索引值都将为null,而bDay的每个奇数索引值都将为1/1/0001 我在下面提供了

我的程序(C#.NET)有多个问题,不知道是什么导致了这些问题

该程序旨在按姓名、姓氏和生日按升序和降序对姓名和生日列表(格式为
名、姓、DD/MM/YYYY
)进行排序。它还将具有尚未实现的其他功能

第一个问题是
quikSortStr
方法。程序在第一个
if
块中崩溃,说明
j
超出数组的界限。无论
模式==“asc”
与否,都会发生这种情况

第二个也是更容易混淆的问题是,当从文本文件加载值时,
first
last
的每个奇数索引值都将为null,而
bDay
的每个奇数索引值都将为
1/1/0001

我在下面提供了完整的程序供参考,需要使用快速排序方法和并行数组。我为没有评论而道歉

提前感谢您的帮助。我完全被难住了

namespace Names_Arrays
{
public partial class frmNamArrays : Form
{
    System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-CA");
    string[] first;
    string[] last;
    DateTime[] bDay;
    string order = "asc";
    string format = "d/M/yyyy";

    public frmNamArrays()
    {
        InitializeComponent();
    }
    private void write()
    {
        string[] lines = new string[first.Length];

        for (int i = 0; i < lines.Length; i++)
            lines[i] = first[i] + ',' + last[i] + ',' + bDay[i].ToString(format);

        txtbxNames.Clear();
        txtbxNames.Lines = lines;
    }

    private void load()
    {
        string[] lines = txtbxNames.Lines;

        first = new string[lines.Length];
        last = new string[lines.Length];
        bDay = new DateTime[lines.Length];

        int i = 0;
        foreach (string line in lines)
        {
            string[] data = line.Split(',');

            //There aren't any lines that split to a string[] of length less than three,
            //but for some reason the program kept believing there are.
            //patched that leak.

            if (data.Length == 3)
            {
                first[i] = data[0];
                last[i] = data[1];
                bDay[i] = Convert.ToDateTime(data[2], culture);
            }
            i++;
        }
    }
    public DateTime[] quikSortTim(DateTime[] primary, string mode, int left, int right)
    {
        if (primary.Length > 1)
        {
            int i = left, j = right;
            DateTime pivot = primary[left + (right - left) / 2];

            while (i <= j)
            {
                if (mode == "asc")
                {
                    while (DateTime.Compare(primary[i], pivot) < 0)
                        i++;
                    while (DateTime.Compare(primary[j], pivot) > 0)
                        j--;
                }
                else
                {
                    while (DateTime.Compare(primary[i], pivot) > 0)
                        i++;
                    while (DateTime.Compare(primary[j], pivot) < 0)
                        j--;
                }
                if (i <= j)
                {
                    DateTime holdoverB = primary[i];
                    primary[i++] = primary[j];
                    primary[j--] = holdoverB;

                    string holdover = last[i - 1];
                    last[i] = last[j + 1];
                    last[j] = holdover;

                    holdover = first[i - 1];
                    first[i] = first[j + 1];
                    first[j] = holdover;

                }
            }
            if (j > left)
                primary = quikSortTim(primary, mode, left, j);
            if (i < right)
                primary = quikSortTim(primary, mode, i, right);
        }
        return primary;
    }

    public string[] quikSortStr(string[] primary, string type, string mode, int left, int right)
    {
        if (primary.Length > 1)
        {
            int i = left, j = right;
            string pivot = primary[left + (right - left) / 2];

            while (i <= j)
            {
                if (mode == "asc")
                {
                    while (String.Compare(primary[i], pivot) < 0)
                        i++;
                    while (String.Compare(primary[j], pivot) > 0)
                        j--;
                }
                else
                {
                    while (String.Compare(primary[i], pivot) > 0)
                        i++;
                    while (String.Compare(primary[j], pivot) < 0)
                        j--;
                }
                if (i <= j)
                {
                    string holdover = primary[i];
                    primary[i] = primary[j];
                    primary[j] = holdover;
                    if (type == "first")
                    {
                        holdover = last[i];
                        last[i] = last[j];
                        last[j] = holdover;
                    }
                    else
                    {
                        holdover = first[i];
                        first[i] = first[j];
                        first[j] = holdover;
                    }
                    DateTime holdoverBeta = bDay[i];
                    bDay[i] = bDay[j];
                    bDay[j] = holdoverBeta;
                    i++;
                    j++;
                }
            }
            if (j > left)
                primary = quikSortStr(primary, type, mode, left, j);
            if (i < right)
                primary = quikSortStr(primary, type, mode, i, right);
        }
        return primary;
    }

    private void frmNamArrays_SizeChanged(object sender, EventArgs e)
    {
        txtbxNames.Width = this.Width - 40;
        txtbxNames.Height = this.Height - 157;
    }

    private void btnSort_Click(object sender, EventArgs e)
    {
        load();

        switch (cbobxCategory.Text)
        {
            case ("First Name"):
                first = quikSortStr(first, "first", order, 0, first.Length - 1);
                break;
            case ("Last Name"):
                last = quikSortStr(last, "last", order, 0, last.Length - 1);
                break;
            case ("Birthday"):
                bDay = quikSortTim(bDay, order, 0, bDay.Length - 1);
                break;
            default:
                break;
        }

        write();
    }

    private void cbobxOrder_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (cbobxOrder.Text == "Ascending")
            order = "asc";
        else
            order = "desc";
    }

    private void displayfile(string name)
    {
        StreamReader fileData = new StreamReader(name);

        txtbxNames.Lines = fileData.ReadToEnd().Split('\n');

    }

    private void mnuOpen_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        open.Filter = "Text Files|*.txt";
        open.Title = "Select a text file...";

        if (open.ShowDialog() == DialogResult.OK && open.FileName != "")
            displayfile(open.FileName);
    }

    private void mnuExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
}
名称空间名称\u数组
{
公共部分类frmnamarray:表单
{
System.Globalization.CultureInfo culture=新系统.Globalization.CultureInfo(“en CA”);
首先是字符串[];
字符串[]最后一个;
日期时间[]b天;
字符串顺序=“asc”;
字符串格式=“d/M/yyyy”;
公共财政预算案()
{
初始化组件();
}
私有无效写入()
{
string[]行=新字符串[first.Length];
对于(int i=0;i1)
{
int i=左,j=右;
日期时间轴=主[左+(右-左)/2];
而(i 0)
j--;
}
其他的
{
while(DateTime.Compare(primary[i],pivot)>0)
i++;
while(DateTime.Compare(primary[j],pivot)<0)
j--;
}
如果(我离开)
主要=QuikSortim(主要,模式,左侧,j);
如果(我<对)
primary=QuikSortim(primary,mode,i,right);
}
返回主;
}
公共字符串[]quikSortStr(字符串[]主,字符串类型,字符串模式,左整数,右整数)
{
if(primary.Length>1)
{
int i=左,j=右;
字符串轴=主[左+(右-左)/2];
而(i 0)
j--;
}
其他的
{
while(String.Compare(primary[i],pivot)>0)
i++;
while(String.Compare(primary[j],pivot)<0)
j--;
}
如果(我离开)
primary=quikSortStr(primary,type,mode,left,j);
如果(我<对)
primary=quikSortStr(primary,type,mode,i,right);
}
返回主;
}
私有无效frmNamArrays\U SIZECHANGE(对象发送方,事件参数e)
{
txtbxNames.Width=this.Width-40;
txtbxNames.Height=this.Height-157;
}
私有void bSensor\u单击(对象发送方,事件参数e)
{
加载();
开关(cbobxCategory.Text)
{
案例(“名字”):
first=quikSortStr(第一,“第一”,顺序,0,第一。长度-1);
打破
案例(“姓氏”):
last=quikSortStr(last,“last”,顺序,0,last.Length-1);
打破
案例(“生日”):
bDay=quikSortTim(bDay,order,0,bDay.Length-1);
打破
违约:
打破
}
write();
}
私有void cbobxOrder\u SelectedIndexChanged(对象发送方,事件参数e)
{
if(cbobxOrder.Text==“升序”)
order=“asc”;
其他的
order=“desc”;
}
私有void显示文件(字符串名称)
{
StreamReader fileData=新的StreamReader(名称);
txtbxNames.Lines=fileData.ReadToEnd().Split('\n');
}
私有无效MNU打开\单击(对象发送者,事件参数e)
{
OpenFileDialog open=新建OpenFileDialog();
open.Filter=“Text Files |*.txt”;
open.Title=“选择一个文本文件…”;
if(open.ShowDialog()==DialogResult.OK&&open.FileName!=“”)
显示文件(open.FileName);
}
私有void mnuExit_单击(对象发送方,事件参数e)
{
这个。关闭();
}
}
}

您必须在
if(i中的
quikSortStr
方法中更改代码如下
if(i中的
quikSortStr
方法中更改代码,感谢saravanan指出我的第一个错误。超出范围的错误是由意外事故引起的
    DateTime holdoverBeta = bDay[i];
    bDay[i] = bDay[j];
    bDay[j] = holdoverBeta;
    i++;
    j--;//was: j++;
    public DateTime[] quikSortTim(DateTime[] primary, string mode, int left, int right)
    {
        if (primary.Length > 1)
        {
            int i = left, j = right;
            DateTime pivot = primary[left + (right - left) / 2];

            while (i <= j)
            {
                if (mode == "asc")
                {
                    while (DateTime.Compare(primary[i], pivot) < 0)
                        i++;
                    while (DateTime.Compare(primary[j], pivot) > 0)
                        j--;
                }
                else
                {
                    while (DateTime.Compare(primary[i], pivot) > 0)
                        i++;
                    while (DateTime.Compare(primary[j], pivot) < 0)
                        j--;
                }
                if (i <= j)
                {
                    DateTime holdoverB = primary[i];
                    primary[i] = primary[j];
                    primary[j] = holdoverB;

                    string holdover = last[i];
                    last[i] = last[j];
                    last[j] = holdover;

                    holdover = first[i];
                    first[i] = first[j];
                    first[j] = holdover;

                    i++;
                    j--;
                }
            }
            if (j > left)
                primary = quikSortTim(primary, mode, left, j);
            if (i < right)
                primary = quikSortTim(primary, mode, i, right);
        }
        return primary;
    }

    public string[] quikSortStr(string[] primary, string type, string mode, int left, int right)
    {
        if (primary.Length > 1)
        {
            int i = left, j = right;
            string pivot = primary[left + (right - left) / 2];

            while (i <= j)
            {
                if (mode == "asc")
                {
                    while (String.Compare(primary[i], pivot) < 0)
                        i++;
                    while (String.Compare(primary[j], pivot) > 0)
                        j--;
                }
                else
                {
                    while (String.Compare(primary[i], pivot) > 0)
                        i++;
                    while (String.Compare(primary[j], pivot) < 0)
                        j--;
                }
                if (i <= j)
                {
                    string holdover = primary[i];
                    primary[i] = primary[j];
                    primary[j] = holdover;
                    if (type == "first")
                    {
                        holdover = last[i];
                        last[i] = last[j];
                        last[j] = holdover;
                    }
                    else
                    {
                        holdover = first[i];
                        first[i] = first[j];
                        first[j] = holdover;
                    }
                    DateTime holdoverBeta = bDay[i];
                    bDay[i] = bDay[j];
                    bDay[j] = holdoverBeta;
                    i++;
                    j--;
                }
            }
            if (j > left)
                primary = quikSortStr(primary, type, mode, left, j);
            if (i < right)
                primary = quikSortStr(primary, type, mode, i, right);
        }
        return primary;
    }
private void write()
    {
        string[] lines = new string[first.Length];

        for (int i = 0; i < lines.Length; i++)
            if (bDay[i].ToString(format) != "1/1/0001")
                lines[i] = first[i] + ',' + last[i] + ',' + bDay[i].ToString(format);

        lines = lines.Where(s => s != null).ToArray();

        txtbxNames.Clear();
        txtbxNames.Lines = lines;
    }
StreamReader file = new StreamReader(name);
lines = file.ReadToEnd().Split('\n');
lines = File.ReadAllLines(name);