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!