Bubble sort



  • All good days of the day, I need your help, and at least I have a bubble algorithm, and I've been doing it. Share the code and your experience, thank you very much in advance.

    namespace Bubble_sort
    {
        class Program
        {
            static int[] Sort(int[] arr)
            {
                for (int i = 0; i < arr.Length - 1; i++) // O(n2)
                {
                    for (int j = 0; j < arr.Length - i - 1; j++) 
                    {
                        if (arr[j] > arr[j + 1])
                        {
                            int buf = arr[j]; 
    
                        arr[j] = arr[j + 1];
    
                        arr[j + 1] = buf;
                    }
                }
            }
            return arr;
        }
    
        static void Main(string[] args)
    
        {
            int x = 1; // O(1)
            int[] array = new int[] { 5, 2, 3, 4, 1 };
    
            array = Sort(array);
    
            for (int i = 0; i &lt; array.Length; i++) 
            {
                Console.WriteLine(array[i]);
            }
            Console.ReadKey();
        }
    }
    

    }



  • ♪ /questions/tagged/.net There is a special class to calculate the time of implementation - https://msdn.microsoft.com/ru-ru/library/system.diagnostics.stopwatch(v=vs.110).aspx

    var stopwatch = Stopwatch.StartNew();
    ...//ваш код
    stopwatch.Stop();
    MessageBox.Show(string.Format("{0} миллисекунд затрачено", stopwatch.ElapsedMilliseconds));
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2