using System; using System.Linq; namespace Bubble_Sort { class Program { static int[] unorganised_numbers = new int[20]; // initialise an array of size 20 static void Main(string[] args) { // making an array full of random numbers for (int a = 0; a < 20; a++) // length of 20 { Random rnd = new Random(); // generate a bunch of numbers int num = rnd.Next(10); // get a number below 10 unorganised_numbers[a] = num; // put that number into the array } // ui because im bored, smart and finished way too early Console.WriteLine("###########################################################"); Console.WriteLine("#---------------------------------------------------------#"); Console.WriteLine("#-------------------------BUBBLE--------------------------#"); Console.WriteLine("#---------------------------------------------------------#"); // display numbers before sorting Console.Write("#---"); Console.Write(" Unsorted: "); for (int b = 0; b <= unorganised_numbers.Length - 1; b++) // itterate through all values in the array { Console.Write("{0} ", unorganised_numbers[b]); // and then print them all to the console. } Console.Write("---#"); Console.WriteLine(); Sort(unorganised_numbers); // call procedure Console.WriteLine(); Console.WriteLine("#---------------------------------------------------------#"); Console.WriteLine("#--------------------------SORT---------------------------#"); Console.WriteLine("#---------------------------------------------------------#"); Console.WriteLine("###########################################################"); } static void Sort(int[] list) { bool Swap_Active = true; // set boolean swap_active to true while (Swap_Active == true) // while this is true (until all the values are sorted { Swap_Active = false; // its false now for (int i = 0; i <= list.Length - 1; i++) // itterate values in array { if (i == list.Length-1) // dont check any out of bounds { continue; // do NOT check any out of bounds } else if (list[i] > list[i + 1]) // if a value is bigger than the one next { int temp = list[i]; // store it in a temporary variable list[i] = list[i + 1]; // set its value as the next value list[i + 1] = temp; // give the next value its old value Swap_Active = true; // swap_active is still true } } } Console.Write("#---"); Console.Write(" Sorted: "); for (int j = 0; j <= list.Length-1; j++) // itterate through all values in the array { Console.Write("{0} ", list[j]); // and then print them all to the console. } Console.Write("---#"); } } }