C#的数据结构
今天看了C的数据结构,无聊写了一些C#的数据结构算法.
class Program
{
static void Main(string[] args)
{
int[] list = new int[]{10,9,8,7,6,5,4,3,2,1};
bubbleSort(list);
}
//冒泡算法
public static void bubbleSort(int [] list)
{
int temp;
int count=0;
for (int j = list.Length; j>0;j--)//外循环
{
for (int i = 0; i <j-1; i++)//内循环
{
if (list[i] > list[i + 1])
{
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
count++;
}
}
Console.WriteLine("list值:" + list[j-1].ToString ());
}
Console.WriteLine(count);
Console.ReadLine();
}
//选择算法
public static void selectSort(int [] list)
{
int temp = 0;
int count = 0;
for (int j = 0; j <list.Length ; j++)
{
int MinNumber = list[j];
int Index =j;
for (int i = j; i < list.Length; i++) //找最小值
{
if (MinNumber > list[i])
{
MinNumber = list[i];
Index = i;
count++;
}
}
temp = list[Index];
list[Index] = list[j];
list[j] = temp;
Console.WriteLine("list值:"+list[j]);
}
Console.WriteLine(count);
Console.ReadLine();
}
}
class Program
{
static void Main(string[] args)
{
int[] list = new int[]{10,9,8,7,6,5,4,3,2,1};
bubbleSort(list);
}
//冒泡算法
public static void bubbleSort(int [] list)
{
int temp;
int count=0;
for (int j = list.Length; j>0;j--)//外循环
{
for (int i = 0; i <j-1; i++)//内循环
{
if (list[i] > list[i + 1])
{
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
count++;
}
}
Console.WriteLine("list值:" + list[j-1].ToString ());
}
Console.WriteLine(count);
Console.ReadLine();
}
//选择算法
public static void selectSort(int [] list)
{
int temp = 0;
int count = 0;
for (int j = 0; j <list.Length ; j++)
{
int MinNumber = list[j];
int Index =j;
for (int i = j; i < list.Length; i++) //找最小值
{
if (MinNumber > list[i])
{
MinNumber = list[i];
Index = i;
count++;
}
}
temp = list[Index];
list[Index] = list[j];
list[j] = temp;
Console.WriteLine("list值:"+list[j]);
}
Console.WriteLine(count);
Console.ReadLine();
}
}