Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,100 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,649 people online right now. Registration is fast and FREE... Join Now!




Please Advise: Bubble Sort array program

 
Reply to this topicStart new topic

Please Advise: Bubble Sort array program

CrazyJ
30 Nov, 2007 - 09:12 AM
Post #1

D.I.C Head
**

Joined: 15 Oct, 2007
Posts: 51


My Contributions
I have written this simple program to sort an array using the Buble sort algorithm, but for some reason (and I feel it is painfully obvious) it is not sorting my list?
CODE

namespace BubbleSort
{
    class Program
    {
        static void Main(string[] args)
        {
            // Instantiate an instance of the class
            arraySort mySort = new arraySort();


            int n;
            int min;
            int max;

            Random random = new Random();

            Console.WriteLine("Enter the size of the array: ");
            n = MyMethods.GetInteger();

            int[] myArray = new int[n];
//          ===========================

            Console.WriteLine("Please enter a minimum number: ");
            min = MyMethods.GetInteger();
            Console.WriteLine();

            Console.WriteLine("Please enter a maximum number: ");
            max = MyMethods.GetInteger();

            arraySort.RandomNumber(min, max, random);


            arraySort.FillArray(n, myArray, min, max);

            arraySort.SortArray(ref myArray);

            arraySort.PrintArray(n, myArray);

        
            
                // Here to stop app from closing
                Console.WriteLine("\n\nPress Return to exit.");
                Console.Read();

            }


methods...
CODE

class MyMethods
    {
//      ===============================
        public static int GetInteger( )
        {
            string integerString;

            integerString = Console.ReadLine();
            return int.Parse(integerString);

        } // method GetnIeger
//      =====================


Herein lies the problem, in my SortArray method...
CODE

class arraySort
    {
        

        // Generate random numbers
        public static int RandomNumber( int        min,
                                        int        max,
                                        Random randomNbObject )
        {

            return randomNbObject.Next(min, max);
        }

//      ================================



//      =============================================
        public static void FillArray(int n, int[] myArray, int min, int max)
        {
            int ii;
            int jj;
//          ===============================================================
            Random randomNumber = new Random(); // randomNumber declaration
//          ===============================================================
            for (ii = 0; ii < n; ii++)
                for (jj = 0; jj < n; jj++)
                    myArray[ii] = randomNumber.Next(min, max);
        }
//     ==========================================================


        // Bubble Sort Algorithm
        public static void SortArray(ref int [] myArray)
        {
            int n = 1;
            int ii;
            int jj;
            int temp;

            for (ii = (n - 1); ii >= 0; ii--)
            {
                for (jj = 1; jj <= ii; jj++)
                {
                    if (myArray[jj - 1] > myArray[jj])
                    {
                        temp = myArray[jj - 1];
                        myArray[jj - 1] = myArray[jj];
                        myArray[jj] = temp;
                    }
                }
            }
        }// method sortArray
//      ====================
        public static void PrintArray(int n, int[] myArray)
        {
            int ii;
            
            int maxNbSpaces = 0;
            string prettyPrint;
            string formatString;
            for (ii = 0; ii < n; ii++)
            {


                prettyPrint = Convert.ToString(myArray[ii]);

                if (prettyPrint.Length > maxNbSpaces)
                    maxNbSpaces = prettyPrint.Length;
            }
//          =========================================

            maxNbSpaces++;
            formatString = "{0," + maxNbSpaces + "}";
            //          
            for (ii = 0; ii < n; ii++)
            {
                
                Console.Write(formatString, myArray[ii]);
                Console.WriteLine();

            }

        }// method PrintArray
//  =========================


    }// class arraySort
//  ===================


Thanks!

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Please Advise: Bubble Sort Array Program
30 Nov, 2007 - 11:51 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,198



Thanked: 213 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
First of all, your fillArray is working extra hard. That nested loop is not needed. Just load the random numbers straight in.

CODE

public static void FillArray(int n, int[] myArray, int min, int max)
{
     int ii;
     int jj;

     Random randomNumber = new Random(); // randomNumber declaration

     // Load the array straight with the random number. Nested would be for multi-dimensional arrays.
     for (ii = 0; ii < n; ii++)
     {
          myArray[ii] = randomNumber.Next(min, max);
     }
}


Now after that your sort was a bit mixed up. I tried to keep your bubblesort style, even though I use a different variation of it. Here is your fixed bubblesort.

CODE

// Bubble Sort Algorithm
public static void SortArray(ref int[] myArray)
{
     int ii;
     int jj;
     int temp;

     // From element 0 to 1 less than the length of the array (moving upwards)
     for (ii = 0; ii < myArray.Length; ii++)
     {

          // From one less than the length, down to where ii is at (moving downwards)
          for (jj = myArray.Length - 1; jj > ii; jj--)
          {

               // Swap is good
               if (myArray[jj - 1] > myArray[jj])
               {
                    temp = myArray[jj - 1];
                    myArray[jj - 1] = myArray[jj];
                    myArray[jj] = temp;
               }
          }
     }
}


So after you implement these two changes, you will have a leaner and meaner program that loads random numbers faster and sorts them.

Enjoy!

"At DIC we be number loading and sorting code ninjas!" decap.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:49PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month