From 5d81d8d8b4062def695c27fa02f67625871e8dce Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 21 Jun 2021 22:59:39 +0800 Subject: *misc --- Assets/Test/05_Recursion/Test_Recursion.cs | 96 +++++++++++++++++++++++++++++- 1 file changed, 94 insertions(+), 2 deletions(-) (limited to 'Assets/Test/05_Recursion/Test_Recursion.cs') diff --git a/Assets/Test/05_Recursion/Test_Recursion.cs b/Assets/Test/05_Recursion/Test_Recursion.cs index ef72a21..ac9de5e 100644 --- a/Assets/Test/05_Recursion/Test_Recursion.cs +++ b/Assets/Test/05_Recursion/Test_Recursion.cs @@ -2,19 +2,26 @@ using System.Collections.Generic; using AlgorithmCollection; using AlgorithmCollection.Recursion; +using AlgorithmCollection.Searching; +using AlgorithmCollection.Sorting; using UnityEngine; public class Test_Recursion : MonoBehaviour { void Start() { - TestPermutations(); + //TestPermutations(); + //TestHanoi(); + //TestBinarySearch(); + //TestMergeSort(); + //TestQuickSort(); + TestBubbleSort(); } void TestPermutations() { // 全排列 - Debug.Log("====全排列===="); + Debug.Log("全排列"); List data = new List { 'a', 'b', 'c' }; int count = 0; foreach (List p in RecursionHelper.Permutations(data)) @@ -27,4 +34,89 @@ public class Test_Recursion : MonoBehaviour Debug.Assert(count == Algorithms.Factorial(data.Count)); } + // 汉诺塔,a柱上顺序大小的积木移动到c柱 + void TestHanoi() + { + Debug.Log("汉诺塔"); + Hanoi(3, 'a', 'b', 'c'); + } + + // 把n个积木从a移动到c + void Hanoi(int n, char a, char b, char c) + { + if (n <= 0) + return; + Hanoi(n - 1, a, c, b); // 把a柱上面n-1个移动到b柱 + Debug.Log(a + "->" + c); // 把a柱下面最大的积木移到c柱,这个大积木移动完毕可以不用考虑 + Hanoi(n - 1, b, a, c); // 把b柱的n-1个移动到c柱 + } + + void TestBinarySearch() + { + Debug.Log("二分搜索"); + int[] value = {1,2,5,6,7,8 }; //升序数组 + int index = SearchingHelper.BinarySearch(value, 6); + Debug.Log(index); + } + + void TestMergeSort() + { + int[] value = { 9, 7, 2, 3, 4, 6, 1 }; + SortingHelper.MergeSort(value, true); + string content = ""; + for (int i = 0; i < value.Length; ++i) + { + content += value[i] + " "; + } + Debug.Log(content); + + SortingHelper.MergeSort_NoneRecursion(value); + content = ""; + for (int i = 0; i < value.Length; ++i) + { + content += value[i] + " "; + } + Debug.Log(content); + } + + void TestQuickSort() + { + int[] value = { 9, 7, 2, 3, 4, 6, 1 }; + SortingHelper.QuickSort(value, true); + string content = ""; + for (int i = 0; i < value.Length; ++i) + { + content += value[i] + " "; + } + Debug.Log(content); + + SortingHelper.QuickSort(value); + content = ""; + for (int i = 0; i < value.Length; ++i) + { + content += value[i] + " "; + } + Debug.Log(content); + } + + void TestBubbleSort() + { + int[] value = { 9, 7, 2, 3, 4, 6, 1 }; + SortingHelper.BubbleSort(value, true); + string content = ""; + for (int i = 0; i < value.Length; ++i) + { + content += value[i] + " "; + } + Debug.Log(content); + + SortingHelper.BubbleSort(value); + content = ""; + for (int i = 0; i < value.Length; ++i) + { + content += value[i] + " "; + } + Debug.Log(content); + } + } -- cgit v1.1-26-g67d0