#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 : 定义控制台应用程序的入口点。
//
#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<<"程序运行时间是: "<<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;
}
转载于:https://www.cnblogs.com/alexusli/archive/2009/09/17/1568551.html