算法之“鱼龙混杂”-3
鱼龙混杂:
有两个有序(按从小到大排序)数组,现在将数组1添加到数组2,要求数组2只包含数组1中所有值不相同的数据元素!
算法思路:先将数组1不相同的元素值添加到数组2,然后再对数组2进行排序!@^_^@
using System;
using System.Collections.Generic;
namespace run
{
class Program
{
static void Main(string[] args)
{
int[] Array = new int[] { 2, 14, 15, 18, 25, 56 };
int[] Arr = new int[] { 2, 3, 14, 15, 19, 20, 88 };
List<int> list = new List<int>();
list.AddRange(Array);
Console.WriteLine("数据1:");
foreach (int s in list)
{
Console.Write(s + " ");
}
Console.WriteLine("\n数据2:");
List<int> list1 = new List<int>();
list1.AddRange(Arr);
foreach (int y in list1)
{
Console.Write(y + " ");
}
foreach (int n in list)
{
if (list1.Contains(n) == false)
{
list1.Add(n);
}
}
Console.WriteLine("\n排序后:");
foreach (int k in list1)
{
Console.Write(k + " ");
}
#region 冒泡排序
Console.WriteLine("\n对排序后的数据进行从小到大排序(使用冒泡排序法):");
int temp = 0;
for (int i = 0; i < list1.Count - 1; i++)
{
for (int k = 0; k < list1.Count - i - 1; k++)
{
if (list1[k] > list1[k + 1])
{
temp = list1[k];
list1[k] = list1[k + 1];
list1[k + 1] = temp;
}
}
}
foreach (int y in list1)
{
Console.Write(y + " ");
}
#endregion
#region 选择排序
Console.WriteLine("\n对排序后的数据进行从小到大排序(使用选择排序法):");
int t = 0, tm = 0;
for (int i = 0; i < list1.Count - 1; i++)
{
t = i;
for (int b = i + 1; b < list1.Count - 1; b++)
{
if (list1[t] > list1[b])
{
t = b;
}
if (t != i)
{
tm = list1[t];
list1[t] = list1[i];
list1[i] = tm;
}
}
}
foreach (int y in list1)
{
Console.Write(y + " ");
}
#endregion
Console.ReadKey();
}
}
}
运行结果:


浙公网安备 33010602011771号