using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.Diagnostics;
public class Array : MonoBehaviour
{
int[] A = new int[100];
int[] B = new int[100];
public int ArrNum;
void Start()
{
UnityEngine.Debug.Log("初始数组");
for (int i = 0; i < A.Length; i++)
{
A[i] = i + 1;
UnityEngine.Debug.Log(A[i]);
}
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;
}
UnityEngine.Debug.Log("打乱数组");
for (int i = 0; i < A.Length; i++)
{
UnityEngine.Debug.Log(A[i]);
}
FindValue();
Order();
Delete();
Insert();
Clear();
}
private void FindValue()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < A.Length; i++)
{
if (ArrNum == A[i])
{
int n = i + 1;
UnityEngine.Debug.Log("所查找的数据在" + n);
}
}
sw.Stop();
TimeSpan ts = sw.Elapsed;
UnityEngine.Debug.Log("Array查找的时间为:" + ts);
}
private void Order()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < A.Length - 1; i++)
{
for (int j = 0; j < A.Length - i - 1; j++)
{
if (A[j] > A[j + 1])
{
int temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
UnityEngine.Debug.Log("冒泡排序后的数组");
for (int i = 0; i < A.Length; i++)
UnityEngine.Debug.Log(A[i]);
sw.Stop();
TimeSpan ts = sw.Elapsed;
UnityEngine.Debug.Log("Array冒泡排序的时间为:" + ts);
}
private void Delete()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < A.Length; i++)
{
if (ArrNum == A[i])
{
int n = i;
for (int j = 0; j < n; j++)
{
B[j] = A[j];
}
for (int k = n; k < A.Length-1; k++)
{
B[k] = A[k + 1];
}
}
}
UnityEngine.Debug.Log("删除后的数组");
for (int i = 0; i < B.Length; i++)
UnityEngine.Debug.Log(B[i]);
sw.Stop();
TimeSpan ts = sw.Elapsed;
UnityEngine.Debug.Log("Array删除的时间为:" + ts);
}
private void Insert()
{
Stopwatch sw = new Stopwatch();
sw.Start();
for (int i = 0; i < A.Length; i++)
{
if (ArrNum > B[i]&&ArrNum