冒泡排序

来源:互联网 发布:从数组中删除指定元素 编辑:程序博客网 时间:2024/05/23 18:31

//冒泡排序(Bubble Sort)的基本思想是:将相邻的记录的关键码进行比较,若前面记录的关键码大于后面记录的关键码,则将它们交换,否则不交换。

using UnityEngine;using System.Collections;public class MaoPao : MonoBehaviour {    // Use this for initialization    public int temp = 0;    public int[] arr = { 23, 44, 66, 76, 98, 11, 3, 9, 7 };                  IEnumerator Start () {    for (int i = 0; i < arr.Length - 1; i++)    {//# region将大的数字移到数组的arr.Length-1-i        for (int j = 0; j < arr.Length - 1 - i; j++)        {            if (arr[j] > arr[j + 1])            {                temp = arr[j + 1];                arr[j + 1] = arr[j];                yield return new WaitForSeconds(3f);                arr[j] = temp;                    print(""+ temp);            }        }//#endregion    }}}


0 0
原创粉丝点击