using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MaoPao : MonoBehaviour {
private int[] number = { 2, 4, 5, 7, 8, 9, 1, 0, 3, 6 };
void Start()
{
int pos = 1;
int k = number.Length - 1; //初始是数组长度-1
int m = 0;
for (int i = 0; i < number.Length - 1; i++)
{
if (pos == 0)
{
break; //如果上一次循环没有发生交换 说明数组已经是有序的 直接跳出
}
pos = 0;
for (int j = 0; j < k; j++)
{
if (number[j] > number[j + 1])
{
int temmp = number[j + 1];
number[j + 1] = number[j];
number[j] = temmp;
pos = 1;
m = j;
}
}
k = m; //外层循环每一趟记录最后交换的位置 比较number.length - 1 - i 会有优化
}
foreach (int item in number)
{
Debug.Log(item);
}
}
}
冒泡排序的优化
最新推荐文章于 2023-05-23 09:29:34 发布