流浪家园

上帝给我们十字架,同时也给我们背负十字架的勇气和力量!

QuickSort(C#)

对简单的文本按行进行快速排序

//////////////////////////////////////////////////////////////////////////
// QuickSort ues C#
// By HCJ
// 2005.4.26
//////////////////////////////////////////////////////////////////////////

//import namespace
using System;
using System.Collections;
using System.IO;
//using QuickSort;

namespace QuickSortApp
{

 class QuickSort
 {
  
  [STAThread]
  static void Main(string[] args)
  {
                Console.WriteLine ("/nQuickSort simple use C#");
                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");
   
                 //get source  and out file name
                  Console.Write("Source File: ");
                  string srcFile=Console.ReadLine();
                  Console.Write("Output File: ");
                  string outFile=Console.ReadLine();

                 //read contents of source file

                  string szSrcLine;
                  ArrayList szComtents = new ArrayList();
                  FileStream fsInput = new FileStream( srcFile, FileMode.Open, FileAccess.Read);
                  StreamReader srInput = new StreamReader( fsInput );

                 while( (szSrcLine=srInput.ReadLine()) != null )
                 {
                          szComtents.Add(szSrcLine);
                 }
                 srInput.Close();
                 fsInput.Close();
   
                 Sort( szComtents,0,szComtents.Count-1 );

                 //write to output file
                 FileStream fsOut  = new FileStream                    (outFile,FileMode.Create,FileAccess.Write);
                  StreamWriter srOut = new StreamWriter( fsOut );
 
                for(int i =0 ; i < szComtents.Count-1; i++ )
               {
                     srOut.WriteLine( szComtents[i] );
               }
   
               srOut.Close();
               fsOut.Close();
   

               Console.WriteLine("/nThe sorted lines have been written to the output file.");
               Console.WriteLine("/nPlease press any key to contiune....");
               Console.Read();
  }

  public static void Sort(ArrayList szArray,int low, int high )
  {
               if( low<high )
              {
                       int pos = GetPos( szArray,low,high );
                        Sort(szArray, low,pos-1 );
                       Sort(szArray,pos+1,high);
             }
   }
  //GetPos implementation
  private static int GetPos(ArrayList szArray, int low, int high)
  {
             int left=low+1;
             string szPivot = (string)szArray[low];
              int right=high;
               string szSwap;

              while (left<right)
             {
                          while( left<=right && ((string)(szArray[left])).CompareTo(szPivot) <= 0 )
                            left++;
    
                           while( left<=right && ((string)(szArray[right])).CompareTo(szPivot) > 0 )
                           right--;
                            //swap value if necessary

                            if( left<right )
                           {
                                     szSwap =(string)szArray[left];
                                     szArray[left]=szArray[right];
                                     szArray[right]=szSwap;
                                      left++;
                                       right--;
                          }
            }
                         //move povit element
                        szSwap=(string)szArray[low];
                        szArray[low]=szArray[right];
                       szArray[right]=szSwap;

                       return right;
  }

 }
}

阅读更多
个人分类:
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭