/************************************************************************/
/*Binary Insert Sort */
/************************************************************************/
#include <iostream>
#include <time.h>
#include "Timer.h"
using namespace std;
#define Swap(x, y) {int temp = x; x = y; y = temp;}
const int MAX = 100000;
void Input(int* numbers)
{
srand((unsigned)time(NULL));
for (int i = 0; i < MAX; i++)
{
numbers[i] = rand() % (MAX * 10);
}
}
void Output(int* numbers)
{
for (int i = 1; i <= MAX; i++)
{
cout << numbers[i-1] << " ";
if (0 == i % 10)
{
cout << endl;
}
}
cout << endl;
}
//折半插入排序
void BiInsertSort(int* numbers, int low, int high)
{
for (int i = 1; i < high; i++)
{
int temp = numbers[i];
int l = low;
int h = i;
while (l <= h)
{
int mid = (l + h) / 2;
if (temp > numbers[mid])
{
l = mid + 1;
}
else
{
h = mid - 1;
}
}
//Move datas
for (int j = i; j > l; j--)
{
numbers[j] = numbers[j-1];
}
numbers[l] = temp;
}
}
void main()
{
//int num[MAX];
int* num = new int[MAX];
Input(num);
cout << "Bubble before: " << endl;
//Output(num);
Timer timer;
BiInsertSort(num, 0, MAX);
cout <<"\nTime Elapsed: " << timer.GetElapsedTime() << "s" << endl;
cout << "\nBubble after: " << endl;
//Output(num);
delete[] num;
}
测试数据:10万个int数据。
运行结果如下: