Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- procedure BinaryInserts(var mas:TMyArr; const n:integer; out cmp1,trans1:integer);
- var
- i,j,m,l,r,x:integer;
- begin
- for i:=1 to n-1 do
- begin
- x:=mas[i];
- l:=1;
- r:=i;
- while l<r do
- begin
- m:=(l+r) div 2;
- cmp1:=cmp1+1;
- if mas[m]<x then l:=m+1
- else r:=m;
- end;
- for j:=i downto r+1 do
- begin
- mas[j]:=mas[j-1];
- trans1:=trans1+1;
- end;
- mas[r]:=x;
- trans1:=trans1+1;
- end;
- end;
- procedure StraightSelection(var mas:TMyArr; const n:integer; out cmp1,trans1:integer);
- var
- min,i,j,x:integer;
- begin
- for i:=1 to n-1 do
- begin
- min:=i;
- x:=mas[min];
- for j:=i+1 to n do
- begin
- cmp1:=cmp1+1;
- if mas[j]<x then
- begin
- min:=j;
- x:=mas[min];
- trans1:=trans1+1;
- end;
- end;
- trans1:=trans1+1;
- mas[min]:=mas[i];
- mas[i]:=x;
- end;
- end;
- procedure QuickSort(var mas:TMyArr; L,R:integer; out cmp1,trans1:integer);
- var
- i,j,x,tmp:integer;
- begin
- i:=L;
- j:=R;
- x:=mas[(L+R) div 2];
- repeat
- while mas[i]<x do
- begin
- i:=i+1;
- cmp1:=cmp1+1;
- end;
- while mas[j]>x do
- begin
- j:=j-1;
- cmp1:=cmp1+1;
- end;
- if i<=j then
- begin
- tmp:=mas[i];
- mas[i]:=mas[j];
- mas[j]:=tmp;
- i:=i+1;
- j:=j-1;
- trans1:=trans1+1;
- end;
- until i>j;
- if L<j then QuickSort(mas, L, j,cmp1,trans1);
- if i<R then QuickSort(mas, i, R,cmp1,trans1);
- end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement