QuickSort C# .NET剖析

开发 后端
这里介绍QuickSort C# .NET,本文旨在帮助您用 Visual Studio 构建一个简单的 C# 项目。它无法进行全面的介绍。我们鼓励您查询关于 C# 和 .NET 的其他资源,以便更多地学习这些技术。

本文向大家介绍QuickSort C# .NET,可能好多人还不知道QuickSort C# .NET,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

本文旨在帮助您用 Visual Studio 构建一个简单的 C# 项目。它无法进行全面的介绍。我们鼓励您查询关于 C# 和 .NET 的其他资源,以便更多地学习这些技术。在完成本教程之后,您至少有了一个可用的项目,在您研究 Visual C# 时,可以从修改此这些代码开始。

下面是 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)  
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;  
}  
}  

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.

关于 QuickSort C# .NET为了演示 QuickSort Visual C# .NET 示例应用程序实际是如何运行的,我们提供了编译好的可执行文件。您可以通过编译这些项目文件来创建自己的可执行文件。单击 Quicksort_Visual_CSharp_.NET.exe,下载源代码项目文件和可执行文件包。

使用应用程序启动 Command Prompt(从"开始"菜单运行"cmd.exe")。使用 CD 命令将目录更改为可执行文件所在的目录。然后运行"quicksort.exe".

程序将提示您提供输入和输出文件的名称。任何包含多行的文本文件均可使用。如果需要,可以使用记事本来创建一个此类文件。然后,该程序将对输入文件的内容进行排序,并且将其写入输出文件。

示例程序输出下面是来自此 QuickSort C# .NET 应用程序的一个实例的输出。此示例演示了 QuickSort 算法,方法是读取输入文件、对文件的内容进行排序,然后将其写入新的文件。

【编辑推荐】

  1. C# 3.0编译器简单介绍
  2. C#使用函数重载学习笔记
  3. Visual C#对数据库处理概述
  4. C#具有隐式类型声明描述
  5. C#使用SharpZipLib分析
责任编辑:佚名 来源: 博客园
相关推荐

2009-09-07 14:29:52

C# ServiceC

2009-08-27 17:14:36

C# Socket

2009-10-21 10:45:50

VB.NET Quic

2009-09-11 11:17:04

C#引用类型

2009-08-27 16:29:18

C#动态编译

2009-09-01 11:04:59

C#调用扩展方法

2009-09-11 11:09:36

C#引用类型

2009-09-02 18:14:33

C# WebClien

2009-08-27 17:51:34

C#匿名方法

2009-08-28 10:44:46

C#字符数组转换

2009-08-31 17:26:32

C#异常处理

2009-09-18 10:00:17

C#数组操作

2009-09-03 16:58:49

C#内存管理

2009-08-28 11:16:51

C#日期型数

2009-09-02 13:36:58

C#实现多个接口

2009-09-04 17:56:22

C#删除数据

2009-08-28 15:32:39

C#利用WMI获取数据

2009-09-02 17:14:28

C#修饰符

2009-09-09 14:04:18

C# XML解析XML解析方法

2009-08-31 15:39:11

C#编写操作TreeV
点赞
收藏

51CTO技术栈公众号