#include <iostream>
using namespace std;
void Bin_sort(int a[],int n)
{
for(int i=1; i<=n; ++i)
{
if(a[i] < a[i-1])
{
int temp = a[i];
//折半查找
int low = 0;
int high = i-1;
while(low <= high)
{
int mid = (low+high)/2;
if(a[mid] > temp)
{
high = mid-1;
}
else
{
low = mid+1;
}
}
for(int j=i-1; j > high; j--)
{
a[j+1] = a[j];
}
a[high+1] = temp;
}
}
}
void print(int a[],int n)
{
for(int i=0; i<n; i++)
{
cout<<a[i]<<" ";
}
cout << endl;
}
void main()
{
int a[] = {1,3,6,8,0,5,7};
cout << "排序前:";
print(a,sizeof(a)/sizeof(a[0]));
Bin_sort(a,sizeof(a)/sizeof(a[0]));
cout << "排序后:";
print(a,sizeof(a)/sizeof(a[0]));
}
排序前:1 3 6 8 0 5 7
排序后:0 1 3 5 6 7 8
Press any key to continue