using System;
using System.Threading;
namespace Array_Sorting
{
internal static class Program
{
public static void Main()
{
// define variables
int i, j, temp, tblsize, rmin, rmax;
int[] mytable;
Random myrandom = new Random();
// User has to define the Array size
Console.Clear(); //clears to make it better looking
Console.WriteLine("\n----- Array Sorting App -----\n");
Console.Write("Please insert the size of the table: ");
tblsize = Convert.ToInt32(Console.ReadLine());
Console.Write("\n");
if (tblsize <= 0) //Check: table size must be greater than zero
{
Console.Clear(); //clears to make it better looking
Console.Write("Table size must be greater than zero\n ");
Thread.Sleep(3000); //delay 3000 milliseconds
Environment.Exit(0);
}
mytable = new int[tblsize];// initialize array with specific size
// User has to define min and max values for RANDOM command
Console.Write("Please insert the MIN random value: ");
rmin = Convert.ToInt32(Console.ReadLine());
Console.Write("\n");
Console.Write("Please insert the MAX random value: ");
rmax = Convert.ToInt32(Console.ReadLine());
Console.Write("\n");
if (rmin > rmax) //Check: min value must be less than max value
{
Console.Clear(); //clears to make it better looking
Console.Write("MIN value must be less than MAX value\n ");
Thread.Sleep(3000); //delay 3000 milliseconds
Environment.Exit(0);
}
Console.Clear();//clears to make it better looking
for (i = 0; i < mytable.Length; i++)// Loop to fill the Array with RANDOM values, between min and max limits
{
mytable[i] = myrandom.Next(rmin, rmax);
}
Console.WriteLine("\n----- Table export -----");
for (i = 0; i < mytable.Length; i++)// Loop to export full Array
{
Console.WriteLine(" [{0}] -> {1}", i, mytable[i]);
}
Console.WriteLine("\n----- Table export (alternate) -----");
for (i = 0; i < mytable.Length; i+=2)// Loop to export Array in alternate mode
{
Console.WriteLine(" [{0}] -> {1}", i, mytable[i]);
}
//Loop to Shuffle the Array
for (i = 0; i < mytable.Length - 1; i++)
{
j = myrandom.Next(i, mytable.Length);
temp = mytable [i];
mytable [i] = mytable [j];
mytable [j] = temp;
}
Console.WriteLine("\n----- Suffled Table export -----");
for (i = 0; i < mytable.Length; i++)// Loop to export suffled Array
{
Console.WriteLine(" [{0}] -> {1}", i, mytable[i]);
}
//Loop (with inner loop) to Sort the Array, using bubble sort method
for (j = 0; j <= mytable.Length - 2; j++)
{
for (i = 0; i <= mytable.Length - 2; i++)
{
if (mytable[i] > mytable[i + 1])
{
temp = mytable[i + 1];
mytable[i + 1] = mytable[i];
mytable[i] = temp;
}
}
}
Console.WriteLine("\n----- Sorted Table export -----");
for (i = 0; i < mytable.Length; i++)// Loop to export sorted Array
{
Console.WriteLine(" [{0}] -> {1}", i, mytable[i]);
}
}
}
}
Add a code snippet to your website: www.paste.org