C# 中的归并排序算法问题
这段代码应该像合并排序算法一样工作,但它不起作用并给出输出 0 而不是排序数字,朋友们有什么问题吗?谢谢
private void button3_Click(object sender, EventArgs e)
{
string[] source = textBox1.Text.Split(',');
string[] source1 = textBox3.Text.Split(',');
int[] nums2 = new int[8];
int[] nums = new int[source.Length];
for (int i = 0; i < source.Length; i++)
{
nums[i] = Convert.ToInt32(source[i]);
}
int[] nums1 = new int[source1.Length];
for (int j = 0; j < source1.Length; j++)
{
nums1[j] = Convert.ToInt32(source1[j]);
}
int x = 0;
int y = 0;
int z = 0;
while (x < nums.Length && y < nums1.Length)
{
if (nums[x] < nums1[y])
{
nums2[z] = nums[x];
x++;
}
else
{
nums2[z] = nums1[y];
y++;
}
z++;
}
while (x > nums.Length){
if (y <= nums1.Length)
{
nums2[z] = nums1[y];
z++;
y++;
}
}
while (y > nums1.Length)
{
if (x <= nums.Length)
{
nums2[z] = nums[x];
z++;
x++;
}
}
string merge = "";
foreach (var n in nums2)
merge += n.ToString() + ",";
textBox4.Text = merge;
}
this code should work like merge sort algorithm but it doesnt work and gives the output 0 instead of sorting numbers,whats the problem friends?thanks
private void button3_Click(object sender, EventArgs e)
{
string[] source = textBox1.Text.Split(',');
string[] source1 = textBox3.Text.Split(',');
int[] nums2 = new int[8];
int[] nums = new int[source.Length];
for (int i = 0; i < source.Length; i++)
{
nums[i] = Convert.ToInt32(source[i]);
}
int[] nums1 = new int[source1.Length];
for (int j = 0; j < source1.Length; j++)
{
nums1[j] = Convert.ToInt32(source1[j]);
}
int x = 0;
int y = 0;
int z = 0;
while (x < nums.Length && y < nums1.Length)
{
if (nums[x] < nums1[y])
{
nums2[z] = nums[x];
x++;
}
else
{
nums2[z] = nums1[y];
y++;
}
z++;
}
while (x > nums.Length){
if (y <= nums1.Length)
{
nums2[z] = nums1[y];
z++;
y++;
}
}
while (y > nums1.Length)
{
if (x <= nums.Length)
{
nums2[z] = nums[x];
z++;
x++;
}
}
string merge = "";
foreach (var n in nums2)
merge += n.ToString() + ",";
textBox4.Text = merge;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了完全获得输出,请尝试
(好吧,使用 Linq & String.Join 或 StringBuilder 可以更快/更好/更漂亮地完成此操作,但出于测试目的,这应该足够了)。
也许这并不能解决上面代码的所有问题,但它可能会帮助您更轻松地调试它。
For getting your output completely, try
(ok, this can be done faster / nicer / fancier using Linq & String.Join, or a StringBuilder, but for testing purposes this should be enough).
Perhaps this does not solve all the problems with your code above, but it will probably help you to debug it easier.
该行
不应位于内部
,因为您要测试每个条件。如果由于
x >= nums.Length - 1
而退出第一个while
循环,则需要确保已运行y
到结束也是如此。The line
Should not be inside
because you want to test for each of those conditions. If you exit your first
while
loop becausex >= nums.Length - 1
, you want to ensure you've runy
through to the end as well.'nums2' 来存储结果,我建议你
应该使用更好的名字。
如果你不这样做,应该使用其他东西
知道确切的长度。例如使用列表
反而。性能较慢但其
更适合小阵列
排序。
实施。如果你有 while 循环并且
需要增量计数,为什么不呢
使用for循环?
增加 z 而不将其重置为
'0' 它将索引超出范围
通过 LINQ 可以更轻松地实现逻辑
'nums2' to store result, i suggest you
should use a better name.
should use something else if you don't
know exact length. eg use List
instead. Slower performance but its
more suitable for small array
sorting.
implement. if you have while loop and
need increment counting, why don't you
use for loop?
incrementing z without reset it to
'0' it will index out of range
The logic is much easier achievable through LINQ