Insert sort & Binary insert sort

Source: Internet
Author: User
# Include "stdafx. H"
# Include "insert_sort.h"

Insert_sort: insert_sort (vector <int> _ vector, int _ SIZE)
{
// Assign the value to the private member
For (INT I = 0; I <_ size-1; I ++)
{
Intvector. push_back (_ vector [I]);
}
Vector_size = _ size;
}

Insert_sort ::~ Insert_sort (void)
{
}
Void insert_sort: insert_sort_run ()
{
Int insertnumber;
// Sort began
For (INT I = 1; I <Vector_size-1; I ++)
{
Int J = I;
Insertnumber = intvector [I];
While (j> 0 & insertnumber <intvector [J-1]) // if the value greater than insertnumber, move it to the next
{
Intvector [J] = intvector [J-1];
J --;
}
Intvector [J] = insertnumber;
}
}
// Output the answer
Void insert_sort: output_vector ()
{
For (INT I = 0; I <intvector. Size (); I ++)
{
Cout <intvector [I] <"";
If (I> 5 & I % 10 = 0)
Cout <Endl;
}
Cout <Endl;
}

// Binary_insert_sort.cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include "binary_insert_sort.h"
# Include <iostream>
# Include <vector>
# Include <time. h>
Using namespace STD;

Int _ tmain (INT argc, _ tchar * argv [])
{
Clock_t start, finish;
Double duration;
Start = clock ();
Srand (unsigned) Time (null ));
Vector <int> intvector;
Int randomnumber;
Cout <"Please input the number of random" <Endl;
Cin> randomnumber;
For (INT I = 0; I <randomnumber; I ++)
{
Intvector. push_back (RAND () % 1000); // % 1000 );
}
Binary_insert_sort binarysort;
Cout <"initialize the vector for sort" <Endl;
Binarysort. binary_insert_sort_ini (intvector, intvector. Size ());
Cout <"binary_insert_sort is running" <Endl;
Binarysort. binary_insert_sort_run_sort ();
Binarysort. binary_insert_sort_output ();
Finish = clock ();
Duration = (double) (finish-Start)/clocks_per_sec;
Cout <"the running time is:" <duration <"seconds" <Endl;
System ("pause ");
Return 0;
}

Binary_insert_sort: binary_insert_sort (void)
{
}

Binary_insert_sort ::~ Binary_insert_sort (void)
{
Int_vector.clear ();
}
Void binary_insert_sort: binary_insert_sort_ini (vector <int> _ intvector, int _ SIZE)
{
For (INT I = 0; I <_ size; I ++)
{
Int_vector.push_back (_ intvector [I]);
}
Vector_size = _ size;
}
Void binary_insert_sort: binary_insert_sort_run_sort ()
{
Int low, high, middle;
Int swapintvalue;
For (INT I = 0; I <int_vector.size (); I ++)
{
Swapintvalue = int_vector [I];
Low = 0;
High = I-1;
While (low <= high)
{
Middle = (low + high)/2;
If (swapintvalue <int_vector [Middle])
High = middle-1;
Else
Low = middle + 1;
}
For (int m = I; m> low; m --)
Int_vector [m] = int_vector [M-1];
Int_vector [low] = swapintvalue;
}
}
Void binary_insert_sort: binary_insert_sort_output ()
{
For (INT I = 0; I <int_vector.size (); I ++)
{
Cout <int_vector [I] <"";
If (I> 5 & I % 10 = 0)
Cout <Endl;
}
Cout <Endl;
}

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.