新聞中心
本文向大家介紹QuickSort C# .NET,可能好多人還不知道QuickSort C# .NET,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

創(chuàng)新互聯(lián)建站是一家專業(yè)提供麥蓋提企業(yè)網(wǎng)站建設(shè),專注與成都做網(wǎng)站、成都網(wǎng)站制作、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為麥蓋提眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
本文旨在幫助您用 Visual Studio 構(gòu)建一個(gè)簡(jiǎn)單的 C# 項(xiàng)目。它無(wú)法進(jìn)行全面的介紹。我們鼓勵(lì)您查詢關(guān)于 C# 和 .NET 的其他資源,以便更多地學(xué)習(xí)這些技術(shù)。在完成本教程之后,您至少有了一個(gè)可用的項(xiàng)目,在您研究 Visual C# 時(shí),可以從修改此這些代碼開始。
下面是 QuickSort C# .NET 示例應(yīng)用程序的完整源代碼。您可以復(fù)制、使用和分發(fā)這些代碼(無(wú)版權(quán)費(fèi))。注意,這些源代碼以"原樣"提供并且不作任何保證。
- //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)
- nLeftnLeft = nLeft + 1;
- while (nLeft <= nRight && ((string) szArray[nRight]).CompareTo (szPivot) > 0)
- nRightnRight = nRight - 1;
- // Swap values if necessary
- if (nLeft < nRight)
- {
- szSwap = (string) szArray[nLeft];
- szArray[nLeft] = szArray[nRight];
- szArray[nRight] = szSwap;
- nLeftnLeft = nLeft + 1;
- nRightnRight = nRight - 1;
- }
- }
- // Move pivot element
- szSwap = (string) szArray[nLower];
- szArray[nLower] = szArray[nRight];
- szArray[nRight] = szSwap;
- return nRight;
- }
- }
- }
關(guān)于 QuickSort C# .NET為了演示 QuickSort Visual C# .NET 示例應(yīng)用程序?qū)嶋H是如何運(yùn)行的,我們提供了編譯好的可執(zhí)行文件。您可以通過編譯這些項(xiàng)目文件來(lái)創(chuàng)建自己的可執(zhí)行文件。單擊 Quicksort_Visual_CSharp_.NET.exe,下載源代碼項(xiàng)目文件和可執(zhí)行文件包。
使用應(yīng)用程序啟動(dòng) Command Prompt(從"開始"菜單運(yùn)行"cmd.exe")。使用 CD 命令將目錄更改為可執(zhí)行文件所在的目錄。然后運(yùn)行"quicksort.exe".
程序?qū)⑻崾灸峁┹斎牒洼敵鑫募拿Q。任何包含多行的文本文件均可使用。如果需要,可以使用記事本來(lái)創(chuàng)建一個(gè)此類文件。然后,該程序?qū)?duì)輸入文件的內(nèi)容進(jìn)行排序,并且將其寫入輸出文件。
示例程序輸出下面是來(lái)自此 QuickSort C# .NET 應(yīng)用程序的一個(gè)實(shí)例的輸出。此示例演示了 QuickSort 算法,方法是讀取輸入文件、對(duì)文件的內(nèi)容進(jìn)行排序,然后將其寫入新的文件。
【編輯推薦】
- C# 3.0編譯器簡(jiǎn)單介紹
- C#使用函數(shù)重載學(xué)習(xí)筆記
- Visual C#對(duì)數(shù)據(jù)庫(kù)處理概述
- C#具有隱式類型聲明描述
- C#使用SharpZipLib分析
本文題目:QuickSort C# .NET剖析
網(wǎng)頁(yè)URL:http://www.dlmjj.cn/article/ccccjjj.html


咨詢
建站咨詢
