气泡排序问题

时间:2018-11-18 23:53:06

标签: c# arrays bubble-sort

我目前正在编写代码以通过冒泡排序对数组进行排序。我之前已经使用我创建的set数组编写了它,并且效果很好。但是,我试图将其更改为随机生成的数组,但现在已损坏。想知道是否有人可以帮助我。预先感谢。

using System;

namespace BubbleSot_Fin
{
    class Program
    {
        static void Main(string[] args)
        {
            int N = 5;
            int m = 100;
            int i = 1; //n = number of values m = max value in array 
            Random Rand = new Random();


            int[] array = new int[N + 1];

            for ( i = 1; i <= N; i++)
            {
                array[i] = Rand.Next(1, m);      //Randomise the array

            Console.WriteLine("This is the unsorted array");
            Console.WriteLine("");

            for (i = 0; i < array.Length; i++)
            {
                Console.WriteLine("A[" + i + "] = " + array[i] ); // shows the unsorted numbers
            }




            int[] Bubble = BubbleSort(array);
            Console.WriteLine("");
            Console.WriteLine("Array after Bubble Sort");
            Console.WriteLine("");
            for ( i = 0; i < Bubble.Length; i++)
            {
                Console.WriteLine("A[" + i + "] = " + Bubble[i]); // shows the numbers after sorting
            }
            Console.ReadLine();
        }
        private static int[] BubbleSort(int[] BSArray)
        {
            int length = array.Length;
            for ( i = 0; i < length - 1; i++)    
            {
                for (int j = 0; j < length - 1 - i; j++)
                {
                    if (array[j] > array[j + 1])  
                    {
                        int number = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = number;
                    }
                }
            }
            return BSArray;
        }
    }
}

我遇到的错误是气泡排序的私有和静态单词不正确。

错误为: -CS0106修饰语“私人”对此商品无效 -CS0106修饰符“静态”对此物品无效

0 个答案:

没有答案