Bubble Sort C#

Bubble Sort using C#


A Bubble Sort is an algorithm used to sort thru a list by comparison of data adjacent to itself then sorting if necessary. This process continues untill the entire list is sorted.


*Featured Code Created By: Shane Overby


C#


/*
    Original by me, Shane Overby
    
    This is my own implementation of the Bubble Sort algorithm
    
    You can download this code for LINQPad, here: http://share.linqpad.net/c5rmaq.linq
    
    Input: None
    
    Output: 1.) an unsorted list of random numbers
            2.) an unsorted control list
            3.) the same list of random numbers, sorted
            4.) control list, sorted using <List>.Sort()
    
    Instructions:
    
        1.) Run the program
        2.) Verify that the output is valid
        
    I hope you have found something helpful and/or useful in this code and, as always, Happy Coding!
    
    SPECIAL NOTE:
        
        The <List>.Sort() method implements multiple sorting algorithms and executes them based on conditions of the List object calling the method
        
        Conditions for sort-type selection:
        
            -- If the partition size is fewer than 16 elements, it uses an insertion sort algorithm
            
            -- If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm
            
            -- Otherwise, it uses a Quicksort algorithm
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace SoloLearn
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> nums = new List<int>();
            List<int> control;
            Random rdm = new Random();
            bool sorted = false;
            
            for (int i = 0; i <= 27; i++)
                nums.Add(rdm.Next(1, 28));
                
            control = nums;
            
            Console.WriteLine("Unsorted list, to be sorted:");
            
            for(int i = 0; i < nums.Count; i++)
                Console.Write($"{nums[i]}{(i == nums.Count - 1 ? "\n\n" : ", ")}");
                
            Console.WriteLine("Unsorted control list:");
            
            for(int i = 0; i < control.Count; i++)
                Console.Write($"{control[i]}{(i == control.Count - 1 ? "\n\n" : ", ")}");
            
            //Sorting list using my implementation of the Bubble Sort algorithm
            while (!sorted)
            {
                for (int i = 1; i < nums.Count; i++)
                    if (nums[i - 1] > nums[i])
                    {
                        int x = nums[i];
                        nums[i] = nums[i - 1];
                        nums[i - 1] = x;
                    }
                    
                sorted = true;
                
                for (int i = 1; i < nums.Count; i++)
                    if (nums[i - 1] > nums[i])
                    {
                        sorted = false;
                        break;
                    }
            }
            
            Console.WriteLine("List sorted, using my implementation of the bubble sort:");
            
            for (int i = 0; i < nums.Count; i++)
                Console.Write($"{nums[i]}{(i == nums.Count - 1 ? string.Empty : ", ")}");
            
            //Sorting control list
            control.Sort();
                
            Console.WriteLine("\n\nControl list, sorted using <List>.Sort():");
            
            for(int i = 0; i < control.Count; i++)
                Console.Write($"{control[i]}{(i == control.Count - 1 ? "\n\n" : ", ")}");
        }
    }
}




Share with Friends


Twitter Facebook Google LinkedIn Email Pinterest Reddit StumbleUpon Tumblr




Thank you and have a Blessed Day




Try these Fun Games by Bobbie:


Ocean Treasures Game


Travel Blast Game New York


Play and Learn Russian


Battlestarship Game



Take a look at these Groovy Codes:


Fun IQ Test


Html Svg Starburst


Javascript Particles Fishes



Read the Latest Breaking Programming and Tech News, Great Articles and Tips:


Codenewz Programming and Tech News






Comments

Popular posts from this blog

Multi-tap Keypad Text Entry

Crypto Mining