气泡排序列表框不起作用C# [英] Bubble sort for Listbox not working C#

查看:66
本文介绍了气泡排序列表框不起作用C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,用户可以在其中输入用户也可以得到的列表框中的数字,并可以对列表框进行排序.我不允许使用任何数组或容器或列表,只需修改项目列表框属性并使用转换和解析即可.我想通过气泡排序来做到这一点,尽管单击排序按钮后仅在列表框上显示的数字是0、1、2、3、4 ...

I have a program where the user can input number into a listbox the user also gets and option to sort the listbox. I am not allowed to use any arrays or containers or list, just modify the items listbox property and use converting and parsing. I want to do this through a bubble sort, although the numbers that only displays on the listbox once the sort button is clicked is 0,1,2,3,4...

private void sorted()
    {       
       int a = Convert.ToInt32(lstHoldValue.Items.Count);
        int temp = Convert.ToInt32(lstHoldValue.Items[0]); 
        for (int i = 0; i < a; i++)
        {
            for (int j = i + 1; j < a; j++)
            { 
                if (Convert.ToInt32(lstHoldValue.Items[i]) > Convert.ToInt32(lstHoldValue.Items[j]))

                {
                    temp = Convert.ToInt32(lstHoldValue.Items[i]);
                   (lstHoldValue.Items[i]) = Convert.ToInt32(lstHoldValue.Items[j]);
                   (lstHoldValue.Items[j]) = temp;
                }
            }
        }
        lstHoldValue.Items.Clear();
        for (int i = 0; i < a; i++)
        {
           Convert.ToInt32(lstHoldValue.Items.Add("\t" + i));  
        }
    }

用户如何在列表框中输入值

How users enter values to the listbox

   private void btnAdd_Click(object sender, EventArgs e)
{         
    string text = "\t" + txtInitialise.Text;
        if (this.index < MAX_ITEMS) // MAX_ITEMS or 10 
        {
            Convert.ToInt32(lstHoldValue.Items.Count);
            int dnum;
            if (int.TryParse(txtInitialise.Text, out dnum))
            {
               Convert.ToInt32(lstHoldValue.Items.Add( "\t" + dnum));
                index++;
                txtInitialise.Text = "";

推荐答案

似乎您只是将List项目的索引添加到List,这就是它始终返回0-4(5个元素)的原因.将答案更新为仅使用列表项进行排序.

Seems like you are just adding the index of the List item to List, that's the reason it always returning 0 - 4 (5 elements). Updated the answer to sort only using the list items.

已更新:

最多可插入列表框的项目:

Maximum items allowed to insert into listbox:

const int MAX_ITEMS = 10;

尽管最后清除了列表框,但您的排序方法仍正常工作,因为您使用for循环在列表框内进行了交换,因此您失去了排序功能:

Your sorted method was working correctly, though you cleared the listbox in the end, so you lose the sorting, as swapping is done inside the listbox using the for-loop:

private void sorted()
{
    int a = Convert.ToInt32(lstHoldValue.Items.Count);
    int temp = Convert.ToInt32(lstHoldValue.Items[0]);

    for (int i = 0; i < a; i++)
    {
        for (int j = i + 1; j < a; j++)
        {
            if (Convert.ToInt32(lstHoldValue.Items[i]) > Convert.ToInt32(lstHoldValue.Items[j]))

            {
                temp = Convert.ToInt32(lstHoldValue.Items[i]);
                (lstHoldValue.Items[i]) = "\t" + Convert.ToInt32(lstHoldValue.Items[j]);
                (lstHoldValue.Items[j]) = "\t" + temp;
            }
        }
    }
}

添加按钮单击:

private void btnAdd_Click(object sender, EventArgs e)
{
    int index = 0;
    if (index < MAX_ITEMS) // MAX_ITEMS set to 10
    {                
        int dnum;
        if (int.TryParse(txtInitialise.Text, out dnum))
        {
            lstHoldValue.Items.Add("\t" + dnum);
            index++;
            txtInitialise.Text = "";
        }
    }
}

点击排序按钮

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

这篇关于气泡排序列表框不起作用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆