Bubble Sort
c:
/*
-Victor de la Rocha
-Algoritmia@groups.msn.com
-www.myalgorithm.com
-Jueves 10/AGO/05
-Programa 1, Practica 1
-Bubble Sort (Ordenamiento burbuja)
-Program that orders an adjustment with the arrangement algorithm bubble
-Programa que ordena un arreglo con el algoritmo de ordenacion burbuja
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int fillrand(int A[],int N,int R){
int c;
for(c=1;c<=N;c++){
A[c]=rand()%R;
}
return 1;
}
int bubblesort(int A[],int N){
int i,j,AUX;
for(i=2;i<=N;i++){
for(j=N;j>=i;j--){
if(A[j-1]>A[j]){
AUX=A[j-1];
A[j-1]=A[j];
A[j]=AUX;
}
}
}
return 1;
}
int salida(int A[],int N){
int c;
for(c=1;c<=N;c++){
printf("%d, ",A[c]);
}
return 1;
}
void main(){
int A[10];
randomize();
clrscr();
fillrand(A,10,100);
printf("BUBBLE SORT\n");
printf("Datos a ordenar: \n");
salida(A,10);
printf("\n\nDatos ordenados: \n");
bubblesort(A,10);
salida(A,10);
getch();
}
/*
BURBUJA(A,N)
{El algoritmo ordena los elementos del arreglo utilizando el método de la burbuja.
´Transporta en cada pasada el elemento más pequeño hacia la parte izquierda
del arreglo. A es un arreglo de N elementos}
{I, J y AUX son variables de tipo entero}
1. Repetir con I desde 2 hasta N
1.1 Repetir con J desde N hasta I
1.1.1 Si A[J-1] > A[J] entonces
Hacer AUX <-- A[J-1]
A[J-1] <-- A[J]
A[J] <-- AUX
1.1.2 {Fin del condicional del paso 1.1.1}
1.2 {Fin del ciclo del paso 1.1}
2. {Fin del ciclo del paso 1}
*/
-Victor de la Rocha
-Algoritmia@groups.msn.com
-www.myalgorithm.com
-Jueves 10/AGO/05
-Programa 1, Practica 1
-Bubble Sort (Ordenamiento burbuja)
-Program that orders an adjustment with the arrangement algorithm bubble
-Programa que ordena un arreglo con el algoritmo de ordenacion burbuja
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int fillrand(int A[],int N,int R){
int c;
for(c=1;c<=N;c++){
A[c]=rand()%R;
}
return 1;
}
int bubblesort(int A[],int N){
int i,j,AUX;
for(i=2;i<=N;i++){
for(j=N;j>=i;j--){
if(A[j-1]>A[j]){
AUX=A[j-1];
A[j-1]=A[j];
A[j]=AUX;
}
}
}
return 1;
}
int salida(int A[],int N){
int c;
for(c=1;c<=N;c++){
printf("%d, ",A[c]);
}
return 1;
}
void main(){
int A[10];
randomize();
clrscr();
fillrand(A,10,100);
printf("BUBBLE SORT\n");
printf("Datos a ordenar: \n");
salida(A,10);
printf("\n\nDatos ordenados: \n");
bubblesort(A,10);
salida(A,10);
getch();
}
/*
BURBUJA(A,N)
{El algoritmo ordena los elementos del arreglo utilizando el método de la burbuja.
´Transporta en cada pasada el elemento más pequeño hacia la parte izquierda
del arreglo. A es un arreglo de N elementos}
{I, J y AUX son variables de tipo entero}
1. Repetir con I desde 2 hasta N
1.1 Repetir con J desde N hasta I
1.1.1 Si A[J-1] > A[J] entonces
Hacer AUX <-- A[J-1]
A[J-1] <-- A[J]
A[J] <-- AUX
1.1.2 {Fin del condicional del paso 1.1.1}
1.2 {Fin del ciclo del paso 1.1}
2. {Fin del ciclo del paso 1}
*/
Arreglo original 140, 10, 5, 9, 16, 8, 0, 1, 52 Arreglo ordenado 0, 1, 5, 8, 9, 10, 16, 52, 140