C# 中的冒泡排序

时间:2023-01-13 11:25:13
        int num;

        int[] arr = new int[] { , , , , , , , , ,  };

        for (int i = ; i < arr.Length; i++)
{ for (int j=i+; j < arr.Length; j++)
{ if (arr[j] > arr[i])
{ num = arr[j]; arr[j] = arr[i]; arr[i] = num; } } } for (int i = ; i < arr.Length; i++)
{ Debug.Log(arr[i]); }

另一种遍历方式:

     int temp;
int[] arr = new[] {, , , , , , };
for (int j = arr.Length -; j > ; j--)
{
for (int i = ; i < j; i++)
{
if (arr[i] > arr[i + ])
{
temp = arr[i];
arr[i] = arr[i + ];
arr[i + ] = temp;
}
}
}