public static void bubbleSortRange(ref int[] x)
int upperBound = x.Length - 1;
while (lowerBound <= upperBound)
for (int i = lowerBound; i < upperBound; i++)
lowerBound = firstExchange - 1;
upperBound = lastExchange;
public static void DisplayElements(ref int[] xArray, char status, string sortname)
Console.WriteLine("After sorting using algorithm: " + sortname);
Console.WriteLine("Before sorting");
for (int i = 0; i <= xArray.Length - 1; i++)
if ((i != 0) && (i % 10 == 0))
Console.Write(xArray[i] + " ");
static void MixDataUp(ref int[] x, Random rdn)
for (int i = 0; i <= x.Length - 1; i++)
x[i] = (int)(rdn.NextDouble() * x.Length);
public static void Main()
Console.WriteLine("Chapter 05 - Sorting Algorithms Demo\n\n");
Random rdn = new Random(nItems);
int[] xdata = new int[nItems];
MixDataUp(ref xdata, rdn);
DisplayElements(ref xdata, 'b', "");
bubbleSortRange(ref xdata);
DisplayElements(ref xdata, 'a', "bubbleSortRange");
Console.WriteLine("\n\n");