include iostream include stdlib include windows include stdio using na

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stdlib.h>
#include <windows.h>
#include <stdio.h>
using namespace std;
void BinaryInsertion (int *, int);
int count=0;
int main()
{
int a[50],i;
for (i=0;i<50;i++)
a[i]=rand()%100-50;
for (i=0;i<50;i++)
cout<<a[i]<<" ";
BinaryInsertion(a,50);
for (i=0;i<50;i++)
cout<<a[i]<<" ";
cout<<endl<<endl<<count<<endl;
return 0;
}
void BinaryInsertion (int *a, int n)
{
int i, j, m, left, right, x;
for (i=1; i<n; i++)
{
x=a[i]; left=0; right=i;
while (left<right) //ïîêà åñòü ÷òî ïðîñìàòðèâàòü
{
m = (left+right)/2; //äåëèì èíòåðâàë ïîèñêà ïîïîëàì
if (a[m]<=x) //åñëè âñòàâëÿåìîå çíà÷åíèå áîëüøå òåêóùåãî
left = m+1; //èñêàòü íàäî â ïðàâîé ÷àñòè
else
right = m; //à åñëè íåò, òî áóäåì èñêàòü â ëåâîé
count++;
}
for (j=i; j>right; a[j--]=a[j-1]);
a[right]=x;
}
}