| 网站首页 | 业界新闻 | 技术文章 | 视频教程 | 下载频道 | 程序源码 | 个人空间 | 编程论坛 |
 
 
 
您现在的位置: 编程中国 >> 技术教程 >> .NET技术 >> VB.NET >> VB.NET教程 >> 正文
  ►  VB.NET 入门教程
VB.NET 入门教程
作者:未知    阅读人次:……    文章来源:microsoft    发布时间:2004-9-27    网友评论()条
 

补遗:QuickSort VISUAL BASIC .NET 的源代码

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

'
'  QuickSort Visual Basic .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 Academic Alliance Web site for more!
'  The source code is provided "as is" without warranty.
'

' Import namespaces
Imports System
Imports System.Collections
Imports System.IO

' Declare application class
Module QuickSortApp
    ' Application initialization
    Sub Main()
        'Print startup banner
        Console.WriteLine()
        Console.WriteLine("QuickSort Visual Basic .NET Sample Application")
        Console.WriteLine("Copyright (c)2001-2002 Microsoft Corporation. All rights reserved.")
        Console.WriteLine()
        Console.WriteLine("MSDN ACADEMIC ALLIANCE [http://www.msdn.microsoft.com/academic]")
        Console.WriteLine()
        ' 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.")
        Console.WriteLine()
        ' Prompt user for filenames
        Dim szSrcFile, szDestFile As String
        Console.Write("Source: ")
        szSrcFile = Console.ReadLine()
        Console.Write("Output: ")
        szDestFile = Console.ReadLine()
        ' Read contents of source file
        Dim szSrcLine As String
        Dim szContents As ArrayList
        Dim fsInput As FileStream
        Dim srInput As StreamReader
        szContents = New ArrayList()
        fsInput = New FileStream(szSrcFile, FileMode.Open, FileAccess.Read)
        srInput = New StreamReader(fsInput)
        szSrcLine = srInput.ReadLine()
        While Not IsNothing(szSrcLine)
            ' Append to array
            szContents.Add(szSrcLine)
            szSrcLine = srInput.ReadLine()
        End While
        srInput.Close()
        fsInput.Close()
        ' Pass to QuickSort function
        QuickSort(szContents, 0, szContents.Count - 1)
        ' Write sorted lines
        Dim fsOutput As FileStream
        Dim srOutput As StreamWriter
        Dim nIndex As Integer
        fsOutput = New FileStream(szDestFile, FileMode.Create, FileAccess.Write)
        srOutput = New StreamWriter(fsOutput)
        For nIndex = 0 To szContents.Count – 1
            ' Write line to output file
            srOutput.WriteLine(szContents(nIndex))
        Next nIndex
        srOutput.Close()
        fsOutput.Close()
        ' Report program success
        Console.WriteLine()
        Console.WriteLine("The sorted lines have been written to the output file.")
        Console.WriteLine()
        Console.WriteLine()
    End Sub
    ' QuickSort implementation
    Sub QuickSort(ByRef szArray As ArrayList, ByVal nLower As Integer, ByVal nUpper As Integer)
        ' Check for non-base case
        If nLower < nUpper Then
            ' Split and sort partitions
            Dim nSplit As Integer
            nSplit = Partition(szArray, nLower, nUpper)
            QuickSort(szArray, nLower, nSplit - 1)
            QuickSort(szArray, nSplit + 1, nUpper)
        End If
    End Sub
    ' QuickSort partition implementation
    Function Partition(ByRef szArray As ArrayList, ByVal nLower As Integer, ByVal nUpper As Integer) As Integer
        ' Pivot with first element
        Dim szPivot As String
        Dim nLeft, nRight As Integer
        nLeft = nLower + 1
        szPivot = szArray(nLower)
        nRight = nUpper
        ' Partition array elements
        Dim szSwap As String
        While nLeft <= nRight
            ' Find item out of place
            While nLeft <= nRight
                If szArray(nLeft).CompareTo(szPivot) > 0 Then Exit While
                nLeft = nLeft + 1
            End While
            While nLeft <= nRight
                If szArray(nRight).CompareTo(szPivot) <= 0 Then Exit While
                nRight = nRight – 1
            End While
            ' Swap values if necessary
            If (nLeft < nRight) Then
                szSwap = szArray(nLeft)
                szArray(nLeft) = szArray(nRight)
                szArray(nRight) = szSwap
                nLeft = nLeft + 1
                nRight = nRight – 1
            End If
        End While
        ' Move pivot element
        szSwap = szArray(nLower)
        szArray(nLower) = szArray(nRight)
        szArray(nRight) = szSwap        Return nRight
    End Function
End Module
 

上一页  [1] [2] [3] [4] [5] [6] [7] [8] 下一页

 

 
文章录入:静夜思    责任编辑:静夜思 
  • 上一篇文章: 没有了

  • 下一篇文章:

  •  
    相关文章
    没有相关文章
    原创地带
    24小时热门帖子