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
}

Recommended Answers

All 5 Replies

Line 28 generates a compiler error and I'm not sure why you even have it there in the first place. Other than that I don't see obvious errors.

Line 28 generates a compiler error and I'm not sure why you even have it there in the first place. Other than that I don't see obvious errors.

I deleted that line, but I still have errors starting on line 32 (cannot implicitly convert type 'double' to 'int'. An explicit conversion exists (are you missing a cast?)

I've researched online about what that error means, and all I've come up with is I either need to cast to a double or an int. I'm not quite sure where to go with it.

indexes into arrays must be of integer type. You've (for some reason) declared your loop variables to be of type double. The compiler wants to convert them to ints, but since the range of the Int32 type is smaller than the range of the Double type it won't do it without explicit instructions to do so. This is because you might lose value. For example, a Double will hold 6 billion easily. An Int32 will not. What happens if you tell it to put the Double into the Int32 anyway?

Change your loop variables to be of type int (number, number1), get rid of the variable 'one' and use the actual number where the compiler complains about not having a variable 'one' :)

Sorry I didn't check your types before, was just looking at the logic.

BTW, next time you post that you have issues, error messages really help us to solve the problems.

indexes into arrays must be of integer type. You've (for some reason) declared your loop variables to be of type double. The compiler wants to convert them to ints, but since the range of the Int32 type is smaller than the range of the Double type it won't do it without explicit instructions to do so. This is because you might lose value. For example, a Double will hold 6 billion easily. An Int32 will not. What happens if you tell it to put the Double into the Int32 anyway?

Change your loop variables to be of type int (number, number1), get rid of the variable 'one' and use the actual number where the compiler complains about not having a variable 'one' :)

Sorry I didn't check your types before, was just looking at the logic.

BTW, next time you post that you have issues, error messages really help us to solve the problems.

Thank you very much for your help! It works the way it should now. I'll make sure to be more specific next time in my posting.

Don't worry too much about it, we get a lot of "This doesn't work" type posts and it just helps if people say *how* it doesn't work :)

commented: Nice help! +15
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.