summaryrefslogtreecommitdiff
path: root/Assets/Test/05_Recursion/Test_Recursion.cs
blob: ac9de5e1fc3bceda06a6c35beef3f4764bf192c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System.Collections;
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();
        //TestHanoi();
        //TestBinarySearch();
        //TestMergeSort();
        //TestQuickSort();
        TestBubbleSort();
    }

    void TestPermutations()
    {
        // 全排列
        Debug.Log("全排列");
        List<char> data = new List<char> { 'a', 'b', 'c' };
        int count = 0;
        foreach (List<char> p in RecursionHelper.Permutations(data))
        {
            count++;
            string content = "";
            p.ForEach((char c) => content += c + "  ");
            Debug.Log(content);
        }
        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);
    }

}