讲述VB.NET QuickSort函数

开发 后端
这里介绍VB.NET QuickSort函数,包括介绍调用 Partition() 函数将数组分成两部分,其中一部分包含 Pivot 值之前的所有字符串,另一部分包含 Pivot 值之后的所有字符串。然后,它调用自身来对每个部分进行排序。

VB.NET经过长时间的发展,很多用户都很了解VB.NET QuickSort函数了,这里我发表一下个人理解,和大家讨论讨论。首先创建一个函数来在字符串数组中运行VB.NET QuickSort函数。我们将此函数放到应用程序类 QuickSortApp 之中。

修改源代码

更改 C# 源文件 (class1.cs),如下面以斜体突出显示的 代码所示。其他的差异(如类名)可忽略不计。

  1. // Import namespaces  
  2. using System;  
  3. using System.Collections;  
  4. using System.IO;  
  5. // Declare namespace  
  6. namespace MsdnAA  
  7. {  
  8.     // Declare application class  
  9.     class QuickSortApp  
  10.     {  
  11.         // Application initialization  
  12.         static void Main (string[] szArgs)  
  13.         {  
  14.             ... ... ...  
  15.             // Pass to QuickSort function  
  16.             QuickSort (szContents, 0, szContents.Count - 1);  
  17.             ... ... ...  
  18.         }  
  19.         // QuickSort implementation  
  20.         static void QuickSort (ArrayList szArray, int nLower, int nUpper)  
  21.         {  
  22.             // Check for non-base case  
  23.             if (nLower < nUpper)  
  24.             {  
  25.                 // Split and sort partitions  
  26.                 int nSplit = Partition (szArray, nLower, nUpper);  
  27.                 QuickSort (szArray, nLower, nSplit - 1);  
  28.                 QuickSort (szArray, nSplit + 1, nUpper);  
  29.             }  
  30.         }  
  31.         // QuickSort partition implementation  
  32.         static int Partition (ArrayList szArray, int nLower, int nUpper)  
  33.         {  
  34.             // Pivot with first element  
  35.             int nLeft = nLower + 1;  
  36.             string szPivot = (string) szArray[nLower];  
  37.             int nRight = nUpper;  
  38.             // Partition array elements  
  39.             string szSwap;  
  40.             while (nLeft <= nRight)  
  41.             {  
  42.                 // Find item out of place  
  43.                 while (nLeft <= nRight)  
  44.                 {  
  45.                     if (((string) szArray[nLeft]).CompareTo (szPivot) > 0)  
  46.                         break;  
  47.                     nLeftnLeft = nLeft + 1;  
  48.                 }  
  49.                 while (nLeft <= nRight)  
  50.                 {  
  51.                     if (((string) szArray[nRight]).CompareTo (szPivot) <= 0)  
  52.                         break;  
  53.                     nRightnRight = nRight - 1;  
  54.                 }  
  55.                 // Swap values if necessary  
  56.                 if (nLeft < nRight)  
  57.                 {  
  58.                     szSwap = (string) szArray[nLeft];  
  59.                     szArray[nLeft] = szArray[nRight];  
  60.                     szArray[nRight] = szSwap;  
  61.                     nLeftnLeft = nLeft + 1;  
  62.                     nRightnRight = nRight - 1;  
  63.                 }  
  64.             }  
  65.             // Move pivot element  
  66.             szSwap = (string) szArray[nLower];  
  67.             szArray[nLower] = szArray[nRight];  
  68.             szArray[nRight] = szSwap;  
  69.             return nRight;  
  70.         }  
  71.     }  
  72. }  

VB.NET QuickSort函数

这个函数需要三个参数:对数组的引用、下界和上界。它调用 Partition() 函数将数组分成两部分,其中一部分包含 Pivot 值之前的所有字符串,另一部分包含 Pivot 值之后的所有字符串。然后,它调用自身来对每个部分进行排序。

上面修改中的注释应该说明了每个代码块的作用。唯一的新概念就是 CompareTo() 方法的使用,该方法是 String 类的成员,并且应该是自说明的。

运行 QuickSort 应用程序

这一步完成 QuickSort C# 示例应用程序。现在,可以构建项目并运行应用程序。需要提供一个示例文本文件,以供其进行排序。将该文件放在与 EXE 文件相同的目录中。

QuickSort C# 示例应用程序


程序输出

下面是已完成的 QuickSort C# .NET 示例应用程序的输出。

QuickSort C# .NET 示例应用程序

【编辑推荐】

  1. 分析VB QuickSort应用程序
  2. 如何掌握强大的VB.NET ReadLine()方法
  3. 讲述强大的VB.NET Web Forms,使用起来却如此简单
  4. 两步就可以掌握VB使用ArrayList类
  5. VB.NET应用程序的入门指南
责任编辑:佚名 来源: cnbeta
相关推荐

2009-10-16 13:26:53

VB.NET Exce

2009-10-14 17:08:44

VB.NET使用Fil

2009-10-13 17:03:55

VB.NET面向对象

2009-11-02 15:45:03

VB.NET IEnu

2009-10-12 16:39:59

OracleTransVB.NET使用

2009-10-16 09:35:24

VB.NET制作透明窗

2009-10-19 08:55:22

VB.NET多重继承

2009-10-15 11:11:08

VB.NET Text

2009-10-23 13:22:25

VB.NET实现拖动图

2009-10-10 16:44:52

VB.NET开发控件

2009-10-14 11:15:06

VB.NET Grou

2009-10-15 16:39:00

VB.NET读取INI

2009-10-27 11:39:03

VB.NET事件处理程

2009-10-26 18:11:47

VB.NET调用Exc

2009-10-21 18:28:48

VB.NET表间拖放

2009-10-22 09:20:46

VB.NET Proc

2009-10-29 09:57:16

VB.NET实现数据绑

2009-11-03 17:31:01

VB.NET窗体

2010-01-15 13:30:50

VB.NET Prog

2009-10-26 19:22:29

VB.NET使用Log
点赞
收藏

51CTO技术栈公众号