I'm trying to make a bubble sort to sort numbers like 3.2, 5.8, etc(double / float numbers). I have this code so far, but I'm still trying to learn and don't know why my code isn't working the way I think it should. Any help is appreciated.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bubble
{
class Bubble
{
// array to hold values
private double[] values = new Double[10];
// number of elements
private double elements;
// Bubble Sort Algorithm
public void sortBubble()
{
double number;
double number1;
double temp;
double one = 1.0;
for (number = (elements - 1); number >= 0; number--)
{
for (number1 = 1; number1 <= number; number1++)
{
number = Convert.ToDouble;
if (values[number1 - one] > 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?");
string elementNum =Console.ReadLine();
myBubble.elements = Int32.Parse(elementNum);
// Header
Console.WriteLine("");
Console.WriteLine("***********************");
Console.WriteLine(" Enter Number ");
Console.WriteLine("***********************");
// Get array elements
for (int number = 0; number < myBubble.elements; number++)
{
Console.Write("<{0}> ", number + 1);
string s1 = Console.ReadLine();
myBubble.values[number] = Double.Parse(s1);
}
// 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
}