增加通过次数
我想增加传递次数,但到目前为止我的输出显示的格式不正确。例如,我输入了 5 个数字,如 5 4 3 2 1。格式为:
Pass 1: 1 4 3 2 5
通过 2: 1 4 3 2 5
通过 3: 1 2 3 4 5
通过 4: 1 2 3 4 5
通过 5: 1 2 3 4 5
但我当前的输出是:
通过 1: 2: 3: 4: 5: 1 4 3 2 5
通过 1: 2: 3: 4: 5: 1 4 3 2 5
通过 1: 2: 3: 4: 5: 1 2 3 4 5
通过 1: 2: 3: 4: 5: 1 2 3 4 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5
我被 for 循环语句困住了。任何想法我将如何做这种输出。我正在做一个快速排序。
int[] nums = new int[100];
int SizeNum;
bool isNum = false;
private void ExeButton_Click(object sender, EventArgs e)
{
string SizeString = SizeTextBox.Text;
isNum = Int32.TryParse(SizeString, out SizeNum);
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
if (SizeNum == numsInString.Length)
{
sortArray();
}
}
}
public void q_sort(int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = nums[left];
while (left < right)
{
while ((nums[right] >= pivot) && (left < right))
{
right--;
}
if (left != right)
{
nums[left] = nums[right];
left++;
}
while ((nums[left] <= pivot) && (left < right))
{
left++;
}
if (left != right)
{
nums[right] = nums[left];
right--;
}
}
nums[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
Display();
if (left < pivot)
{
q_sort(left, pivot - 1);
}
if (right > pivot)
{
q_sort(pivot + 1, right);
}
}
public void sortArray()
{
q_sort(0, SizeNum - 1);
}
public void Display()
{
int i;
int x;
String numbers = "";
ResultText.AppendText("Pass ");
for (x = 1; x < SizeNum; x++)
{
ResultText.AppendText(" " + x + ": ");
}
for (i = 0; i < SizeNum; i++)
{
numbers += nums[i].ToString() + " , ";
}
ResultText.AppendText(numbers + "\n");
}
I want to increment the number of passes but the output I have so far displays incorrect format. For example, I entered 5 numbers like 5 4 3 2 1. The format would be:
Pass 1: 1 4 3 2 5
Pass 2: 1 4 3 2 5
Pass 3: 1 2 3 4 5
Pass 4: 1 2 3 4 5
Pass 5: 1 2 3 4 5
but my current output is:
Pass 1: 2: 3: 4: 5: 1 4 3 2 5
Pass 1: 2: 3: 4: 5: 1 4 3 2 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5
Pass 1: 2: 3: 4: 5: 1 2 3 4 5
I'm stuck with the for loop statements. Any ideas how will I do this kind of output. I'm doing a quick sort.
int[] nums = new int[100];
int SizeNum;
bool isNum = false;
private void ExeButton_Click(object sender, EventArgs e)
{
string SizeString = SizeTextBox.Text;
isNum = Int32.TryParse(SizeString, out SizeNum);
string[] numsInString = EntNum.Text.Split(' '); //split values in textbox
for (int j = 0; j < numsInString.Length; j++)
{
nums[j] = int.Parse(numsInString[j]);
}
if (SizeNum == numsInString.Length)
{
sortArray();
}
}
}
public void q_sort(int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = nums[left];
while (left < right)
{
while ((nums[right] >= pivot) && (left < right))
{
right--;
}
if (left != right)
{
nums[left] = nums[right];
left++;
}
while ((nums[left] <= pivot) && (left < right))
{
left++;
}
if (left != right)
{
nums[right] = nums[left];
right--;
}
}
nums[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
Display();
if (left < pivot)
{
q_sort(left, pivot - 1);
}
if (right > pivot)
{
q_sort(pivot + 1, right);
}
}
public void sortArray()
{
q_sort(0, SizeNum - 1);
}
public void Display()
{
int i;
int x;
String numbers = "";
ResultText.AppendText("Pass ");
for (x = 1; x < SizeNum; x++)
{
ResultText.AppendText(" " + x + ": ");
}
for (i = 0; i < SizeNum; i++)
{
numbers += nums[i].ToString() + " , ";
}
ResultText.AppendText(numbers + "\n");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
最简单的方法是通过快速排序函数传递调试变量以跟踪当前状态,例如:
The easiest way would be to pass a debugging variable along through your quicksort function to keep track of the current state, e.g.:
您的 SizeNum 始终为 5,因此 Display: 中的循环
将始终打印 1-5。您需要以某种方式让 Display 知道它被调用了多少次才能获得正确的标签。
your SizeNum is always 5 so the loop in Display:
will always print 1-5. You need to somehow let Display know how many times its been called in order to get the right label.
让我们看一下第一个 for 循环:
请注意,您正在从
1
计数到SizeNum
。此循环将每个数字附加到您的结果中,这就是为什么您在每行上得到Pass 1: 2: 3: 4: 5:
的原因。如果您接受更大的数字数组,您将看到输出反映了这一点。例如,如果您输入一个包含 10 个数字的数组,您将看到Pass 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
后跟该数组。为了获得您正在寻找的内容,您需要将当前迭代传递给您的函数,正如一些答案已经指出的那样,如下所示:
Let's take a look at that first for loop:
Note that you're counting from
1
toSizeNum
. This loop appends each number to your result, which is why you getPass 1: 2: 3: 4: 5:
on each line. If you take in a larger array of numbers, you'll see the output reflect that. For example, if you take in an array of 10 numbers, you'll seePass 1: 2: 3: 4: 5: 6: 7: 8: 9: 10:
followed by the array.To get what you're looking for, you'll need to pass in the current iteration to your function, as some answers have already pointed out, like so:
在类的顶部添加
,然后在显示例程中替换
为
由于您的代码看起来可以运行多次,您可能想在 SortArray(); 中重置 Pass=1;
at the top of your class add
and then in your display routine replace
with
As your code looks like it could run more than once you probably want to reset Pass=1 in SortArray();