【发布时间】:2015-06-21 17:40:42
【问题描述】:
我想制作一个基于 C# 的程序,该程序具有来自记事本的输入,其中包含数字,然后将其放入第一个列表框中。然后我有第二个列表框,我想显示第一个列表框中的数据,这些数据使用 SELECTION SORT 算法进行排序。我怎样才能做到这一点? 这是我目前写的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Sorting
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
static void selectionSort(int[] list, int n)
{
int i, j;
for (i = 0; i < n; i++)
{
int min = i;
for (j = i + 1; j < n; j++)
if (list[j] < list[min])
{
min = j;
}
int temp = list[i];
list[i] = list[min];
list[min] = temp;
}
private void btn_open_Click(object sender, EventArgs e)
{
string name;
openFileDialog1.Filter = "Text File (*.txt) | *.txt";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
name = openFileDialog1.FileName;
listBox1.Items.AddRange(File.ReadAllLines(name));
}
}
private void btn_sort_Click(object sender, EventArgs e)
{
int[] list = new int[listBox1.Items.Count];
for (int a = 0; a < listBox1.Items.Count; a++)
{
try
{
list[a] = int.Parse(listBox1.Items[a].ToString());
}
catch
{
DialogResult Text = MessageBox.Show("The data types is not number..", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Warning);
break;
}
selectionSort(list, listBox1.Items.Count);
}
for (int i = 0; i < listBox1.Items.Count-1; i++)
{
listBox2.Items.Add(list[i]);
}
}
}
}
但它对排序不起作用..我的代码有什么问题?
【问题讨论】:
-
“它不起作用”不是问题描述。
-
@JohnSaunders 我有一个数据:7,9,4,6,2 但是当我单击排序按钮时,结果是 0,0,2,4.. 那么您认为有问题吗?
-
我认为问题是你没有调试你的应用程序。
标签: c# listbox selection-sort