Quicksort (C #)

Source: Internet
Author: User

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;
}

}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.