STL in C/C ++

Source: Internet
Author: User
# Include "stdafx. H"
# Include "mergesort. H"

Typedef vector <int> intdefvector;
Mergesort: mergesort (void)
{
}

Mergesort ::~ Mergesort (void)
{
}

Void mergesort: mergesort_ini (STD: vector <int> _ int_vector, int _ vector_size)
{
Mergesort: list_vector.push_back (0 );
Mergesort: link_vector.push_back (0 );
For (INT I = 0; I <_ vector_size; I ++)
{
List_vector.push_back (_ int_vector [I]);
Link_vector.push_back (0 );
}
List_vector_size = _ vector_size;
}

Int mergesort: mergesort_list_merge (INT ST1, int st2)
{
Int K = 0, I = ST1, j = st2;
While (I & J)
If (list_vector [I] <= list_vector [J])
{
Link_vector [k] = I;
K = I;
I = link_vector [I];
}
Else
{
Link_vector [k] = J;
K = J;
J = link_vector [J];
}
If (! I) link_vector [k] = J;
Else link_vector [k] = I;
Return link_vector [0];
}
Int mergesort: mergesort_sort (INT left, int right)
{
If (left> = right) return left;
Int middle = (left + right)/2;

Return mergesort_sort (mergesort_list_merge (left, middle), mergesort_list_merge (middle + 1, right ));
}

Vector <int> mergesort: mergesort_split (vector <int> _ int_vector, int _ vector_size)
{
Intdefvector vectorleft;
Intdefvector vectorright;
Intdefvector vectorresult;
// Obtain the intermediate subscript
Int middle = _ vector_size/2;
// If the array has only one element, it is returned directly without sorting.
If (_ vector_size <= 1)
Return _ int_vector;
For (int K = 0; k <_ vector_size; k ++)
{
If (k <middle)
{
Vectorleft. push_back (_ int_vector [k]);
}
Else
{
Vectorright. push_back (_ int_vector [k]);
}
}
// Recursively splits the left array.
Vectorleft = mergesort_split (vectorleft, vectorleft. Size ());
// Recursively splits the right Array
Vectorright = mergesort_split (vectorright, vectorright. Size ());
/// Sort and merge
Vectorresult = mergesort_merge (vectorleft, vectorleft. Size (), vectorright, vectorright. Size ());
Return vectorresult;
}
Vector <int> mergesort: mergesort_merge (STD: vector <int> _ int_vector_left, int _ int_vector_left_len,
STD: vector <int> _ int_vector_right, int _ int_vector_right_len)
{
Intdefvector vectorresult;
Int I = 0, j = 0;
While (I <_ int_vector_left_len & J <_ int_vector_right_len)
{
If (_ int_vector_left [I] <_ int_vector_right [J])
// Assign a small value to the result
Vectorresult. push_back (_ int_vector_left [I ++]);
Else
// Assign a small value to the result
Vectorresult. push_back (_ int_vector_right [J ++]);
}
While (I <_ int_vector_left_len)
{
// Assign the last element to the result
Vectorresult. push_back (_ int_vector_left [I ++]);
}
While (j <_ int_vector_right_len)
{
// Assign the last element to the result
Vectorresult. push_back (_ int_vector_right [J ++]);
}
Return vectorresult;
}
Void mergesort: mergesort_output ()
{
Int I = link_vector [0];
Int J = 0;
While (I)
{
J ++;
Cout <list_vector [I] <"";
I = link_vector [I];
If (I> 5 & I % 10 = 0)
Cout <Endl;
}
Cout <Endl;
}

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.