Sort simple text by row quickly
//////////////////////////////////////// //////////////////////////////////
// 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 {
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 szwon = (string) szarray [low];
Int right = high;
String szswap;
While (left <right)
{
While (left <= Right & (string) (szarray [left]). compareto (szeto) <= 0)
Left ++;
While (left <= Right & (string) (szarray [right]). compareto (szeto)> 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;
}
}
}