当前位置: 网站首页 > .NET > c#

Visual C# .NET 入门:步骤 7. 创建函数

时间:1970-1-1 08:33:31来源: c#作者:admin 点击:0次 字体 [ С]
 步骤 7. 创建函数

    最后一步就是创建一个函数来在字符串数组中运行 QuickSort.我们将此函数放到应用程序类 QuickSortApp 之中。

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

// 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)
        {
            ... ... ...
            // Pass to QuickSort function
            QuickSort (szContents, 0, szContents.Count - 1);
            ... ... ...
        }
        // QuickSort implementation
        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
        static int Partition (ArrayList szArray, int nLower, int nUpper)
发表评论
验证码:
最新评论