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
- Unity中冒泡排序
- JAVA中冒泡排序
- java中冒泡排序
- Python中冒泡排序:
- JS中冒泡排序
- C#中冒泡排序法
- Java中冒泡排序算法
- 冒泡排序3中方法
- Java中数组冒泡排序
- java中得冒泡排序
- java中关于冒泡排序
- Java中冒泡排序法
- java中冒泡排序与选择排序
- C#中冒泡排序和比较排序
- Java中冒泡排序和选择排序
- java中选择排序和冒泡排序
- Java中常见排序之冒泡排序
- java中冒泡排序和选择排序
- magicleap 资料
- 输入你的邮箱账号,手动向你邮箱发邮件
- shader 基础
- NHibernate Linq查询 扩展增强 (第九篇)
- mybatis生成动态sql的常用标签
- Unity中冒泡排序
- 动手打造Bypass UAC自动化测试小工具,可绕过最新版Win10
- AngularJs 基础教程 —— 控制器
- 判断物体位于什么位置,用于添加方向指针
- Linux中使用vim乱码
- Hibernate 一对多双向操作
- Swift之构造器(下)
- 自动释放池和消息循环的关系
- NHibernate之一级缓存(第十篇)