System.IndexAutoFrangeException:';索引超出了数组的界限;是什么意思如何在C#中进行冒泡排序?

System.IndexAutoFrangeException:';索引超出了数组的界限;是什么意思如何在C#中进行冒泡排序?,c#,loops,sorting,iteration,bubble-sort,C#,Loops,Sorting,Iteration,Bubble Sort,我正在为我的课程做一个编程任务,我被要求用C语言来完成。当我运行它时,它突出显示数字[position+1],并给出错误:System.IndexOutOfRangeException:“索引超出了数组的界限。” using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp1 {

我正在为我的课程做一个编程任务,我被要求用C语言来完成。当我运行它时,它突出显示数字[position+1],并给出错误:System.IndexOutOfRangeException:“索引超出了数组的界限。”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {

            // array of numbers
            int[] numbers = { 9, 5, 4, 15, 3, 8, 11, 2 };

            int noOfNumbers = 8;

            int temp;

            while (noOfNumbers > 1) {

                foreach(int position in numbers) {

                    if (numbers[position] > numbers[position+1])
                    {

                        temp = numbers[position];

                        numbers[position] = numbers[position + 1];

                        numbers[position + 1] = temp;

                    }

                }
                noOfNumbers = noOfNumbers + 1;

            }

            Console.WriteLine(numbers);


        }
    }
}
以下是伪代码:

BEGIN
Numbers=[9,5,4,15,3,8,11,2]
AmountOfNumbers=8
Temp=0
WHILE AmountOfNumbers>1
    FOR each itemPositionInArray in Numbers
        IF Numbers[ItemPositionInArray] > Numbers[ItemPositionInArray+1]
            Temp=Numbers[ItemPositionInArray]
            Numbers[ItemPositionInArray]=Numbers[ItemPositionInArray+1]
            Numbers[ItemPositionInArry+1]=Temp
    AmountOfNumbers=AmountOfNumbers-1
OUTPUT(Numbers)
END

你代码中的问题就在这里:(就像艾米说的)

您正在使用位置作为数组的索引。foreach将枚举数组,并将元素值放入
位置
变量,而不是索引。因此,第一个迭代位置将包含9(不是索引0)

为此,应使用“正常”For循环

for(int i=0; i < count; i++) { }
for(inti=0;i

下面是一个做泡泡运动的例子。我写了一些评论来解释它的作用

using System;

public class Program
{
    public static void Main()
    {
        // array of numbers
        int[] numbers = { 9, 5, 4, 15, 3, 8, 11, 2 };

        // declare a boolean.
        bool done;

        do
        {
            // when nothing happens, you're done.
            done = true;

            // create a for-loop to iterate all item-1 (except the last)
            for(int position=0;position<numbers.Length-1;position++)
            {
                // compare the values in the array
                if (numbers[position] > numbers[position+1])
                {
                    // swap the values
                    int temp = numbers[position];

                    numbers[position] = numbers[position + 1];

                    numbers[position + 1] = temp;

                    // if something was swapped, you're not ready.
                    done = false;
                }
            }

            // loop until done is set.
        } while (!done);

        Console.WriteLine(string.Join(",", numbers));
    }
}
使用系统;
公共课程
{
公共静态void Main()
{
//数列
int[]数字={9,5,4,15,3,8,11,2};
//声明一个布尔值。
布尔多;
做
{
//如果什么都没发生,你就完了。
完成=正确;
//创建for循环以迭代所有项1(最后一项除外)
对于(整数位置=0;位置编号[位置+1])
{
//交换值
int temp=数字[位置];
数字[位置]=数字[位置+1];
数字[位置+1]=温度;
//如果有东西被交换了,你还没准备好。
完成=错误;
}
}
//循环直到设置完成。
}而(!完成);
Console.WriteLine(string.Join(“,”,number));
}
}

foreach(整数位置){
这不会在它们的位置上循环,而是在数字本身上循环。循环的第一次迭代中的值
position
将是9,而不是0。副本解释了错误的含义。然后,如果您开始使用调试器,您将看到第一个位置等于9,当然,这超出了范围e当您将其用作数字数组的索引时,请将其更改为(int position=0;position对于bubblesort,您应该使用布尔值检查是否有更改。在
if(numbers[position]>numbers[position+1]中
,如果不交换任何内容,您将ready@Steve-可能重新打开以帮助OP解决所有问题,而不仅仅是手头的问题?
using System;

public class Program
{
    public static void Main()
    {
        // array of numbers
        int[] numbers = { 9, 5, 4, 15, 3, 8, 11, 2 };

        // declare a boolean.
        bool done;

        do
        {
            // when nothing happens, you're done.
            done = true;

            // create a for-loop to iterate all item-1 (except the last)
            for(int position=0;position<numbers.Length-1;position++)
            {
                // compare the values in the array
                if (numbers[position] > numbers[position+1])
                {
                    // swap the values
                    int temp = numbers[position];

                    numbers[position] = numbers[position + 1];

                    numbers[position + 1] = temp;

                    // if something was swapped, you're not ready.
                    done = false;
                }
            }

            // loop until done is set.
        } while (!done);

        Console.WriteLine(string.Join(",", numbers));
    }
}