# C# 入门算法“冒泡排序“ 升序 降序 最大值 最小值 平均值

``````int[] arr = {12,18,4,6,80,10,14,12,66,19};

for (int i = 0; i < arr.Length; i++)
{
for (int j=0;j<arr.Length-1;j++)
{
//大于号就是 大 到 小
if (arr[i] < arr[j])
{
int number = arr[i];
arr[i] = arr[j];
arr[j] = number;

}
}
}

foreach (int look in arr)
{
Console.Write(look+" ");

}

``````

``````  int[] arr = {12,18,4,6,80,10,14,12,66,19};
``````

`````` for (int i = 0; i < arr.Length; i++)
{
for (int j=0;j<arr.Length-1;j++)
{

if (arr[i] < arr[j])
{
int number = arr[i];
arr[i] = arr[j];
arr[j] = number;
}
}
}
``````

``````if (arr[i] < arr[j])
{
int number = arr[i];
arr[i] = arr[j];
arr[j] = number;
``````

``````  if (arr[i] < arr[j])
{
arr[i] = arr[i]+arr[j];
arr[j] = arr[i]-arr[j];
arr[i] = arr[i] - arr[j];
}
``````

``````int a =10;
int  b =20;
a =a+b;
b =a+b;
a=a-b;
``````

i的每一个元素 j 都会一个个对比

``````//变成大于号
if (arr[i] > arr[j])
{
int number = arr[i];
arr[i] = arr[j];
arr[j] = number;
}
``````

`````` int[] arr = {12,18,4,6,80,10,14,12,66,19};
Array.Sort(arr);
//一建对数组 进行升序的排序
``````

``````int[] arr = {12,18,4,6,80,10,14,12,66,19};
Array.Sort(arr);
//升序排序
Array.Reverse(arr);
//数祖反转达到 降序效果
``````

`````` int[] arr = {-12,18,4,26,100,10,14,12,66,-19};
int max = 0;
for (int i = 0; i < arr.Length; i++)
{    //大于号就是 最大值
if (arr[i] <max)
{

max = arr[i];
}
}
Console.WriteLine(max);
``````

``````int[] arr = {-12,18,4,26,100,10,14,12,66,-19};
int max = 0;
for (int i = 0; i < arr.Length; i++)
{
max +=arr[i];
}

Console.WriteLine(max/arr.Length-1);

``````

`````` Random r = new Random();
int[] arr = new int[10];
int average = 0;
for (int i = 0; i < arr.Length; i++)
{
arr[i] = r.Next(-10,100);

}

for (int i = 0; i < arr.Length; i++)
{
average += arr[i];

for (int j = 0; j < arr.Length - 1; j++)
{
if (arr[i] < arr[j])
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}

}
}

foreach (int number in arr)
{

Console.Write(number + " ");
}

Console.WriteLine("n最小值是：" + arr[0]);
Console.WriteLine("最大值是：" + arr[arr.Length-1]);
Console.WriteLine("平均值是：" + (average / arr.Length - 1));