Sei sulla pagina 1di 5

METODOS DE ORDENAMIENTO

Para poder ordenar una cantidad determinada de nmeros almacenadas en un vector o matriz, existen
distintos mtodos (algoritmos) con distintas caractersticas y complejidad.
Existe desde el mtodo ms simple, como el Bubblesort (o Mtodo Burbuja), que son Simples iteraciones,
hasta el Quicksort (Mtodo Rpido), que al estar optimizado usando recursin, su tiempo de ejecucin es
menor y es ms efectivo.
METODOS ITERATIVOS
Estos mtodos son simples de entender y de programar ya que son iterativos, simples ciclos y sentencias
que hacen que el vector pueda ser ordenado.
Dentro de los Algoritmos iterativos encontramos:

Burbuja

Insercin

Seleccin

Shellsort

METODOS RECURSIVOS

Estos mtodos son an ms complejos, requieren de mayor atencin y conocimiento para ser entendidos.
Son rpidos y efectivos, utilizan generalmente la tcnica Divide y vencers, que consiste en dividir un
problema grande en varios pequeos para que sea ms fcil resolverlos.
Mediante llamadas recursivas a s mismas, es posible que el tiempo de ejecucin y de ordenacin sea ms
ptimo.
Dent de los algoritmos recursivos encontramos:
Ordenamiento por Mezclas (merge)
Ordenamiento Rpido (quick)

METODO BURBUJA
public static void burbuja(int[]matrix){
int temp;
for(int i=1;i < matrix.length;i++){
for (int j=0 ; j < matrix.length- 1; j++){
if (matrix[j] > matrix[j+1]){
temp = matrix[j];

matrix[j] = matrix[j+1];
matrix[j+1] = temp;

METODO INSERCION
public static void Insercion (int[] vector) {
for (int i=1; i < vector.length; i++) {
int aux = vector[i];
int j;
for (j=i-1; j > =0 && vector[j] > aux; j--){
vector[j+1] = vector[j];
}
vector[j+1] = aux;

METODO SELECCIN
public static void Seleccion(int[]matrix){
int i, j, k, p, buffer, limit = matrix.length-1;
for(k = 0; k < limit; k++){
p = k;
for(i = k+1; i < = limit; i++){
if(matrix[i] < matrix[p]) p = i;
if(p != k){
buffer = matrix[p];
matrix[p] = matrix[k];
matrix[k] = buffer;

METODO SHELL SHORT


public static void shellSort(int[] matrix) {
for ( int increment = matrix.length / 2;increment > 0;
increment = (increment == 2 ? 1 : (int) Math.round(increment / 2.2))) {
for (int i = increment; i < matrix.length; i++) {

for (int j = i; j > = increment && matrix[j - increment] > matrix[j]; j = increment) {
int temp = matrix[j];
matrix[j] = matrix[j - increment];
matrix[j - increment] = temp;

ORDENAMIENTO POR MEZCLAS (MERGE)


El algoritmo de ordenamiento por mezcla (Merge) se divide en dos procesos, primero se divide en partes
iguales la lista:
public static void mergesort(int[ ] matrix, int init, int n){
int n1;
int n2;
if (n > 1){
n1 = n / 2;
n2 = n - n1;
mergesort(matrix, init, n1);
mergesort(matrix, init + n1, n2);
merge(matrix, init, n1, n2);
}
}

Y el algoritmo que nos permite mezclar los elementos segn corresponda:


private static void merge(int[ ] matrix, int init, int n1, int n2){
int[ ] buffer = new int[n1+n2];
int temp = 0;
int temp1 = 0;
int temp2 = 0;
int i;
while ((temp1 < n1) && (temp2 < n2)){
if (matrix[init + temp1] < matrix[init + n1 + temp2]){
buffer[temp++] = matrix[init + (temp1++)];
}else{

buffer[temp++] = matrix[init + n1 + (temp2++)];


}
}
while (temp1 < n1){
buffer[temp++] = matrix[init + (temp1++)];
}
while (temp2 < n2){
buffer[temp++] = matrix[init + n1 + (temp2++)];
}
for (i = 0; i < n1+n2; i++){
matrix[init + i] = buffer[i];
}
}

ORDENAMIENTO RAPIDO
view plainprint?

public static void Rapido(int matrix[], int a, int b){


matrix = new int[matrix.length];
int buf;
int from = a;
int to = b;
int pivot = matrix[(from+to)/2];
do{
while(matrix[from] < pivot){
from++;
}
while(matrix[to] > pivot){
to--;
}
if(from < = to){

buf = matrix[from];
matrix[from] = matrix[to];
matrix[to] = buf;
from++; to--;
}
}while(from < = to);
if(a < to){
Rapido(matrix, a, to);
}
if(from < b){
Rapido(matrix, from, b);
}
}

Potrebbero piacerti anche