冒泡排序与选择排序
namespace day5
{
class Program
{
static void Main(string[] args)
{
int[] nums = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
int[] nums1 = BubbleSort(nums,1);
int[] nums2 = SelectSort(nums,1);
//打印数组
for (int i = 0; i < nums2.Length; i++)
{
Console.Write(nums2[i]);
}
Console.ReadKey();
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array">导入的数组</param>
/// <param name="direction">1:从小到大 0:从大到小</param>
/// <returns>返回数组</returns>
public static int[] BubbleSort(int[] array, int direction)
{
if (direction==1)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length-1-i; j++)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
if (direction==0)
{
for (int i = 0; i < array.Length - 1; i++)
{
for (int j = 0; j < array.Length-1-i; j++)
{
if (array[j] < array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
return array;
}
/// <summary>
/// 选择排序
/// </summary>
/// <param name="array">导入的数组</param>
/// <param name="direction">1:从小到大 0:从大到小</param>
/// <returns>返回数组</returns>
public static int[] SelectSort(int[] array, int direction)
{
if (direction==0)
{
for (int i = 0; i < array.Length -1; i++)
{
int maxIndex = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[maxIndex] < array[j])
maxIndex = j;
}
if (maxIndex != i)
{
int temp = array[i];
array[i] = array[maxIndex];
array[maxIndex] = temp;
}
}
}
if (direction==1)
{
for (int i = 0; i < array.Length -1; i++)
{
int minIndex = i;
for (int j = i + 1; j < array.Length; j++)
{
if (array[minIndex] > array[j])
minIndex = j;
}
if (minIndex != i)
{
int temp = array[i];
array[i] = array[minIndex];
array[minIndex] = temp;
}
}
}
return array;
}
}
}
二维数组
int[,] array = new int[,];
array.GetLength(0)? 行数??array.GetLength(1)? 列数
2048移动逻辑代码
// 2048核心算法
static void Main()
{
int[,] map = new int[4, 4]
{
{2,2,4,8},
{2,4,4,4},
{0,8,4,0},
{2,4,0,4}
};
PrintArray(map);
Console.WriteLine("上移");
map = MoveUp(map);
PrintArray(map);
Console.WriteLine("下移");
map = MoveDown(map);
PrintArray(map);
Console.WriteLine("左移");
map = MoveLeft(map);
PrintArray(map);
Console.WriteLine("右移");
map = MoveRight(map);
PrintArray(map);
}
/// <summary>
/// 控制台展示矩阵
/// </summary>
/// <param name="array">矩阵</param>
private static void PrintArray(Array array)
{
for (int r = 0; r < array.GetLength(0); r++)
{
for (int c = 0; c < array.GetLength(1); c++)
{
Console.Write(array.GetValue(r,c) + "\t");
}
Console.WriteLine();
}
}
/// <summary>
/// 数组非零元素移至左侧
/// </summary>
/// <param name="array">一维数组</param>
/// <returns>一维数组</returns>
private static int[] RemoveZero(int[] array)
{
// 新数组
int[] newArray = new int[array.Length];
// 将非零元素依次赋值给新数组
int index = 0;
for (int i = 0; i < array.Length; i++)
if (array[i] != 0)
newArray[index++] = array[i];
return newArray;
}
/// <summary>
/// 合并相同数据
/// </summary>
/// <param name="array">一维数组</param>
/// <returns>一维数组</returns>
private static int[] Merge(int[] array)
{
array = RemoveZero(array);
// 合并数据
for (int i = 0; i < array.Length-1; i++)
{
// 相邻相同
if (array[i] != 0 && array[i] == array[i+1])
{
array[i] += array[i + 1];
array[i + 1] = 0;
// 统计合并位置
}
}
array = RemoveZero(array);
return array;
}
/// <summary>
/// 2048上移
/// </summary>
/// <param name="map">矩阵</param>
/// <returns>矩阵</returns>
private static int[,] MoveUp(int[,] map)
{
// 从上到下 获取列数据,形成一维数组
int[] mergeArray = new int[map.GetLength(0)];
for (int c = 0; c < map.GetLength(1); c++)
{
for (int r = 0; r < map.GetLength(0); r++)
mergeArray[r] = map[r, c];
mergeArray = Merge(mergeArray);
for (int r = 0; r < map.GetLength(0); r++)
map[r, c] = mergeArray[r];
}
return map;
}
/// <summary>
/// 2048下移
/// </summary>
/// <param name="map">矩阵</param>
/// <returns>矩阵</returns>
private static int[,] MoveDown(int[,] map)
{
// 从下到上 获取列数据,形成一维数组
int[] mergeArray = new int[map.GetLength(0)];
for (int c = 0; c < map.GetLength(1); c++)
{
for (int r = map.GetLength(0)-1; r >= 0 ; r--)
// 从头到尾存入一维数组
mergeArray[map.GetLength(0)-1-r] = map[r, c];
mergeArray = Merge(mergeArray);
for (int r = map.GetLength(0)-1; r >= 0 ; r--)
map[r, c] = mergeArray[map.GetLength(0)-1-r];
}
return map;
}
/// <summary>
/// 2048左移
/// </summary>
/// <param name="map">矩阵</param>
/// <returns>矩阵</returns>
private static int[,] MoveLeft(int[,] map)
{
// 从左到右 获取列数据,形成一维数组
int[] mergeArray = new int[map.GetLength(1)];
for (int r = 0; r < map.GetLength(0); r++)
{
for (int c = 0; c < map.GetLength(1); c++)
mergeArray[c] = map[r, c];
mergeArray = Merge(mergeArray);
for (int c = 0; c < map.GetLength(1); c++)
map[r, c] = mergeArray[c];
}
return map;
}
/// <summary>
/// 2048右移
/// </summary>
/// <param name="map">矩阵</param>
/// <returns>矩阵</returns>
private static int[,] MoveRight(int[,] map)
{
// 从右到左 获取列数据,形成一维数组
int[] mergeArray = new int[map.GetLength(1)];
for (int r = 0; r < map.GetLength(0); r++)
{
for (int c = map.GetLength(1) - 1; c >= 0; c--)
mergeArray[map.GetLength(1) - 1 - c] = map[r, c];
mergeArray = Merge(mergeArray);
for (int c = map.GetLength(1) - 1; c >= 0; c--)
map[r, c] = mergeArray[map.GetLength(1) - 1 - c];
}
return map;
}
|