Unity中冒泡排序

来源:互联网 发布:大数据行业现状 编辑:程序博客网 时间:2024/06/08 07:33
本篇文章主要记录,如何在Unity中进行冒泡排序。
using UnityEngine;using System.Collections;using System;public class Solt : MonoBehaviour{    public Vector2[] vector2_array = new Vector2[10];    public int[] Num_Array = new int[10];    void Start()    {        Vector2Sort(vector2_array);        IntSort(Num_Array);    }    /// <summary>    ///一维数组冒泡排序    /// </summary>    public int[] IntSort(int[] listInt)    {        try        {            if (listInt != null && listInt.Length != 0)            {                bool flag;                int temp;                for (int i = 0; i < listInt.Length - 1; i++)                {                    flag = false;                    for (int j = 0; j < listInt.Length - i - 1; j++)                    {                        if (listInt[j] > listInt[j + 1])                        {                            temp = listInt[j];                            listInt[j] = listInt[j + 1];                            listInt[j + 1] = temp;                            flag = true;                        }                    }                    if (!flag)                    {                        break;                    }                }                for (int i = 0; i < listInt.Length; i++)                {                    Debug.Log(listInt[i]);                }                return listInt;            }            else            {                return null;            }        }        catch (Exception ex)        {            return null;        }    }    /// <summary>    /// 二维数组冒泡排序,以Vector2为例    /// </summary>    public Vector2[] Vector2Sort(Vector2[] listVector2)    {        try        {            if (listVector2 != null && listVector2.Length != 0)            {                bool flag;                Vector2 temp;                for (int i = 0; i < listVector2.Length - 1; i++)                {                    flag = false;                    for (int j = 0; j < listVector2.Length - i - 1; j++)                    {                        if (listVector2[j].y > listVector2[j + 1].y)                        {                            temp = listVector2[j];                            listVector2[j] = listVector2[j + 1];                            listVector2[j + 1] = temp;                            flag = true;                        }                        else if (listVector2[j].y == listVector2[j + 1].y)                        {                            if (listVector2[j].x > listVector2[j + 1].x)                            {                                temp = listVector2[j];                                listVector2[j] = listVector2[j + 1];                                listVector2[j + 1] = temp;                                flag = true;                            }                        }                    }                    if (!flag)                    {                        break;                    }                }                for (int i = 0; i < listVector2.Length; i++)                {                    Debug.Log(listVector2[i]);                }                return listVector2;            }            else            {                return null;            }        }        catch (Exception ex)        {            return null;        }    }}

运行结果如下所示:
一维数组:
这里写图片描述
二维数组:
这里写图片描述

1 0
原创粉丝点击