我在学习数据结构时刚刚写了一个关于数组旋转的代码。我需要知道如何通过测量时间和空间复杂度来改进下面的程序。
阵列旋转程序。将数组旋转 2 将使数组
1,2,3,4 输入
3,4,1,2 输出
public class Program
{
public static void Main(string[] args)
{
int arrayCount = 0;
int rotate = 2;
int []answer = new int[4];
for (int i = 0; i < answer.Length; i++)
{
answer[i] = Convert.ToInt32(Console.ReadLine());
}
arrayCount = answer.Count();
ArrayRotation.displayRotatedArray(answer, rotate, arrayCount);
ArrayRotation.printArray(answer, arrayCount);
Console.ReadKey();
}
}
public static class ArrayRotation
{
public static void displayRotatedArray(int []temp, int rotate, int count)
{
int c = rotate;
int d = rotate;
int[] firstOccurenceArray = new int[rotate];
for (int g = 0; g < rotate; g++)
{
int num = g;
firstOccurenceArray[g] = temp[g];
}
for (int i = 0; i < temp.Length - c; i++)
{
temp[i] = temp[rotate];
rotate++;
}
for (int k = 1; k < d + 1; k++)
{
temp[count - k] = firstOccurenceArray[c - 1];
c--;
}
}
/* utility function to print an array */
public static void printArray(int[] temp, int size)
{
for (int i = 0; i < size; i++)
Console.Write( temp[i] + " ");
}
}
慕田峪9158850
相关分类