You are on page 1of 32

TECNICAS ALGORITMICAS EN UNO O MAS

OBJETOS DE TIPO VECTOR.

UNIDAD IV
Ing. Mario M. Lpez Winnipeg

CONTENIDO
Cortes de control de 2 o mas niveles.
Dispersin Hash
Segmentacin.
Intercalacin
Purga.
Segmentacin ordenada.
Cortes de control ordenados.

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

CORTES DE CONTROL DE DOS


O MAS NIVELES.
Los cortes de control en vectores se realizan cuando
tenemos un vector donde cada una de sus celdas
esta constituido por mas de un campo ordenado.
DPT
O

PROV

MESA

VOTOS

SCZ

COTOCA

15

SCZ

WARNES

123

CBBA

CERCAD
O

223

CBBA

PUNATA

23

CBBA

PUNATA

345

LPZ

MURILLO

23

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

CORTES DE CONTROL DE DOS


O MAS NIVELES.
DPT
O

PROV

MES
A

VOTO
S

SCZ

COTOCA

15

SCZ

WARNES

123

CBBA

CERCAD
O

223

CBBA

PUNATA

23

CBBA

PUNATA

345

LPZ

MURILLO

23

CUADRO DE VOTACION POR DEPARTAMENTO


DPTO
PROV
MESA
VOTOS
SCZ
COTOCA
1
15
SCZ
WARNES
2
123
---------------------------------------------------------------------------------------Total Dpto.
138
CBBA
CERCADO
1
223
CBBA
PUNATA
2
23
CBBA
PUNATA
3
345
-------------------------------------------------------------------------------------Total Dpto
591
LPZ
MURILLO
1
23
---------------------------------------------------------------------------------------Total Dpto.
23

CUADRO DE VOTACION POR DEPARTAMENTO Y PROVINCIA


DPTO
PROV
MESA
VOTOS
SCZ
COTOCA
1
15
----------------------------------------------------------Total Prov.
15
SCZ
WARNES
2
123
-----------------------------------------------------------Total Prov.
123
---------------------------------------------------------------------------------------Total Dpto.
138
CBBA
CERCADO
1
223
-----------------------------------------------------------Total Prov.
223
CBBA
PUNATA
2
23
CBBA
PUNATA
3
345
-----------------------------------------------------------Total Prov.
368
---------------------------------------------------------------------------------------Total Dpto.
591
LPZ
MURILLO
1
23
-----------------------------------------------------------Total Prov.
23
---------------------------------------------------------------------------------------Total Dpto.
23

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
Crear un archivo texto con la informacin de la votacin
de los departamentos de Bolivia

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
Crear la Clase CorteControl con los siguientes atributos
votos array[1..100] of Tvoto, donde tvoto es un tipo de
dato definido de la siguiente forma:
Tvoto=record
dpto : string;
prov : string;
mesa: integer;
nro: integer;
End;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO (crear unidad)


unit UCCorteControl;
interface
Uses
SysUtils,StdCtrls;
Const
max = 100;
Type
Tvoto = Record
Dpto:string;
prov:String;
mesa:integer;
nro: integer;
End;

CEMCorteControl = Class(Exception);
CCorteControl = Class
Private
Votos: array [1..Max ] of TVoto;
N : Integer;
function llenarespacio(S:string;Tam:byte;tipo:char):String;
Public
Constructor Crear;
Procedure CargarVotos(NombreArchivo:String);
Procedure Mostrar(Pant:Tmemo);
End;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
implementation
function CCorteControl.llenarespacio(S:string;Tam:byte;tipo:char):String;
var i:integer;
begin
if tipo='D' then
for i:= length(s) to Tam do
// Llenando espacios a la
s:=s+' ';
// Derecha de S
if tipo='A' then
for i:= length(s) to Tam do
// Llenando espacios adelante
s:=' '+s;
// de S
if tipo='C' then
for i:= length(s) to Tam do
if odd(i) then s:=' '+s
// Llenando espacios a ambos
else s:=s+' ';
// lados de S
llenarEspacio:=S;
end;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO

Constructor CCorteControl.Crear;
begin
n:=0;
end;

Procedure CCorteControl.CargarVotos(NombreArchivo:String);
var F:TextFile;
R:Tvoto;
Linea:string;
begin
Assign(F,nombreArchivo);
reset(f);
R.prov:=copy(linea,1,pos(' ',linea)-1);
while not eof(f) do
delete(linea,1, pos(' ',linea));
begin
readln(f,Linea);
R.mesa:=StrToInt( copy(linea,1,pos(' ',linea)linea:=linea+' ';
1) );
R.dpto:=copy(linea,1,pos(' ',linea)-1);
delete(linea,1, pos(' ',linea));
delete(linea,1, pos(' ',linea));
R.nro:=StrToInt( copy(linea,1,pos(' ',linea)-1) );
delete(linea,1, pos(' ',linea));
n:=n+1;
votos [n]:=R;
end;
end;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
Procedure CCorteControl.Mostrar(Pant:Tmemo);
var linea:string;
i:integer;
begin
pant.Clear;
i:=1;
while i<=N do
Begin
linea:=LlenarEspacio(
votos[i].Dpto ,10 ,'D') +
LlenarEspacio(
votos[i].prov ,10 ,'D')+
LlenarEspacio(IntToStr(votos[i].mesa),5 ,'D') +
LlenarEspacio(IntToStr(votos[i].nro) ,6 ,'A');
pant.Lines.Append(linea);
i:=i +1;
end;
end;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
Public
AplCorte: CCorteContro;

begin
aplCorte:=ccortecontrol.crear;
end;
begin
AplCorte.cargarvotos('C:\Users\m
ario lopez\Desktop\votos.txt');
end;
begin
aplCorte.mostrar(memo1);
end;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO

begin
aplCorte.CorteUnNivel(memo1);
end;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO

while i<=N do
Begin
linea:=LlenarEspacio(votos[i].Dpto,10,'D') +
Procedure
LlenarEspacio(votos[i].prov ,10,'D')+
CCorteControl.CorteUnNivel(Pant:Tmemo);
LlenarEspacio(IntToStr(votos[i].mesa),5,'D') +
LlenarEspacio(IntToStr(votos[i].nro),6,'A');
var linea:string;
if CampoDpto=votos[i].Dpto then
i:integer;
Begin
CampoDpto:String;
pant.Lines.Append(linea);
totalDpto:=TotalDpto + votos[i].nro;
TotalDpto: integer;
end
begin
else
pant.Clear;
Begin
pant.Lines.Append(LlenarEspacio('-------------------',35,'A'));
i:=1;
pant.Lines.Append(LlenarEspacio('Total Dpto '+
TotalDpto:=0;
intToStr(TotalDpto),34,'A'));
CampoDpto:= votos[1].Dpto;
pant.Lines.Append(linea);
CampoDpto:=votos[i].Dpto;
totalDpto:=votos[i].nro;
End;
i:=i +1;
end;
pant.Lines.Append(LlenarEspacio('-------------------',35,'A'));
pant.Lines.Append(LlenarEspacio('Total Dpto '+intToStr(TotalDpto),34,'A'));
end;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO

begin
aplcorte.CorteDosNivel(memo1);
end;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

DISPERSION - HASH
Una tabla HASH es una estructura de datos que soporta
la eliminacin, recuperacin e insercin de elementos de
forma muy rpida.
Una funcin hash es una funcin que asigna
una clave a un ndice dentro del arreglo.
Este ndice indica donde esta almacenado el
elemento.

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

INSERCION
Ejemplo: insertar el valor Hanson en el vector A

Hanson

Hash ( Valor)

Hanson

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

INSERCION

Ultimo digito +1
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

INSERCION

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
CVectorHash = Class
Private
MaxIndex : Integer;
Elementos : Attay [ 1.. Max ] of String;
private
function hash( valor:String):Integer;
Public
Constructor Crear;
Procedure Poner( Elemento : Integer);
Function MaximoIndice : Word;
Function ComoString : String;
End;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
Function CvectorHash.Crear;
Begin
MaxIndex:=0;
End;

Procedure CVectorHash.ComoString:String;
Var i:integer;
s:string;
Begin
s:=;
For i:= 1 to maxindex do
if length(elementos[ i ] )>0 then
s:= s + elementos[ i ]+ , ;
comostring:=s;
End;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

Function CvectorHash.Hash( Valor: String ):integer;


Var
letra:char;
Snumero:string;
Begin
letra:= valor [ 2 ];
Str ( ord ( letra) , Snumero);
Hash:= StrToInt( snumero [ length(Snumero) ] );
End;

LABORATORIO

Procedure CVectorHash.Poner( Valor : String);


Var ndice:integer;
Begin
if elementos <= Max Then
Begin
ndice:=Hash( Valor);
Elementos[ ndice ] := Elemento;
if ndice > maxindex then maxindes := idice;
End;
End;
TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
Begin
Apl: CvectorHash.crear;
End;
Begin
Apl.poner( edit1.text );
End;
Begin
label1.caption:= apl.comostring;
End;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

SEGMENTACION
r
a
g
ti
s
e
v
in
r
o
p
a
e
r
Ta

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

PURGA
La purga consiste en eliminar los elementos repetidos del Vector
1

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

Procedure Cvector.Purga;
Var
Valor,i,j: integer;
Begin
i:=1;
While i<= N do
Begin
Valor:= Elementos[ i ];
j:= i+1;
while j<= N do
Begin
if elementos[j ] = valor then
Begin
for k:= j+1 to N do
elementos[ k -1 ]:= elementos[k-1];
n:=n-1;

PURGA

End;
j:=j+1;
// Verificar si esta lnea esta correcta.
End;
i:=i+1;
End;
End;
TECNICAS ALGORITMICAS
EN UNO O MAS OBJETOS DE TIPO VECTOR

INTERCALAR
1

10

10

20

20

30

30

40

40

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

INTERCALAR

Procedure Cvector.intercala( A, B : Cvector);;


Var
Valor,i,j: integer;
Begin
i:=1
k:=0;
While i<= a.dimensin and i<= b.diemnsion do
Begin
k:=k+1;
elementos[k]:=a.elemento(i);
k:=k+1;
elementos[k]:=b.elementos(i);
i:=i+1;
End;
if a.dimensin > b.dimensin then
// Vaciar el resto del vector A
else
// Vaciar el resto del vector B
End;

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

SEGMENTACION ORDENADA
r
a
g
ti
s
e
v
in
r
o
p
a
e
r
Ta

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

CORTE DE COTROL
ORDENADO
Los cortes de control ordenados se refieren
a la condicin que debe cumplir un vector
para poder aplicar el algoritmo de corte de
control. Caso contrario es necesario
ordenar el vector de acuerdo a los niveles
que se tengan que realizar los cortes de
control.

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

LABORATORIO
If votos[i].dpto. > votos[i+1].dpto. then
// Intercambiar registro
else
if votos[i].dpto.= votos[i+1].dpto. then
if votos[i].prov > votos[i +1].prov then
// Intercambiar Registro.

TECNICAS ALGORITMICAS EN UNO O MAS OBJETOS DE TIPO VECTOR

You might also like