bubble sort

The following is bubble sort algorithm using C#:

//------------------------------------------------------------------------------
// <copyright file="bubbleSortAlgorithm.cs" company="Objectiva">
//     Copyright (c) 2008-2009. Objectiva Corporation. All Rights Reserved.
// </copyright>
// <summary>Defines the bubbleSortAlgorithm class.</summary>
// <Author>Allan Li.</Author>
//------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;

namespace bubbleSort
{
    class bubbleSortAlgorithm
    {
        private static List<int> bubbleSort(List<int> numbersToSort)
        {
            List<int> sortingNumbers = numbersToSort;

            for (int i = 0; i < sortingNumbers.Count; i++)
            {
                for (int j = 1; j < (sortingNumbers.Count - i); j++)
                {
                    if (sortingNumbers[j - 1] > sortingNumbers[j])
                    {
                        int t = sortingNumbers[j];
                        sortingNumbers[j] = sortingNumbers[j - 1];
                        sortingNumbers[j - 1] = t;
                    }

                    List<string> sortingStringList = sortingNumbers.ConvertAll<string>(delegate(int k) { return k.ToString(); });
                    string sortingString = string.Empty;
                    foreach (string item in sortingStringList)
                    {
                        sortingString += string.Format("{0}  ", item);
                    }

                    Console.WriteLine(string.Format("The {0} times sort:{1}", i + 1, sortingString));
                }
            }

            return sortingNumbers;
        }

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please input numbers you want to sort, press blank space between two numbers and press Enter to continue:");
                string inputString = Console.ReadLine();
                List<string> inputStringList = new List<string>(inputString.Trim().Split(' '));
                List<int> inputNumberList = new List<int>();

                foreach (string item in inputStringList)
                {
                    inputNumberList.Add(Convert.ToInt32(item));
                }

                List<int> sortedNumberList = bubbleSort(inputNumberList);
                List<string> sortedStringList = sortedNumberList.ConvertAll<string>(delegate(int j) { return j.ToString(); });
                string sortedString = string.Empty;
                foreach (string item in sortedStringList)
                {
                    sortedString += string.Format("{0}  ", item);
                }

                Console.WriteLine(string.Format("The sorted result is:{0}", sortedString));
                Console.ReadLine();
            }
            catch (Exception)
            {
                Console.WriteLine("Please input the correct format numbers!");
                Console.ReadLine();
            }
        }
    }
}

你可能感兴趣的:(Bubble)