You are on page 1of 31

Estructura de datos y algoritmos

Unidad 05 – Recursividad y
Ordenamiento

Profesores
•Alfredo Granda
•Juan Ramírez
Unidad 05 - Ordenamiento Simple
Objetivos

• Definición
• Ordenamiento por intercambio
• Burbuja
• Burbuja Modificada
• Ordenamiento por selección
• Ordenamiento por inserción
Definición
Ordenamiento
• El ordenamiento de los datos consiste en disponer un
conjunto de datos (o una estructura) en algún
determinado orden con respecto a alguno de sus
campos.
▫ Clave: Campo por el cual se ordena.

• Según donde estén almacenados los datos a ordenar,


podemos decir que el ordenamiento es:
▫ Interna: Arrays, listas o árbol. Típicamente en RAM.
▫ Externa: En Archivos en discos.
Orden
• Una lista de datos está ordenada por la clave k si la lista está en
orden con respecto a la clave anterior.

• Este Orden puede ser:


▫ Ascendente: (i<j) entonces (k[i]<=k[j])
▫ Descendente: (i>j) entonces (k[i]>=k[j])

• Hay Numerosos Métodos de Ordenamiento que difieren en


eficiencia.
▫ Análisis de algoritmo orientado a las comparaciones realizadas por
cada uno.
▫ Las comparaciones serán función de “n”. Siendo “n” el tamaño del
vector a Ordenar.
Clasificación de Métodos de Ordenamiento
• Analizaremos los siguientes métodos:
▫ Básicos: Son eficaces en Listas pequeñas
 Burbuja e Intercambio: simple pero Ineficiente.
 Selección e inserción: Recomendados.

▫ Avanzados: Son eficaces en Listas grandes.


 Merge Sort: muy extendido
 Quick Sort: muy extendido
 Shell Sort: muy extendido
 Counting Sort: el mas rápido para números
Ordenamiento por intercambio
Ordenamiento por Intercambio
(Conocido como Burbuja)
• El más sencillo de todos. Se basa en:
▫ La lectura sucesiva de la lista a ordenar,
▫ Comparando el elemento inferior de la lista con todos
los restantes
▫ Efectuando el Intercambio de posiciones cuando el
orden resultante no sea correcto.

• Siendo n la cantidad de elementos, Realizará al


menos n–1 pasadas.
Ejemplo - Ordenamiento por Intercambio
Pasada 1: Se compara a[1] con todos, así primero se cambia a[1] con a[2] pues a[1] >
a[2] y debe ser Ascendente, es decir a[1]<a[2] …y por utimo a[1] con a[4]

a[1] a[2] a[3] a[4] a[1] a[2] a[3] a[4] a[1] a[2] a[3] a[4]
8 4 6 2 4 8 6 2 2 8 6 4

Pasada 2: El elemento mas pequeño esta en a[1] y se analiza la sublista restante.


Al cabo de la pasada, el segundo mas chico esta en a[2]….

Pasada 3:
a[1] a[2] a[3] a[4] a[1] a[2] a[3] a[4]
2 4 8 6 2 4 6 8

Pasada i: Al cabo de la pasada i, el elemento de orden i, está en a[i]


Algoritmo: Ordenamiento por
Intercambio
Algoritmo ordIntercambio (a[], n)
i, k, aux: enteros /* se realizan n-1 pasadas */

para i desde 1 hasta n-1 hacer


para k desde i+1 hasta n hacer
si a[i] > a[k] entonces
aux  a[i]
a[i]  a[k]; Complejidad (n)(n–1)
a[k]  aux ; Del Orden F(n)=n .
2

Mejor Caso = O(n22)


Peor Caso = O(n22)
Código en C++
Ordenamiento por Intercambio
void ordIntercambio (int a[], int n)
{
for (int i=0; i < n-1; i++)
{
for (int k=i+1; k<n; k++)
{
if (a[i] > a[k])
{
int aux = a[i];
a[i] = a[k];
a[k] = aux;
}
}
}
}
Burbuja
Burbuja (Verdadera Burbuja)
• Los elementos burbujean:
▫ Los mas grandes, caen al fondo del arreglo (posición n)
▫ Los mas chicos suben a la cima (posición 1).

• Estudia parejas de elementos Adyacentes


▫ a[1] y a[2], a[2] y a[3]…a[i] y a[i+1]… a[n–1] y a[n].
▫ Si a[i+1] < a[i] Entonces Los INTERCAMBIA

• Algoritmo:
▫ Pasada 1: considera desde (a[1], a[2]) hasta (a[n–1], a[n]).
 En a[n] esta el elemento mas grande.
▫ Pasada 2: considera desde (a[1], a[2]) hasta (a[n–2], a[n–1]).
 En a[n–1] esta el segundo elemento mas grande.
▫ Pasada i: considera desde (a[1], a[2]) hasta (a[n–i], a[n-i+1]).
 En a[n–i+1] esta el elemento de orden i.
▫ El proceso termina con la pasada n–1
 El elemento mas pequeño esta en a[1].
Burbuja
Pasada 1: Pasada 2: Pasada 3:
a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5]
50 20 40 80 30 20 40 50 30 80 20 40 30 50 80

a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5]
20 50 40 80 30 20 40 50 30 80 20 40 30 50 80

a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5]
20 40 50 80 30 20 40 50 30 80 20 30 40 50 80

a[1] a[2] a[3] a[4] a[5] a[1] a[2] a[3] a[4] a[5]
20 40 50 80 30 20 40 30 50 80

a[1] a[2] a[3] a[4] a[5]


20 40 50 30 80
Algoritmo: Ordenamiento por Burbuja
Algoritmo ordBurbuja (a[], n)
i, k, aux: enteros

Repetir con i desde 1 hasta n-1


Repetir con k desde 1 hasta n-i
si (a[k] > a[k+1]) entonces
aux = a[k]
a[k] = a[k+1]
a[k+1] = aux

Mejor Caso = O(n22)


Caso Prom. = O(n22)
Peor Caso = O(n22)
Algoritmo: Ordenamiento por Burbuja
(Modificado)
Algoritmo ordBurbujaModificado (a[], n)
i, k, aux: enteros
ordenado: boolean

Repetir con i desde 1 hasta n-1


ordenado = true
Repetir con k desde 1 hasta n-i
si (a[k] > a[k+1]) entonces
aux = a[k]
a[k] = a[k+1]
a[k+1] = aux
ordenado = false Mejor Caso = O(n)
si (ordenado) break Caso Prom. = O(n22)
Peor Caso = O(n22)
Código en C++:
Ordenamiento por Burbuja (Modificado)
void ordBurbujaModificado (int a[], int n)
{
bool ordenado;
for (int i=0; i < n-1; i++)
{
ordenado = true;
for (int j=0; j < n - (i + 1); j++)
{
if (a[j] > a[j+1])
{
int aux = a[j];
a[j] = a[j+1];
a[j+1] = aux;
ordenado = false;
}
}
if (ordenado) break;
}
}
Ordenamiento por selección
Selection Sort
Ordenamiento Por Selección
• Realiza sucesivas pasadas que
▫ Buscan el elemento más pequeño de la lista a y lo escribe al
frente de la lista a[1].
▫ Considera las posiciones restantes, a[2]…a[n]
▫ Finaliza cuando ya no hay Posiciones Restantes.

• En la pasada i
▫ Está Ordenado: desde a[1] hasta a[i–1].
▫ Está Desordenado: Desde a[i] hasta a[n].

• El proceso continua n–1 vueltas.


Ejemplo: Ordenamiento por Selección
a[1] a[2] a[3] a[4]
Lista Original: 51 21 39 80

Pasada 1: Lista entre 1 y 4. Selecciona el menor (21) a[1] a[2] a[3] a[4]
y lo pasa al a[1] 21 51 39 80

a[1] a[2] a[3] a[4]


Pasada 2: Lista entre 2 y 4. Selecciona el menor (39)
y lo pasa al a[2] 21 39 51 80

a[1] a[2] a[3] a[4]


Pasada 3: Lista entre 3 y 4. No selecciona nada. 21 39 51 80
Algoritmo: Ordenamiento por Selección
Algortimo ordSeleccion (a[], n)
menor, k, j: enteros;
Repetir con i desde 1 hasta n-1
k = i /* comienzo de la exploración en índice i */
menor = a[i]
Repetir con j desde i+1 hasta n
si a[j] < menor entonces
menor = a[j]
Complejidad (n (n–1))/2
k=j Del Orden F(n)=n2.
a[k] = a[i]
Mejor Caso = O(n ) 2
2

a[i] = menor Caso Prom. = O(n ) 2


2

Peor Caso = O(n22)


Código en C++:
Ordenamiento por Selección
void ordSeleccion (int a[], int n)
{
int k, menor;
for (int i=0; i < n-1; i++)
{
k = i;
menor = a[i];
for (int j=i+1; j<n; j++)
{
if (a[j] < menor)
{
menor = a[j];
k = j;
}
}
a[k] = a[i];
a[i] = menor;
}
}
Ordenamiento por inserción
Ordenamiento Por Inserción
• Similar al proceso de ordenar tarjetas en un tarjetero por orden alfabético:
▫ Consiste en insertar un elemento en su posición correcta, dentro de una lista que ya
está Ordenada.

• Algoritmo:
▫ El 1er elemento a[1] se lo considera ordenado.

▫ Se inserta a[2] en la posición correcta, delante o detrás del a[1], según sea mayor o
menor.

▫ Por cada bucle i (desde i = 2 hasta n) se explora la sublista a[1]..a[i–1] buscando la


posición correcta de inserción del elemento a[i].

▫ Al dejar vacía la posición a[i] se impone un desplazamiento de todo el vector, desde el


lugar de inserción.
Ejemplo: Ordenamiento por Inserción
a[1] a[2] a[3] a[4]
Lista Original: 51 21 10 15

Pasada 1: Comenzamos en a[2] desplazando a la a[1] a[2] a[3] a[4]


derecha todos los valores que sean mayores a 21 (a[1]) 21 51 10 15

a[1] a[2] a[3] a[4]


Pasada 2: El 10 lo mueve hasta a[1]
10 21 51 15

a[1] a[2] a[3] a[4]


Pasada 3: El 15 lo mueve hasta a[2]. 10 15 21 51
Algoritmo: Ordenamiento por Inserción
Algoritmo ordInsercion (a[], n)
i, j, aux : enteros
Repetir con i desde 2 hasta n
aux = a[i]
k = i-1
Repetir mientras (k >= 1) y (aux < a[k])
a[k+1] = a[k]
Complejidad (n (n–1))/2
k = k-1 Del Orden F(n)=n2.
a[k+1] = aux Mejor Caso = O(n)
Caso Prom. = O(n+d)
Peor Caso = O(n22)
d es la cantidad de inversiones
Código en C++:
Ordenamiento por Inserción
void ordInsercion (int a[], int n)
{
int aux, k;
for (int i=1; i<n; i++)
{
aux = a[i];
k = i-1;
while (k >= 0 && aux < a[k])
{
a[k+1] = a[k];
k--;
}
a[k+1] = aux;
}
}
Ejercicios
Ejercicio 1 - UVA - 10327
Sorting in computer science is an important part. Almost every problem can be solved effeciently if sorted data are
found. There are some excellent sorting algorithm which has already acheived the lower bound nlgn. In this problem
we will also discuss about a new sorting approach. In this approach only one operation ( Flip ) is available and that is
you can exchange two adjacent terms. If you think a while, you will see that it is always possible to sort a set of
numbers in this way.

The Problem
A set of integers will be given. Now using the above approach we want to sort the numbers in ascending order. You
have to find out the minimum number of flips required. Such as to sort "1 2 3" we need no flip operation whether to
sort "2 3 1" we need at least 2 flip operations.

The Input
The input will start with a positive integer N ( N<=1000 ). In next few lines there will be N integers. Input will be
terminated by EOF.

The Output
For each data set print "Minimum exchange operations : M" where M is the minimum flip operations required to
perform sorting. Use a seperate line for each case.

Sample Input
3
1 2 3
3
2 3 1

Sample Output
Minimum exchange operations : 0
Minimum exchange operations : 2
Ejercicio 2 - UVA - 299
At
At an
an old
old railway
railway station,
station, you
you may
may still
still encounter
encounter one
one of
of the
the last
last remaining
remaining ``train
``train swappers''.
swappers''. A
A train
train swapper
swapper isis an
an employee
employee ofof the
the railroad,
railroad, whose
whose sole
sole job
job it
it is
is to
to
rearrange the carriages of trains.
rearrange the carriages of trains.
Once
Once the
the carriages
carriages are
are arranged
arranged inin the
the optimal
optimal order,
order, all
all the
the train
train driver
driver has
has to
to do,
do, is
is drop
drop the
the carriages
carriages off,
off, one
one by
by one,
one, at
at the
the stations
stations for
for which
which the
the load
load is
is meant.
meant.

The
The title
title ``train
``train swapper''
swapper'' stems
stems from
from the
the first
first person
person who
who performed
performed this
this task,
task, at
at aa station
station close
close to
to aa railway
railway bridge.
bridge. Instead
Instead of
of opening
opening up
up vertically,
vertically, the
the bridge
bridge
rotated around a pillar in the center of the river. After rotating the bridge 90 degrees, boats could pass left
rotated around a pillar in the center of the river. After rotating the bridge 90 degrees, boats could pass left or right.or right.
The
The first
first train
train swapper
swapper had
had discovered
discovered that
that the
the bridge
bridge could
could be
be operated
operated with
with at
at most
most two
two carriages
carriages on
on it.
it. By
By rotating
rotating the
the bridge
bridge 180
180 degrees,
degrees, the
the carriages
carriages switched
switched
place,
place, allowing
allowing himhim to
to rearrange
rearrange the
the carriages
carriages (as
(as aa side
side effect,
effect, the
the carriages
carriages then
then faced
faced the
the opposite
opposite direction,
direction, but
but train
train carriages
carriages can
can move
move either
either way,
way, so
so who
who
cares).
cares).

Now
Now that
that almost
almost all
all train
train swappers
swappers have
have died
died out,
out, the
the railway
railway company
company would
would like
like to
to automate
automate their
their operation.
operation. Part
Part of
of the
the program
program toto be
be developed,
developed, is
is aa routine
routine
which
which decides
decides for
for aa given
given train
train the
the least
least number
number of
of swaps
swaps of
of two
two adjacent
adjacent carriages
carriages necessary
necessary to
to order
order the
the train.
train. Your
Your assignment
assignment is
is to
to create
create that
that routine.
routine.

Input
Input Specification
Specification
The
The input
input contains
contains on
on the
the first
first line
line the
the number
number of
of test
test cases
cases (N).
(N). Each
Each test
test case
case consists
consists of
of two
two input
input lines.
lines. The
The first
first line
line of
of aa test
test case
case contains
contains an
an integer
integer L,
L,
determining
determining the length of the train ( ). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current order of
the length of the train ( ). The second line of a test case contains a permutation of the numbers 1 through L, indicating the current order of the
the
carriages.
carriages. The
The carriages
carriages should
should bebe ordered
ordered such
such that
that carriage
carriage 1
1 comes
comes first,
first, then
then 2,
2, etc.
etc. with
with carriage
carriage LL coming
coming last.
last.

Output
Output Specification
Specification
For
For each
each test
test case
case output
output the
the sentence:
sentence: 'Optimal
'Optimal train
train swapping
swapping takes
takes SS swaps.'
swaps.' where
where SS is
is an
an integer.
integer.

Example Input
3
3
1 3 2
4
4 3 2 1
2
2 1

Example Output
Optimal train swapping takes 1 swaps.
Optimal train swapping takes 6 swaps.
Optimal train swapping takes 1 swaps.
Ejercicio 3 - UVA - 11321
Hmm! Here you are asked to do a simple sorting. You will be given N numbers and a positive integer M. You will have to sort the N numbers in ascending order
of their modulo M value.

If
If there
there is
is aa tie
tie between
between an
an odd
odd number
number and
and an
an even
even number
number (that
(that is
is their
their modulo
modulo M
M value
value is
is the
the same)
same) then
then the
the odd
odd number
number will
will precede
precede the
the even
even number.
number.

If there is a tie between two odd numbers (that is their modulo M value is the same) then the larger odd number will precede the smaller odd number and if
there is a tie between two even numbers (that is their modulo M value is the same) then the smaller even number will precede the larger even number.

For
For remainder
remainder value
value of
of negative
negative numbers
numbers follow
follow the
the rule
rule of
of C
C programming
programming language:
language: A
A negative
negative number
number can
can never
never have
have modulus
modulus greater
greater than
than zero.
zero. E.g.
E.g. -100
-100
MOD 3 = -1, -100 MOD 4 = 0 etc.

Input
The input file contains 20 sets of inputs. Each set starts with two integers N (0<N<=10000) and M (0<M<=10000) which denotes how many numbers are
within this set. Each of the next N lines contains one number each. These numbers
should all fit in 32-bit signed integer. Input is terminated by a line containing two zeroes.

Output
For each set of input produce N+1 lines of outputs. The first line of each set contains the value of N and M. The next N lines contain N numbers, sorted
according to the rules mentioned above. Print the last two zeroes of the input file in the output file also.

Sample Input Sample Output


15 3 15 3
1 15
2 9
3 3
4 6
5 12
6 13
7 7
8 1
9 4
10 10
11 11
12 5
13 2
14 8
15 14
0 0 0 0

You might also like