unity C#Queue增删改查并记录时间

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Diagnostics;
using System;

public class Queue : MonoBehaviour
{
    Queue<int> Q1 = new Queue<int>(100);
    int[] A = new int[100];
    int[] B = new int[100];
    public int que;
    // Start is called before the first frame update
    void Start()
    {

        UnityEngine.Debug.Log("初始队列");
        for (int i = 0; i < 100; i++)
        {
            Q1.Enqueue(i + 1);
            //UnityEngine.Debug.Log(Q1.Peek().ToString());
        }
        foreach (int y in Q1)
        {
            UnityEngine.Debug.Log(y);
        }
        A = Q1.ToArray();//将队列转化为数组
        Q1.Clear();
        UnityEngine.Debug.Log("打乱后的数列");
        for (int i = 0; i < A.Length; i++)
        {
            int index = UnityEngine.Random.Range(0, A.Length - 1);
            int num;
            num = A[i];
            A[i] = A[index];
            A[index] = num;
        }

        for (int i = 0; i < A.Length; i++)
        {
            Q1.Enqueue(A[i]);

        }
        foreach (int x in Q1)
        {
            UnityEngine.Debug.Log(x);
        }

        FindValue();
        Order();
        Delete();
        Insert();
        Clear();
    }

    private void FindValue()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();

        //UnityEngine.Debug.Log(Q1.Peek());
        B = Q1.ToArray();
        for (int i = 0; i < B.Length; i++)
        {
            if (que == B[i])
            {
                int n = i + 1;
                UnityEngine.Debug.Log("所查找的数据在" + n);

            }
        }

        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Queue查找的时间为:" + ts);


    }

    private void Order()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        B = Q1.ToArray();
        for (int i = 0; i < B.Length - 1; i++)
        {
            for (int j = 0; j < B.Length - i - 1; j++)
            {
                if (B[j] > B[j + 1])
                {
                    int temp = B[j];
                    B[j] = B[j + 1];
                    B[j + 1] = temp;
                }
            }
        }

        UnityEngine.Debug.Log("冒泡排序后的数组");
        Q1.Clear();
        for(int i = 0; i < B.Length; i++)
        {
            Q1.Enqueue(B[i]);

        }
        foreach (int x in Q1)
        {
            UnityEngine.Debug.Log(x);
        }

        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Queue排序的时间为:" + ts);
    }

    

    private void Delete()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        int[] B = new int[10];
        B = Q1.ToArray();
        Q1.Clear();

        for (int i = 0; i < B.Length; i++)
        {
            if (que == B[i])
            {
                int n = i;
                for (int j = 0; j < n; j++)
                {
                    A[j] = B[j];
                }
                for (int k = n; k < B.Length - 1; k++)
                {
                    A[k] = B[k + 1];
                }

            }
        }
        A[9] = 0;
        UnityEngine.Debug.Log("删除后的数组");
        for (int i = 0; i < A.Length; i++)
        {
            Q1.Enqueue(A[i]);

        }
        foreach (int x in Q1)
        {
            UnityEngine.Debug.Log(x);
        }

        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Queue删除的时间为:" + ts);

    }

    private void Insert()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        for (int i = 0; i < A.Length; i++)
        {
            if (que > B[i] && que < B[i + 1])
            {

                int n = i + 1;
                for (int j = 0; j < n; j++)
                {
                    B[j] = A[j];
                }
                B[n] = que;
                for (int k = n + 1; k < B.Length; k++)
                {
                    B[k] = A[k];
                }


            }
        }
        Q1.Clear();
        UnityEngine.Debug.Log("插入后的数组");
        for (int i = 0; i < B.Length; i++)
        {
            Q1.Enqueue(B[i]);

        }
        foreach (int x in Q1)
        {
            UnityEngine.Debug.Log(x);
        }

        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Queue插入的时间为:" + ts);
    }


private void Clear()
    {
        Stopwatch sw = new Stopwatch();
        sw.Start();
        Q1.Clear();
        sw.Stop();
        TimeSpan ts = sw.Elapsed;
        UnityEngine.Debug.Log("Queue重置的时间为:" + ts);
    }


    // Update is called once per frame
    void Update()
    {

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值