You are on page 1of 91

Métodos de

Ordenación

Algoritmos y Estructuras de Datos I


ASC – LSI - 2014
Ordenación por Selección
Valor 15 0 23 -5 9
Posición 1 2 3 4 5
begin
H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
For I := 2 to N – H do
begin
If (vec[I] > max) then
begin
max := vec[I];
pos := I;
end;
end;
vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;
end;
Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J If (vec[I] > max) then
I begin
H 0 max := vec[I];
pos := I;
MAX end;
POS end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I begin
H 0 max := vec[I];
pos := I;
MAX end;
POS end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I begin
H 0 max := vec[I];
pos := I;
MAX 15 end;
POS end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I begin
H 0 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 2 begin
H 0 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

IF ( 0 > 15) H := 0;
FALSE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 2 begin
H 0 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

IF (23 > 15) H := 0;


TRUE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 3 begin
H 0 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 3 begin
H 0 max := vec[I];
pos := I;
MAX 23 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 3 begin
H 0 max := vec[I];
pos := I;
MAX 23 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

IF (-5 > 23) H := 0;


FALSE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 4 begin
H 0 max := vec[I];
pos := I;
MAX 23 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

IF (9 > 23) H := 0;
FALSE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 5 begin
H 0 max := vec[I];
pos := I;
MAX 23 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 9
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 4 begin
H 0 max := vec[I];
pos := I;
MAX 23 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 4 begin
H 0 max := vec[I];
pos := I;
MAX 23 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 1 If (vec[I] > max) then
I 4 begin
H 1 max := vec[I];
pos := I;
MAX 23 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 4 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 4 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 2 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

IF (0 > 15) H := 0;
FALSE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 2 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

IF (9 > 15) H := 0;
FALSE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 3 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor 15 0 9 -5 23
Posición 1 2 3 4 5

IF (-5 > 15) H := 0;


FALSE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 4 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 -5 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 4 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 4 begin
H 1 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 2 If (vec[I] > max) then
I 4 begin
H 2 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 4 begin
H 2 max := vec[I];
pos := I;
MAX 15 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 4 begin
H 2 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 4 begin
H 2 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 2 begin
H 2 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

IF (0 > -5) H := 0;
TRUE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 2 begin
H 2 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 2 begin
H 2 max := vec[I];
pos := I;
MAX 0 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 2 begin
H 2 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

IF (9 > 0) H := 0;
TRUE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 3 begin
H 2 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 3 begin
H 2 max := vec[I];
pos := I;
MAX 9 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 3 begin
H 2 max := vec[I];
pos := I;
MAX 9 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 3 begin
H 2 max := vec[I];
pos := I;
MAX 9 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 3 begin
H 2 max := vec[I];
pos := I;
MAX 9 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 3 If (vec[I] > max) then
I 3 begin
H 3 max := vec[I];
pos := I;
MAX 9 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 3 begin
H 3 max := vec[I];
pos := I;
MAX 9 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 3 begin
H 3 max := vec[I];
pos := I;
MAX -5 end;
POS 3 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 3 begin
H 3 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 3 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

IF (0 > -5) H := 0;
TRUE For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 3 max := vec[I];
pos := I;
MAX -5 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 3 max := vec[I];
pos := I;
MAX 0 end;
POS 1 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 3 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 3 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 3 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 4 If (vec[I] > max) then
I 2 begin
H 4 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


Valor -5 0 9 15 23
Posición 1 2 3 4 5

H := 0;
For J := 1 to N – 1 do
begin
max := vec[1];
pos := 1;
Variables For I := 2 to N – H do
begin
J 5 If (vec[I] > max) then
I 2 begin
H 4 max := vec[I];
pos := I;
MAX 0 end;
POS 2 end;
N 5 vec[pos] := vec[N – H];
vec[N – H] := max;
H := H + 1;
end;

Ordenación por Selección


VECTOR ORDENADO:

Valor -5 0 9 15 23
Posición 1 2 3 4 5

Ordenación por Selección


El algoritmo de ordenación por selección de un vector
de n elementos tiene los siguientes pasos:

1. Encontrar el elemento mayor del vector.

2. Intercambiar el elemento mayor con el elemento de


subíndice n.

3. A continuación se busca el elemento mayor en la


parte del vector de subíndices l. .. n - l, y
se intercambia con el elemento de subíndice n - l;
por consiguiente, se sitúa el segundo elemento mayor
en la posición n - l.

4. A continuación se busca el elemento mayor en la


parte del vector l. .. n - 2, y así sucesivamente.

Ordenación por Selección


Ordenación por Burbuja
Valor 15 0 23 -5 9
Posición 1 2 3 4 5

begin
for I := 1 to N - 1 do
begin
for J := 1 to N - I do
If (vec[J] > vec[J+1]) then
begin
aux := vec[J];
vec[J] := vec[J+1];
vec[J+1] := aux
end;
end;
end;
Valor 15 0 23 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J vec[J+1] := aux
AUX end;
end;
N 5 end;

Ordenación por Burbuja


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX end;
end;
N 5 end;

Ordenación por Burbuja


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

IF (15 > 0) For I := 1 to N – 1 do


TRUE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX end;
end;
N 5 end;

Ordenación por Burbuja


Valor 15 0 23 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 0 23 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 23 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 23 -5 9
Posición 1 2 3 4 5

IF (15 > 23) For I := 1 to N – 1 do


FALSE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 23 -5 9
Posición 1 2 3 4 5

IF (23 > -5) For I := 1 to N – 1 do


TRUE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 23 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 -5 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 23 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 23 9
Posición 1 2 3 4 5

IF (23 > 9) For I := 1 to N – 1 do


TRUE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 4 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 23 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 4 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 9
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 4 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 1 vec[J] := vec[J+1];
J 4 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 4 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 23
Posición 1 2 3 4 5

IF (0 > 15) For I := 1 to N – 1 do


FALSE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 23
Posición 1 2 3 4 5

IF (15 > -5) For I := 1 to N – 1 do


TRUE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 15 -5 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 -5 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 15 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 15 9 23
Posición 1 2 3 4 5

IF (15 > 9) For I := 1 to N – 1 do


TRUE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 15 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 9 9 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 2 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 15 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 3 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 9 15 23
Posición 1 2 3 4 5

IF (0 > -5) For I := 1 to N – 1 do


TRUE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor 0 -5 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 0 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 -5 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 0 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 0 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 0 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 0 9 15 23
Posición 1 2 3 4 5

IF (0 > 9) For I := 1 to N – 1 do
FALSE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 3 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 0 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 4 vec[J] := vec[J+1];
J 2 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 0 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 4 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 0 9 15 23
Posición 1 2 3 4 5

IF (-5 > 0) For I := 1 to N – 1 do


FALSE begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 4 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


Valor -5 0 9 15 23
Posición 1 2 3 4 5

For I := 1 to N – 1 do
begin
For J := 1 to N – I do
begin
If (vec[J] > vec[J+1]) then
Variables begin
aux := vec[J];
I 4 vec[J] := vec[J+1];
J 1 vec[J+1] := aux
AUX 23 end;
end;
N 5 end;

Ordenación por Burbuja


VECTOR ORDENADO:

Valor -5 0 9 15 23
Posición 1 2 3 4 5

Ordenación por Burbuja


El algoritmo de ordenación por burbuja de un vector
de n elementos tiene los siguientes pasos:

1. Recorre el vector n – 1 veces.

2. En cada recorrido se encarga de comparar elementos


consecutivos, moviendo aquel de mayor valor hacia
«atrás» en el vector. Es decir, lo deja en la
posición n.

3. A continuación se vuelve a recorrer el vector de


ignorando la última posición, y mueve el segundo
elemento mayor en la posición n - l.

4. El proceso se repite en cada recorrido general


disminuyendo en cada uno la cantidad de elementos a
analizar, disminuyendo el tiempo de proceso.

Ordenación por Burbuja

You might also like