Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么不是';我的冒泡排序不能正确排序数组吗?_C#_Arrays_String_Sorting - Fatal编程技术网

C# 为什么不是';我的冒泡排序不能正确排序数组吗?

C# 为什么不是';我的冒泡排序不能正确排序数组吗?,c#,arrays,string,sorting,C#,Arrays,String,Sorting,所以我有这个气泡排序,第一次尝试创建一个,这就是我所拥有的。 出于某种原因,它以一种奇怪的方式打印出数组。 据我所知,它应该按字母分类 如何在不使用LINQ或Array.sort()的情况下正确执行气泡排序;这是给学校的,所以我需要做冒泡排序算法 这是它打印出来的图像 class Program { static string[] animals = new string[] { "cat", "elephant", "tiger", "fish", "dolphin",

所以我有这个气泡排序,第一次尝试创建一个,这就是我所拥有的。 出于某种原因,它以一种奇怪的方式打印出数组。 据我所知,它应该按字母分类

如何在不使用LINQ或Array.sort()的情况下正确执行气泡排序;这是给学校的,所以我需要做冒泡排序算法

这是它打印出来的图像

class Program
    {
        static string[] animals = new string[] { "cat", "elephant", "tiger", "fish", "dolphin", "giraffe", "hippo", "lion", "rat", "string ray" };

        static void Main(string[] args)
        {
            BubbleSort();
            Console.ReadLine();
        }

        private static void BubbleSort()
        {
            bool swap;
            string temp;

            string[] animals = new string[] { "cat", "elephant", "tiger", "fish", "dolphin", "giraffe", "hippo", "lion", "rat", "string ray" };

            for (int index = 0; index < (animals.Length - 1); index++)
            {
                if (string.Compare(animals[index], animals[index + 1], true) < 0) //if first number is greater then second then swap
                {
                    //swap
                    temp = animals[index];
                    animals[index] = animals[index + 1];
                    animals[index + 1] = temp;
                    swap = true;
                }
            }

            foreach (string item in animals)
            {
                Console.WriteLine(item);
            }
        }
    }

类程序
{
静态字符串[]动物=新字符串[]{“猫”、“大象”、“老虎”、“鱼”、“海豚”、“长颈鹿”、“河马”、“狮子”、“老鼠”、“线鳐”};
静态void Main(字符串[]参数)
{
泡泡运动();
Console.ReadLine();
}
私有静态void BubbleSort()
{
布尔交换;
字符串温度;
字符串[]动物=新字符串[]{“猫”、“大象”、“老虎”、“鱼”、“海豚”、“长颈鹿”、“河马”、“狮子”、“老鼠”、“线鳐”};
对于(int索引=0;索引<(anies.Length-1);索引++)
{
if(string.Compare(动物[index],动物[index+1],true)<0)//如果第一个数字大于第二个,则交换
{
//交换
温度=动物[指数];
动物[指数]=动物[指数+1];
动物[指数+1]=体温;
swap=true;
}
}
foreach(动物中的字符串项)
{
控制台写入线(项目);
}
}
}

对于Bubblesort,您需要两个嵌套循环,因为您要传递数组不是一次而是多次

private static void BubbleSort()
    {
        string temp;

        string[] animals = new string[] { "cat", "elephant", "tiger", "fish", "dolphin", "giraffe", "hippo", "lion", "rat", "string ray" };

        for (int i = 1; i < animals.Length; i++)
        {
            for (int j = 0; j < animals.Length - i; j++)
            {
                if (string.Compare(animals[j], animals[j + 1], StringComparison.Ordinal) <= 0) continue;

                temp = animals[j];
                animals[j] = animals[j + 1];
                animals[j + 1] = temp;
            }
        }

        foreach (string item in animals)
        {
            Console.WriteLine(item);
        }
    }
private静态void BubbleSort()
{
字符串温度;
字符串[]动物=新字符串[]{“猫”、“大象”、“老虎”、“鱼”、“海豚”、“长颈鹿”、“河马”、“狮子”、“老鼠”、“线鳐”};
for(int i=1;i如果(string.Compare(animals[j]、animals[j+1]、StringComparison.Ordinal)只将单个元素冒泡到其在数组中的正确位置(假设您希望在末尾具有最小值,否则,请反转比较运算符)-冒泡排序需要多次通过数组。我将CompareTo<切换到CompareTo>,并将其全部添加到foreach循环中。现在,它打印出除第二个值以外的所有rght。正如@Croemhold所说:“使用搜索时间再长一点”:)祝您好运