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()
{
}
}
unity C#Queue增删改查并记录时间
于 2022-03-24 09:18:33 首次发布