| 作者:未知 文章来源:microsoft 点击数: 33058 更新时间: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 |
正文
VB.NET 入门教程72005-09-24 07:30:00
【评论】 【打印】 【字体:大 中 小】 本文链接:http://blog.pfan.cn/iamben250/5196.html
阅读(1869) | 评论(0)
版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论