填补QuickSort C# .NET 的源代码

QuickSort C# .NET 的源代码

下面是 QuickSort C# .NET 示例应用程序的完整源代码。您可以复制、使用和分发这些代码(无版权费)。注意,这些源代码以"原样"提供并且不作任何保证。

//
//  QuickSort C# .NET Sample Application
//  Copyright 2001-2002 Microsoft Corporation. All rights reserved.
//
//  MSDN ACADEMIC ALLIANCE [http://www.msdn.microsoft.com/academic]
//  This sample is part of a vast collection of resources we developed for
//  faculty members in K-12 and higher education. Visit the MSDN AA web site for more!
//  The source code is provided "as is" without warranty.
//
// Import namespaces
using System;
using System.Collections;
using System.IO;
// Declare namespace
namespace MsdnAA
{
    // Declare application class
    class QuickSortApp
    {
        // Application initialization
        static void Main (string[] szArgs)
        {
            // Print startup banner
            Console.WriteLine ("/nQuickSort C#.NET Sample Application");
            Console.WriteLine ("Copyright (c)2001-2002 Microsoft Corporation. All rights reserved./n");
            Console.WriteLine ("MSDN ACADEMIC ALLIANCE [http://www.msdnaa.net/]/n");
            // Describe program function
            Console.WriteLine 
("This example demonstrates the QuickSort algorithm by reading an input file,");
            Console.WriteLine ("sorting its contents, and writing them to a new file./n");
            // Prompt user for filenames
            Console.Write ("Source: ");
            string szSrcFile = Console.ReadLine ();
            Console.Write ("Output: ");
            string szDestFile = Console.ReadLine ();
            // Read contents of source file
            string szSrcLine;
            ArrayList szContents = new ArrayList ();
            FileStream fsInput = new FileStream (szSrcFile, FileMode.Open, FileAccess.Read);
            StreamReader srInput = new StreamReader (fsInput);
            while ((szSrcLine = srInput.ReadLine ()) != null)
            {
                // Append to array
                szContents.Add (szSrcLine);
            }
            srInput.Close ();
            fsInput.Close ();
            // Pass to QuickSort function
            QuickSort (szContents, 0, szContents.Count - 1);
            // Write sorted lines
            FileStream fsOutput = new FileStream (szDestFile, FileMode.Create, FileAccess.Write);
            StreamWriter srOutput = new StreamWriter (fsOutput);
            for (int nIndex = 0; nIndex < szContents.Count; nIndex++)
            {
                // Write line to output file
                srOutput.WriteLine (szContents[nIndex]);
            }
            srOutput.Close ();
            fsOutput.Close ();
            // Report program success
            Console.WriteLine ("/nThe sorted lines have been written to the output file./n/n");
        }
        // QuickSort implementation
        private static void QuickSort (ArrayList szArray, int nLower, int nUpper)
        {
            // Check for non-base case
            if (nLower < nUpper)
            {
                // Split and sort partitions
                int nSplit = Partition (szArray, nLower, nUpper);
                QuickSort (szArray, nLower, nSplit - 1);
                QuickSort (szArray, nSplit + 1, nUpper);
            }
        }
        // QuickSort partition implementation
        private static int Partition (ArrayList szArray, int nLower, int nUpper)
        {
            // Pivot with first element
            int nLeft = nLower + 1;
            string szPivot = (string) szArray[nLower];
            int nRight = nUpper;
            // Partition array elements
            string szSwap;
            while (nLeft <= nRight)
            {
                // Find item out of place
                while (nLeft <= nRight && ((string) szArray[nLeft]).CompareTo (szPivot) <= 0)
                    nLeft = nLeft + 1;
                while (nLeft <= nRight && ((string) szArray[nRight]).CompareTo (szPivot) > 0)
                    nRight = nRight - 1;
                // Swap values if necessary
                if (nLeft < nRight)
                {
                    szSwap = (string) szArray[nLeft];
                    szArray[nLeft] = szArray[nRight];
                    szArray[nRight] = szSwap;
                    nLeft = nLeft + 1;
                    nRight = nRight - 1;
                }
            }
            // Move pivot element
            szSwap = (string) szArray[nLower];
            szArray[nLower] = szArray[nRight];
            szArray[nRight] = szSwap;
            return nRight;
        }
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值