Commits

Anonymous committed 2ce1713

Added the BubbleSort algorithm in C#.

Comments (0)

Files changed (4)

01-BubbleSort/BubbleSort-CSharp/BubbleSort-CSharp.csproj

+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProductVersion>9.0.21022</ProductVersion>
+    <SchemaVersion>2.0</SchemaVersion>
+    <ProjectGuid>{D51A319C-9086-40C2-8ED8-36569FBA92FB}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <AppDesignerFolder>Properties</AppDesignerFolder>
+    <RootNamespace>BubbleSort_CSharp</RootNamespace>
+    <AssemblyName>BubbleSort-CSharp</AssemblyName>
+    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+  </PropertyGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+    <Reference Include="System.Core">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Xml.Linq">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data.DataSetExtensions">
+      <RequiredTargetFramework>3.5</RequiredTargetFramework>
+    </Reference>
+    <Reference Include="System.Data" />
+    <Reference Include="System.Xml" />
+  </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
+       Other similar extension points exist, see Microsoft.Common.targets.
+  <Target Name="BeforeBuild">
+  </Target>
+  <Target Name="AfterBuild">
+  </Target>
+  -->
+</Project>

01-BubbleSort/BubbleSort-CSharp/Program.cs

+using System;
+
+namespace BubbleSort_CSharp
+{
+    class Program
+    {
+        /// <summary>
+        /// Program entry point.
+        /// </summary>
+        /// <param name="args">Command line arguments.</param>
+        static void Main(string[] args)
+        {
+            int[] dataSet = new int[] { 33, 98, 74, 13, 55, 20, 77, 45, 64, 83 };
+
+            Display(dataSet);
+
+            // pick any of the following implementations
+            //BubbleSortBasic(dataSet);
+            //BubbleSortBasicOptimised(dataSet);
+            BubbleSort(dataSet);
+            Display(dataSet);
+        }
+
+        /// <summary>
+        /// This is the basic bubble sort algorithm, hard-coded to work
+        /// with integer values.
+        /// </summary>
+        /// <param name="dataSet">Array of integers to sort.</param>
+        static void BubbleSortBasic(int[] dataSet)
+        {
+            // loop n-1 times.
+            for (int i = dataSet.Length - 1; i > 0 ; --i)
+            {
+                // for each loop, iterate through the first i
+                // items (ie. the unsorted ones)
+                for (int j = 0; j < i; ++j)
+                {
+                    // if adjacent items need to be swapped
+                    if (dataSet[j] > dataSet[j + 1])
+                    {
+                        // swap them
+                        Swap(dataSet, j, j + 1);
+                    }
+                }
+            }
+        }
+
+        /// <summary>
+        /// This is the basic bubble sort algorithm, hard-coded to work
+        /// with integer values but with a slight difference - a minor
+        /// optimisation.
+        /// </summary>
+        /// <param name="dataSet">Array of integers to sort.</param>
+        static void BubbleSortBasicOptimised(int[] dataSet)
+        {
+            // loop n-1 times.
+            for (int i = dataSet.Length - 1; i > 0 ; --i)
+            {
+                // keep track of whether items were swapped
+                // for this iteration
+                bool swapped = false;
+
+                // for each loop, iterate through the first i
+                // items (ie. the unsorted ones)
+                for (int j = 0; j < i; ++j)
+                {
+                    // if adjacent items need to be swapped
+                    if (dataSet[j] > dataSet[j + 1])
+                    {
+                        // swap them
+                        Swap(dataSet, j, j + 1);
+
+                        // indicate that we found a swap
+                        swapped = true;
+                    }
+                }
+
+                // if nothing was swapped, then we should
+                // already have everything in order
+                if (!swapped)
+                {
+                    break;
+                }
+            }
+        }
+
+        /// <summary>
+        /// This is the genericly optimised version.
+        /// </summary>
+        /// <typeparam name="T">A comparable type.</typeparam>
+        /// <param name="dataSet">Array of those comparable types.</param>
+        /// <remarks>Handles all types of arrays as long as
+        /// the individual items are comparable.</remarks>
+        static void BubbleSort<T>(T[] dataSet) where T : IComparable
+        {
+            // loop n-1 times.
+            for (int i = dataSet.Length - 1; i > 0 ; --i)
+            {
+                // keep track of whether items were swapped
+                // for this iteration
+                bool swapped = false;
+
+                // for each loop, iterate through the first i
+                // items (ie. the unsorted ones)
+                for (int j = 0; j < i; ++j)
+                {
+                    // if adjacent items need to be swapped
+                    if(dataSet[j].CompareTo(dataSet[j + 1]) > 0)
+                    {
+                        // swap them
+                        Swap(dataSet, j, j + 1);
+
+                        // indicate that we found a swap
+                        swapped = true;
+                    }
+                }
+
+                // if nothing was swapped, then we should
+                // already have everything in order
+                if (!swapped)
+                {
+                    break;
+                }
+            }
+        }
+
+        /// <summary>
+        /// Generic helper swap method.
+        /// </summary>
+        /// <typeparam name="T">The type contained in the array.</typeparam>
+        /// <param name="dataSet">Reference to the array of type T.</param>
+        /// <param name="i">One of the indexes to swap.</param>
+        /// <param name="j">One of the indexes to swap.</param>
+        /// <remarks>Items and index i and j are swapped.</remarks>
+        static void Swap<T>(T[] dataSet, int i, int j)
+        {
+            T temp = dataSet[i];
+            dataSet[i] = dataSet[j];
+            dataSet[j] = temp;
+        }
+
+        /// <summary>
+        /// Helper display method.
+        /// </summary>
+        /// <typeparam name="T">Type to display.</typeparam>
+        /// <param name="a">Array of type T to be displayed.</param>
+        static void Display<T>(T[] a)
+        {
+            foreach (T t in a)
+            {
+                Console.Write(" " + t.ToString());
+            }
+            Console.WriteLine();
+        }
+    }
+}

01-BubbleSort/BubbleSort-CSharp/Properties/AssemblyInfo.cs

+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// General Information about an assembly is controlled through the following 
+// set of attributes. Change these attribute values to modify the information
+// associated with an assembly.
+[assembly: AssemblyTitle("BubbleSort-CSharp")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("BubbleSort-CSharp")]
+[assembly: AssemblyCopyright("Copyright ©  2008")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// Setting ComVisible to false makes the types in this assembly not visible 
+// to COM components.  If you need to access a type in this assembly from 
+// COM, set the ComVisible attribute to true on that type.
+[assembly: ComVisible(false)]
+
+// The following GUID is for the ID of the typelib if this project is exposed to COM
+[assembly: Guid("aaa25cbc-50fe-4257-88e1-6d041d6e292b")]
+
+// Version information for an assembly consists of the following four values:
+//
+//      Major Version
+//      Minor Version 
+//      Build Number
+//      Revision
+//
+// You can specify all the values or you can default the Build and Revision Numbers 
+// by using the '*' as shown below:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

01-BubbleSort/BubbleSort.sln

+
+Microsoft Visual Studio Solution File, Format Version 10.00
+# Visual Studio 2008
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BubbleSort-CSharp", "BubbleSort-CSharp\BubbleSort-CSharp.csproj", "{D51A319C-9086-40C2-8ED8-36569FBA92FB}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Any CPU = Debug|Any CPU
+		Release|Any CPU = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{D51A319C-9086-40C2-8ED8-36569FBA92FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{D51A319C-9086-40C2-8ED8-36569FBA92FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{D51A319C-9086-40C2-8ED8-36569FBA92FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{D51A319C-9086-40C2-8ED8-36569FBA92FB}.Release|Any CPU.Build.0 = Release|Any CPU
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal