這段代碼給出了輸出,但它有一個問題,即當用戶在textbox1中寫入5,6和在textbox3中輸出時,它輸出5,6。我知道問題是當元素一個數組結束,它不打印其他數組的其餘元素,我評論了問題的線。合併排序算法的輸出問題
編輯:我用TextBox1中和textbox3用於獲取用戶要合併
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++;
}////----->>it works untill here
while (x > nums.Length)///this mean when the elements of nums end,out the rest of the elements in other textbox but it doesnt do anything,whats the problem ?
{
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;
}
mergesort,textboxes,你在說什麼? mergesort中不存在stinkin'texboxes! – 2010-12-18 15:48:19
我用textbox1和textbox3獲取用戶想要合併的數組元素 – Arash 2010-12-18 15:52:58
不,這不是比你以前的重複問題更清晰:http://stackoverflow.com/questions/4477248/problem-with-mergesort -algorithm-in-c – 2010-12-18 15:53:27