冒泡排序C#给出一个额外的值

冒泡排序C#给出一个额外的值,c#,bubble-sort,C#,Bubble Sort,我试着做了一个冒泡排序算法,但当我输出按顺序排序的值时,它在开始时会给我一个额外的值,这个值不在我的数组中,但我无法解决如何修复这个问题 例如,当我运行程序时,我的数组是:8、3、68、74、67、82、82、18、48、53 排序后的值显示:60、3、8、18、48、53、67、68、74、82、82 public static void Main(string[] args) { int n = 10; //10 values in array Random r = new

我试着做了一个冒泡排序算法,但当我输出按顺序排序的值时,它在开始时会给我一个额外的值,这个值不在我的数组中,但我无法解决如何修复这个问题

例如,当我运行程序时,我的数组是:8、3、68、74、67、82、82、18、48、53

排序后的值显示:60、3、8、18、48、53、67、68、74、82、82

public static void Main(string[] args)
{
    int n = 10; //10 values in array
    Random r = new Random();

    int[] a; //array
    int temp;
    int i;
    a = new int[n + 1];
    a[0] = 1; //starts at 0

    for (i = 0; i <= n; i++) // set the array up
        a[i] = r.Next(1, 100); // + random numbers 

    for (i = 1; i <= n; i++)
        Console.WriteLine(a[i] + " "); // outputs the numbers of array

    Console.WriteLine();
    Console.ReadLine();

    for (i = 1; i <= n; i++)
    {
        for (int k = 1; k < a.Length - 1; k++) // n - 1 passes
        {
            if (a[k] > a[k + 1])
            {
                temp = a[k + 1]; //stores temporarily
                a[k + 1] = a[k];
                a[k] = temp;
            }
        }
    }

    Console.WriteLine("Array is sorted: ");
    foreach (int number in a) Console.Write(number + " ");
    Console.Read();
}
publicstaticvoidmain(字符串[]args)
{
int n=10;//数组中有10个值
随机r=新随机();
int[]a;//数组
内部温度;
int i;
a=新整数[n+1];
[0]=1;//从0开始
对于(i=0;i创建所有循环:

for(int SOMETHING = 0; ...
不是

最后一次尝试像前面的尝试一样打印出数组(使用从1开始的索引,而不是foreach)


c#中的数组从0开始。根据我的评论,您是从索引1开始填充和排序数组的,但最后一次打印数组时,您使用的foreach将包含第0(第一)个元素,在算法的前面部分中,您没有打印或排序该元素

以下是您的代码的固定版本:

public static void Main(string[] args)
{
    int n = 10; //10 values in array
    Random r = new Random();

    int[] a = new int[n]; //array

    for (int i = 0; i < a.Length; i++) // set the array up
        a[i] = r.Next(1, 100); // + random numbers 

    foreach(int x in a)
        Console.WriteLine(x + " "); // outputs the numbers of array

    Console.WriteLine();
    Console.ReadLine();

    for (int i = 0; i < a.Length - 1; i++) 
    {
        for (int j = 0; j < a.Length - 1; j++) 
        {
            if (a[j] > a[j + 1])
            {
                int temp = a[j + 1]; //stores temporarily
                a[j + 1] = a[j];
                a[j] = temp;
            }
        }
    }

    Console.WriteLine("Array is sorted: ");
    foreach (int number in a) 
      Console.Write(number + " ");
    Console.Read();
}
publicstaticvoidmain(字符串[]args)
{
int n=10;//数组中有10个值
随机r=新随机();
int[]a=新的int[n];//数组
for(int i=0;ia[j+1])
{
int temp=a[j+1];//临时存储
a[j+1]=a[j];
a[j]=温度;
}
}
}
WriteLine(“数组已排序:”);
foreach(a中的整数)
控制台。写入(数字+“”);
Console.Read();
}

以下是发生的情况:

  • 执行此操作时,您正在使用
    11
    元素初始化数组:

    int n = 10;  
    int[] a = new int[n + 1];  // n + 1 is 11, so this creates an 11-element array
    
  • 然后在填充数组时,从
    0
    循环到
    10
    ,填充所有11个索引:

    for (i = 0; i <= n; i++)
    a[i] = r.Next(1, 100);`
    
  • 但在最后,您将输出所有元素,这样您就可以看到排序过程中跳过的第一个数字:

    foreach (int number in a) Console.Write(number + " ");
    

  • 为了解决这个问题,通常在数组中循环时,我们从索引
    0
    开始,当索引变量小于数组长度时循环:

    for (i = 0; i < a.Length; i++)
    
    for(i=0;i

    这将始终确保您对数组中的每个项进行迭代。

    < P>我将考虑一个更为LINQ支持的方法:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace BubbleSort
    {
        class Program
        {
            static readonly Random rnd = new Random();
            static void Swap(IList<int> list, int indexA, int indexB)
            {
                int tmp = list[indexA];
                list[indexA] = list[indexB];
                list[indexB] = tmp;
            }
            static void Main(string[] args)
            {
                var array = Enumerable.Range(1, 10).Select(r => rnd.Next(1,100)).ToArray();
    
                Console.WriteLine("Unsorted array:" + String.Join(" ", array.Select(x => x.ToString())));
    
                // array.Sort(); // probably not allowed
                for (int i = 0; i < array.Count() - 1; ++i)
                {
                    for (int j = 0; j < array.Count() - 1; ++j)
                    {
                        if (array[j] > array[j + 1]) Swap(array, j, j + 1);
                    }
                }
    
                Console.WriteLine("Sorted array:" + String.Join(" ", array.Select(x => x.ToString())));
            }
        }
    }
    

    设置断点并逐步查看代码以了解发生了什么。此代码不会输出数组的第一个元素。可以肯定的是,您的60是一个随机生成的数字,始终存在于数组中,但您没有看到它,因为[i]其中i=1是数组中的第二项:
    for(i=1;我建议您放弃
    n
    ,只需继续
    array.Length
    ,并习惯从index
    [0]
    开始的数组,然后运行到
    Length-1
    您可以在所有步骤中处理从索引1开始的数组,只有一个步骤除外,在其中使用随机数据填充数组。因此,
    a[0]=1;//从0开始
    用随机值重写,然后开始忽略第0个元素。似乎您希望通过忽略第一个元素来假装数组从1开始,但在填充数组时也忽略它,并且不要使用
    foreach
    打印出来。如果您计划使用调试器,您应该真正学会使用调试器真正学习编程。这样你可以更好地理解你在做什么。看起来OP知道这一点,并且在所有步骤中都会忽略第0个元素。@Gserg但随后他抱怨“为什么数组的开头有一个额外的数字”当他
    foreach
    在最后打印出数组时。但我实际上不想从索引0开始,我希望索引0表示为1,所以我的数字是1-10我不知道你所说的“所以我的数字是1-10”是什么意思.但是在这种情况下,永远不要使用索引
    0
    。但是如果你想用c语言编程,这是一个非常坏的习惯。数组是基于
    0
    ,最大索引是
    Length-1
    。一旦你习惯了它,你会觉得很自然。
    for (i = 0; i < a.Length; i++)
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    namespace BubbleSort
    {
        class Program
        {
            static readonly Random rnd = new Random();
            static void Swap(IList<int> list, int indexA, int indexB)
            {
                int tmp = list[indexA];
                list[indexA] = list[indexB];
                list[indexB] = tmp;
            }
            static void Main(string[] args)
            {
                var array = Enumerable.Range(1, 10).Select(r => rnd.Next(1,100)).ToArray();
    
                Console.WriteLine("Unsorted array:" + String.Join(" ", array.Select(x => x.ToString())));
    
                // array.Sort(); // probably not allowed
                for (int i = 0; i < array.Count() - 1; ++i)
                {
                    for (int j = 0; j < array.Count() - 1; ++j)
                    {
                        if (array[j] > array[j + 1]) Swap(array, j, j + 1);
                    }
                }
    
                Console.WriteLine("Sorted array:" + String.Join(" ", array.Select(x => x.ToString())));
            }
        }
    }
    
    var swapped = true;
    while (swapped)
    {
        swapped = false;
        for (int i = 0; i < array.Count() - 1; ++i)
        {
            if (array[i] > array[i + 1])
            {
                Swap(array, i, i + 1);
                swapped = true;
            }
        }
    }