我目前正在构建一个排序程序。我已经做了三种不同的方法来创建一个数组Random
:In Order
和Reverse
。我目前遇到了In Order
和Reverse
数组的问题。每次In Order
创建数组时,它都以 a 开头1
,我不知道为什么会这样做。此外,我的Reverse
数组不会每次都显示乱序的数字,而是在单击后显示。我该如何解决这两个问题?
namespace sortingMachine
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Class Level Variables --------------------------------------------------------------------------------------
Stopwatch sw = new Stopwatch();
Random r = new Random();
OpenFileDialog open1 = new OpenFileDialog();
long operations = 0;
int size;
int max;
int[] createArray;
int[] sortArray;
int[] copyArray;
//Create Array Methods --------------------------------------------------------------------------------------
public void RandomNumber()
{
size = Convert.ToInt32(textBoxSize.Text);
max = Convert.ToInt32(textBoxMax.Text);
createArray = new int[size];
copyArray = new int[size];
sortArray = new int[size];
for (int i = 0; i < size; i++)
{
createArray[i] = r.Next(1, max);
}
textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "Random" + Environment.NewLine + Environment.NewLine);
DisplayArrays();
}
public void InOrder()
{
size = Convert.ToInt32(textBoxSize.Text);
max = Convert.ToInt32(textBoxMax.Text);
createArray = new int[size];
copyArray = new int[size];
sortArray = new int[size];
createArray[0] = 1;
for (int i = 1; i < size; i++)
{
createArray[i] = createArray[i - 1] + r.Next(1, max);
}
for (int i = 1; i < size; i++)
{
if (r.Next(1, 101) < Convert.ToInt32(textBoxPercentage.Text))
{
for (int x = 1; x < size; x++)
{
createArray[x] = r.Next(1, createArray[size - 1]);
}
}
}
textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "In Order" + Environment.NewLine + Environment.NewLine);
DisplayArrays();
}
public void ReverseOrder()
{
size = Convert.ToInt32(textBoxSize.Text);
max = Convert.ToInt32(textBoxMax.Text);
createArray = new int[size];
copyArray = new int[size];
sortArray = new int[size];
createArray[size - 1] = 1;
for (int i = size - 1; i > 0; i--)
{
createArray[i - 1] = createArray[i] + r.Next(1, max);
}
for (int i = size - 1; i > 0; i--)
{
if (r.Next(1, 101) < createArray[0])
{
for (int x = size - 1; x > 0; x--)
{
createArray[x] = r.Next(1, createArray[0]);
}
}
}
textBoxResults.AppendText("-------------------------------------------------------------------------------" + Environment.NewLine + "Reverse Order" + Environment.NewLine + Environment.NewLine);
DisplayArrays();
}
private void buttonCreateArray_Click(object sender, EventArgs e)
{
if ((textBoxSortKey.Text != "") && (textBoxCreateKey.Text != ""))
{
if (radioButtonRandom.Checked == true)
{
RandomNumber();
}
if (radioButtonInOrder.Checked == true)
{
InOrder();
}
if (radioButtonReverseOrder.Checked == true)
{
ReverseOrder();
}
}
else
{
MessageBox.Show("Type a key into the Key textbox.");
}
}
}
}
显示结果:
订单数组我不确定为什么它总是以一个开头:
-------------------------------------------------------------------------------
In Order
1
2
4
6
10
有时逆序数组会是这样的:
-------------------------------------------------------------------------------
Reverse Order
10
2
7
6
5
窗体: