Okay, so I have a bubble sort that has some issues. It is meant to sort 10 random numbers, and it does....however, in the console I have to press enter about 11 times for it to produce results, what can I do to fix that?? Here's my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Bubble
{
    class Bubble
    {
        // array to hold values6.0, 4.4, 1.9, 2.9, 3.4, 2.9, 3.5 etc - changed back after testing
        private double[] values = new Double[10]; 
            
            // number of elements
            private int elements=10;

            /// <summary>
            /// Bubble Sort Algorithm
            /// </summary>
        public void sortBubble()
            {
                int number;
                int number1;
                double temp;

                for (number = (elements - 1); number >= 0; number--)
                {
                    for (number1 = 1; number1 <= number; number1++)
                    {
                        
                        if (values[number1 - 1] > values[number1])
                        {
                            temp = values[number1 - 1];
                            values[number1 - 1] = values[number1];
                            values[number1] = temp;
                        }
                    }
                }
            }
        public static void Main()
        {   
            // instantiate instance of class
            Bubble myBubble = new Bubble();

          /*  // retrieve elements to store in array
            Console.Write("Number of elements?");
            */
          
           /* // Header
            Console.WriteLine("");
            Console.WriteLine("***************");
            Console.WriteLine(" Enter Number  ");
            Console.WriteLine("***************");
            */
              // Get array elements
           for (int number = 0; number < myBubble.elements; number++)
            {
                System.Random RandomNumber = new System.Random();
                double element = 0; 

                //Generate a random number 
                double randelement = RandomNumber.NextDouble();
                double randomwhole = RandomNumber.Next(10,100);
                element = randomwhole + randelement;

                Console.ReadKey();
                myBubble.values[number] = element;
            }

            // Sort the array
            myBubble.sortBubble();

            // Output sorted array
            Console.WriteLine("");
            Console.WriteLine("****************");
            Console.WriteLine(" Sorted Numbers ");
            Console.WriteLine("****************");

            for (int number1 = 0; number1 < myBubble.elements; number1++)
            {
                Console.WriteLine(myBubble.values[number1]);
            }

            // Here to stop app from closing
            Console.WriteLine("\n\nPress enter to exit.");
            Console.Read();
        }// end Main
    }//end class Bubble
}

Line 65 in your code requires that you press a key for each time the loop runs. Remove it.

Line 65 in your code requires that you press a key for each time the loop runs. Remove it.

I went ahead and removed it, but now it only gives me an output of 1 number 10 times repeated, instead of 10 random numbers sorted.

You place the creation of the Random object inside the loop (line 57). Since the Random function is seeded off the clock, there isn't enough time for the clock to change before a new number is requested, thus you get the same seed for all 10 numbers. Never create Random in a loop. You should only ever create one Random object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.