C# 改进的选择排序越界错误
我有一个方法是“选择排序的改进版本”。但是,代码不是以“temp[x]=0”的形式运行的;此行给出了数组错误的界限。我不想使用ArrayList。如何将此行更改为数组的边界C# 改进的选择排序越界错误,c#,arrays,sorting,C#,Arrays,Sorting,我有一个方法是“选择排序的改进版本”。但是,代码不是以“temp[x]=0”的形式运行的;此行给出了数组错误的界限。我不想使用ArrayList。如何将此行更改为数组的边界 public static void ImprovedSelectionSort(int[] Array) { Stopwatch timer = new Stopwatch(); timer.Start(); int[] temp = new int[Arra
public static void ImprovedSelectionSort(int[] Array)
{
Stopwatch timer = new Stopwatch();
timer.Start();
int[] temp = new int[Array.Length];
for (int x = 1; x <= Array.Length; x++)
{
//OtherList.Add(0); -- what I want to do
temp[x] = 0;
}
int n = Array.Length;
int i = 0;
while (i < n)
{
int rear = 0;
int curMax = Array[i];
temp[rear] = i;
int j = i + 1;
while (j < n)
{
if (curMax < Array[j])
{
curMax = Array[j];
rear = -1;
}
if (curMax == Array[j])
{
rear = rear + 1;
temp[rear] = j;
}
j++;
}
int front = 0;
int p = Array[temp[front]];
while (front <= rear)
{
int temporary = p;
Array[temp[front]] = Array[i];
Array[i] = temporary;
i++;
front += 1;
}
}
publicstaticvoid-ImprovedSelectionSort(int[]数组)
{
秒表计时器=新秒表();
timer.Start();
int[]temp=newint[Array.Length];
对于(int x=1;x
对于(intx=1;x更改(intx=1;x)的