【问题标题】:Bubble Sort Program冒泡排序程序
【发布时间】:2019-04-21 08:22:20
【问题描述】:

我正在创建一个对数组中的随机整数进行排序的冒泡排序程序。该数组应该能够容纳多达一百万个已排序的整数。当我达到一个很高的数字(例如,250,000)时,程序将坐在那里,从不输出任何东西。代码如下:

using System;

namespace SortingProject
{
    class MainClass
    {
        public static void Main(string[] args)
        {

            //create an array to hold integers
            int[] list = new int[50000];

            //call random function to populate integer values
            Random rand = new Random();

            //add integers to array
            for (int i = 0; i < list.Length; i++) {

                list[i] = rand.Next(1,50000);
            }

            //call bubble sort method and input the array
            BubbleSorting(list);

        }

        //bubble sort method and logic

        public static void BubbleSorting(int[] array)
        {

            //initialize time start 
            DateTime start = DateTime.Now;
            DateTime end;

            end = DateTime.Now;


            //initialize element integer
            int element = 0;

            for (int bubble = 0; bubble < array.Length; bubble++)
            {
                //create for loop to perform bubble sort
                for (int sort = 0; sort < array.Length - 1; sort++)
                {

                    if (array[sort] > array[sort + 1])
                    {
                        element = array[sort + 1];

                        array[sort + 1] = array[sort];

                        array[sort] = element;
                    }
                }

            }

            //loop and print array contents
            for (int i = 0; i < array.Length; i++)

                Console.Write(array[i] + " ");


            //calculate time it takes to sort array
            end = DateTime.Now;
            TimeSpan ts = end.Subtract(start);
            Console.WriteLine(" ");
            Console.WriteLine("Duration = {0} ms", ts.TotalMilliseconds);
        }
    }
    }

我通常会在程序运行时等待一段时间,但它似乎因更大的数组而冻结。任何有关原因的帮助将不胜感激。

【问题讨论】:

  • 您可能只是低估了完成时间字面意思成倍地增加 O(n^2) 吗?
  • 请注意,冒泡排序的运行时间复杂度为 O(n^2)。这意味着,随着您增加输入大小(列表长度),计算需要越来越多的时间来计算答案。您应该看到随着输入的增加,运行时间的超线性增加。您是否尝试过其他一些输入来查看是否可以找到停止工作的长度?
  • 对于数组中的每个元素,对数组中的每个元素做一些事情,想想它是如何缩放的。输入你的计算器,元素的数量,然后点击 x2 按钮,看看它做了什么
  • 你可以放置断点来了解你的代码在哪里花费了所有的时间或者因为这是一个控制台应用程序,你可以添加一个像 if (bubble % 1000 == 0) Console.WriteLine( $"仍在运行:{bubble}");到你的泡沫循环。它没有死,只是需要更多的时间来处理。
  • 为了你以后的参考,我建议你使用Stopwatch类而不是DateTime.Now作为计时码。 DateTime.Now 仅精确到几毫秒,这适用于您运行需要几秒钟、几分钟或几小时的程序的情况。但是请养成使用Stopwatch 来满足您的时间需求的习惯,因为它更精确。

标签: c# arrays sorting bubble-sort


【解决方案1】:

测试了代码,它运行良好(测试了 250000 个值)。正如 cmets 中所指出的,冒泡排序算法不是最优化的算法。其复杂性由下式给出:

for (int bubble = 0; bubble < array.Length; bubble++) 
{
    //create for loop to perform bubble sort
    for (int sort = 0; sort < array.Length - 1; sort++)
    {
       \\do logic
    }
 }

外部 for 循环将执行 N 次循环。内部 for 循环将执行 N 次循环。 大 O 表示法的复杂度为 N*N,因此我们有 O(N^2)。

如果有 250000 个值,将有 62,500,000,000 次迭代。

请记住,复杂性(如果您愿意,所花费的时间)与值 N 的数量成正比,您需要排序的值越多,冒泡排序完成所需的时间就越长。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 2021-04-26
    • 2015-09-12
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多