quicksort

最近在上code quality的课程,要求写一个快速排序算法。

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

namespace quickSort
{
    public class quickSortAlgorithm
    {
        /// <summary>
        /// Input a list of out-of-order numbers and you can get in-order numbers.
        /// </summary>
        /// <param name="list">The numbers that you want to sort.</param>
        /// <param name="start">The first in the number list.</param>
        /// <param name="end">The last in the number list.</param>
        /// <returns>The in-order number list.</returns>
        public static List<int> QuickSort(List<int> list, int start, int end)
        {
            int index = partition(list, start, end);
            if (index - 1 > start)
            {
                QuickSort(list, start, index);
            }

            if (index + 1 < end)
            {
                QuickSort(list, index, end);
            }

            return list;
        }

        /// <summary>
        /// Initialize the index.
        /// </summary>
        /// <param name="list">The numbers that you want to sort.</param>
        /// <param name="start">The first in the number list.</param>
        /// <param name="end">The last in the number list.</param>
        /// <returns>An index that represents the started location.</returns>
        private static int partition(List<int> list, int start, int end)
        {
            int i = start;

            for (int j = start; j < end - 1; j++)
            {
                if (list[j] <= list[end - 1])
                {
                    i++;
                }
            }

            int temp = list[i];
            list[i] = list[end - 1];
            list[end - 1] = temp;
            return i;
        }

        /// <summary>
        /// Main Entry point for this excutable.
        /// </summary>
        /// <param name="args"></param>
        public static void Main(string[] args)
        {
            try
            {
                Console.WriteLine("Please input the numbers that you want to sort,");
                Console.WriteLine("press blank space between the two numbers and press enter:");
                string strNumber = Console.ReadLine();
                List<string> stringList = new List<string>(strNumber.Trim().Split(' '));
                List<int> numberList = new List<int>();
                foreach (string item in stringList)
                {
                    numberList.Add(Convert.ToInt32(item));
                }

                List<int> resultNumberList = QuickSort(numberList, 0, numberList.Count);
                List<string> resultStringList = resultNumberList.ConvertAll<string>(delegate(int i) { return i.ToString(); });
                string resultString = string.Empty;
                foreach (string item in resultStringList)
                {
                    resultString += string.Format("{0}  ", item);
                }

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

}

你可能感兴趣的:(Quicksort)