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
}