QuickSort C# .NET 的源代码
下面是 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 namespacesusing System;using System.Collections;using System.IO;// Declare namespacenamespace 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) nLeft = nLeft + 1; while (nLeft <= nRight && ((string) szArray[nRight]).CompareTo (szPivot) > 0) nRight = nRight - 1; // Swap values if necessary if (nLeft < nRight) { szSwap = (string) szArray[nLeft]; szArray[nLeft] = szArray[nRight]; szArray[nRight] = szSwap; nLeft = nLeft + 1; nRight = nRight - 1; } } // Move pivot element szSwap = (string) szArray[nLower]; szArray[nLower] = szArray[nRight]; szArray[nRight] = szSwap; return nRight; } }}
分享到:
相关推荐
QuickSort-文件阅读器 该存储库将包含对文本文件使用 QuickSort 方法的源代码。
一个Web文件上传的C#源代码.doc 为 ASP.NET Datagrid 创建自定义列.doc 为DataGrid写一个DropDownListColumn.txt 从 ASP.NET 服务器控件插入客户端脚本.doc 从 ASPX 页面进行 Web 服务调用时的性能考虑.do ...
快速排序源代码,采用C++编写,经测试未发现BUG,供大家参考
Quicksort program
C# 快速排序源码, 包含调用方式和注释,QuickSort.cs ///调用方式 /// /// int[] arr = new int[] { 5,3,9,6,4,7,8,1,2}; /// QuickSort.quickSort(arr, 0, arr.Length - 1); /// ///
C++实现快读排序,代码清晰友好,简单易懂
QuickSort算法的实现 最小生成树算法 多段图算法 N皇后问题算法设计 货郎担问题求最优解
摘要:VisualC#.NET是功能强大的编程语言VisualStudio套件的最新成员。VisualC#.NET入门指南通过...使用调试器小结补遗:QuickSortC#.NET的源代码补遗:关于QuickSortC#.NET简介VisualC#.NET是VisualStudio系列中的最新
北大POJ2299-Ultra-QuickSort 解题报告+AC代码
pku acm 2299 Ultra-QuickSort代码,合并排序求逆序数,解题报告请访问:http://blog.csdn.net/china8848
Java 排序算法源代码 BubbleSort.java InsertionSort.java MergeSort.java QuickSort.java SelectionSort.java ShellSort.java Sort.java Sorter.java
快速排序伪代码(非随机) 下面的过程实现快速排序: QUICKSORT(A,p,r) 1 if p 2 then q ←PARTITION(A,p,r) 3 QUICKSORT(A,p,q-1) 4 QUICKSORT(A,q+1,r) 为排序一个完整的数组A,最初的...
编写程序实现归并排序算法 MergeSortL 和快速排序算法 QuickSort;
代码中包含了快速排序这个经典算法的代码,并且给出了改进后的快速排序的代码,代码中同时包含了两个测试用例。测试命令:g++ quicksort.cpp -o quicksort ./quicksork
quicksort en c de la forma mas resumida posible
适用于大学数据结构的QuickSort实验提交
QuickSort-QuickSort
快速排序的迭代版本源代码,在vs上调试通过。
js代码-quickSort
资源名:quicksort_matlab_快速排序 资源类型:matlab项目全套源码 源码说明: 全部项目源码都是经过测试校正后百分百成功运行的,如果您下载后不能运行可联系我进行指导或者更换。 适合人群:新手及有一定经验的...