Quick sorting, implemented in C ++

Source: Internet
Author: User

0 recommended

# I nclude "iostream"
# I nclude "vector"
# I nclude "fstream"
Using namespace STD;
Const int max = 100;

// Quick Sort Template
Template <class elemtype>
Class quick_sort {
Public:
Int partition (INT low, int high );
Void recquick_sort (INT frist, int last );
Quick_sort (vector <elemtype> & S );
~ Quick_sort ();
Protected:
Vector <elemtype> L;
};

Template <class elemtype>
Quick_sort <elemtype >:: quick_sort (vector <elemtype> & S)
{
L = s;
Recquick_sort (0, L. Size ()-1 );
For (vector <int >:: iterator iter = L. Begin (); iter! = L. End (); ITER ++)
Cout <* ITER <"";
Cout <Endl;
}
Template <class elemtype>
Quick_sort <elemtype> ::~ Quick_sort (){
}
Template <class elemtype>
Void quick_sort <elemtype>: recquick_sort (INT first, int last)
{
Int poiv;
If (first <last)
{
Poiv = partition (first, last );
Recquick_sort (first, poiv-1 );
Recquick_sort (poiv + 1, last );
}
}
Template <class elemtype>
Int quick_sort <elemtype >:: partition (INT low, int high)
{
Elemtype poiv;
Poiv = L [low];
While (low {
While (low <High & L [High]> = poiv) -- high;
L [low] = L [High];
While (low <High & L [low] <= poiv) ++ low;
L [High] = L [low];
}
L [low] = poiv;
Return low;
}
Int main ()
{
Int I, n, E;
Vector <int> L;
Ifstream in;
In. Open ("in.txt ");
If (! In)
{
Cerr <"error: Unable to open the file:" <in <Endl;
Return-1;
}
While (in> N)
{
For (I = 0; I <n; I ++)
{
In> E;
L. push_back (E );
}
Quick_sort <int> q (L );
L. Resize (0 );
}
Return 0;
}

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.