In the field of games, the algorithm is also a part, so I started to learn the algorithm,
Write a quick sort in C#
unity
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ValueAnd : MonoBehaviour { // Use this for initialization void Start() { int[] arr = { 1, 2, 3 ,4, 5, 6, 7,8}; bubble(arr); foreach (var i in arr) { Debug.Log(i); } } void changeOther(int[] arr, int a, int b) //it’s nice { arr[a] = arr[a] + arr[b]; //sum arr[b] = arr[a] - arr[b]; //sum-b gets a arr[a] = arr[a] - arr[b]; //similarly get b } void bubble(int[] arr) { int lenth = arr.Length; for (int outSet = lenth - 1; outSet >= 1; outSet--) { bool First = true; for (int inner = 0; inner < lenth - 1; inner++) { Debug.Log(inner); if (arr[inner] > arr[inner + 1]) { changeOther(arr, arr[inner], arr[inner + 1]); if (First) First = false; } } Debug.Log(outSet); if (First){ break; } } } }
Two exchange methods, one is int a temp, and then exchange,
int a; int b; int temp;
temp=a;a=b;b=temp;
Well enough,
temp=a+b;
b=temp-b;
a=temp-b;
First is whether the mark is the first time