Subido por LULU CONTRERAS

Análisis Algoritmos:Burbuja,Inserción,Sort

Anuncio
Analisis de Algoritmos
Practica 1.
1. Metodos de ordenamiento
El siguiente programa en C,muestra los 3 algoritmos de ordenamiento implementados, los
cuales ordenan un vector de tamaño n, y con numero enteros aleatorios.
#include<stdio.h>
#include<time.h>
void sort(int A[], int tamSort);
void burbuja(int B[],int tamBurb);
void insercion(int C[], int tamInser);
void copiaArray(int arrayIni[],int arrayTemp[],int tamInter); //copea lo que hay en "arrayIni" en
"arrayTemp"
void imprimeArray(int D[],int tamImpr);
int main(){
srand(time(NULL));
int tam=(rand()%20)+2; //genera tamaño aleatorio
int array[tam],array2[tam];;
int i;
for(i=0;i<tam;i++){ //llena array
array[i]=(rand()%60)+10;} //de manera aleatoria
copiaArray(array2,array,tam);
imprimeArray(array,tam);printf("\tARREGLO INICIAL\n");
insercion(array,tam);
imprimeArray(array,tam);printf("\tDESPUES DE INSERCION\n");
copiaArray(array,array2,tam);
imprimeArray(array,tam);printf("\tARREGLO INICIAL\n");
sort(array,tam);
imprimeArray(array,tam);printf("\tDESPUES DE SORT\n");
copiaArray(array,array2,tam);
imprimeArray(array,tam);printf("\tARREGLO INICIAL\n");
burbuja(array,tam);
imprimeArray(array,tam);printf("\tDESPUES DE BURBUJA\n");
return 0;
}
void copiaArray(int arrayIni[],int arrayTemp[],int tamInter){
int i;
for(i=0;i<tamInter;i++){
arrayIni[i]=arrayTemp[i];
}
}
void imprimeArray(int D[],int tamImpr){
int i;
for(i=0;i<tamImpr;i++){ //imprime el array
printf("%i ",D[i]);}
}
//METODO DE SORT
void sort (int A[], int tamSort) {
int min,i,j,aux;
for (i=0; i<tamSort-1; i++)
{
min=i;
for(j=i+1; j<tamSort; j++)
if(A[min] > A[ j])
min=j;
aux=A[min];
A[min]=A[i];
A[i]=aux ;
}
}
//METODO DE INSERCION
void insercion(int C[], int tamInser){
int i, j, temp;
for(i=0; i<tamInser; i++){
temp=C[i];
j=i-1;
while(j>=0 && C[ j] >temp){
C[ j+1] = C[ j];
j--;
}
}
C[ j+1] = temp;
}
//METODO DE LA BURBUJA
void burbuja(int B[],int tamBurb){
int i,j,aux;
for(i=1; i<tamBurb; i++){
for(j=0; j<tamBurb-i; j++){
if(B[ j]>B[ j+1]){
aux= B[ j];
B[ j] = B[ j+1];
B[ j+1]= aux;
}
}
}
}
Se genero un vector de tamaño N, con numeros aleatorios. Y se muestra el vector antes de ser
ordenadoEl primer metodo en ser llamado es el de Inserccion. Este ordena los elementos y se muestran
los elementos ordenados.Posteriormente se vuelve a mostrar el vector original sin ordenar
El segundo metodo en ser llamado es el de Sort. Este ordena los elementos y se muestran los
elementos ordenados.Posteriormente se vuelve a mostrar el vector original sin ordenar
El tercer metodo en ser llamado es el de la Burbuja. Este ordena los elementos y se muestran
los elementos ordenados
2. Tiempo de Ejecucion
El siguiente codigo en C, crea un archivo de texto que contiene, una tabla del tiempo en
segundos que tarda cada algoritmo para ordenar un numero N=1000 de datos.
#include<stdio.h>
#include<time.h>
void sort (int A[], int tamSort);
void burbuja(int B[],int tamBurb);
void insercion(int C[], int tamInser);
void copiaArray(int arrayIni[],int arrayTemp[],int tamInter);
void imprimeArray(int D[],int tamImpr);
int main(){
FILE *mifile;
char* namefile="DataTable.txt";
mifile=fopen(namefile,"w");
if(mifile==NULL){return -1;}
clock_t tiSort,tfSort,tiBur,tfBur,ti_1,tf_1;
double timeSort,timeBurb,timeInser;
srand(time(NULL));
int tam,i,n;
fprintf (mifile, "%s %c", "Tamanio",'\t');
fprintf (mifile, "%s %c", "Insercion",'\t');
fprintf (mifile, "%s %c %c","Sort",'\t','\t');
fprintf (mifile, "%s %c", "Burbuja",'\t');
fprintf (mifile, "%c", '\n');
for(n=10;n<=1000;n=n+10){
tam=n;
int array[tam],array2[tam];
for(i=0;i<tam;i++){ //llena array
array[i]=(rand()%300)+10;}
copiaArray(array2,array,tam);
ti_1=clock();//INSERCION
insercion(array,tam);
tf_1=clock();
timeInser=((double)(tf_1-ti_1)/(double)CLOCKS_PER_SEC); //
(double)CLOCKS_PER_SEC)
copiaArray(array,array2,tam);
tiSort=clock();//SORT
sort(array,tam);
tfSort=clock();
timeSort=((double)(tfSort-tiSort)/(double)CLOCKS_PER_SEC);
copiaArray(array,array2,tam);
tiBur=clock();//BURBUJA
burbuja(array,tam);
tfBur=clock();
timeBurb=((double)(tfBur-tiBur)/(double)CLOCKS_PER_SEC);
fprintf (mifile, "%d %c %c", tam, '\t','\t');
fprintf (mifile, "%lf %c", timeInser, '\t');
fprintf (mifile, "%lf %c", timeSort, '\t');
fprintf (mifile, "%lf %c", timeBurb, '\n');
}
}
printf("Datos guardados en: Data1.txt");
return 0;
void copiaArray(int arrayIni[],int arrayTemp[],int tamInter){
int i;
for(i=0;i<tamInter;i++){
arrayIni[i]=arrayTemp[i];
}
}
void imprimeArray(int D[],int tamImpr){
int i;
for(i=0;i<tamImpr;i++){ //imprime el array
printf("array[%i]:%i\n",i,D[i]);}
}
void sort(int A[], int tamSort) {
int min,i,j,aux;
for (i=0; i<tamSort-1; i++)
{
min=i;
for(j=i+1; j<tamSort; j++)
if(A[min] > A[ j])
min=j;
aux=A[min];
A[min]=A[i];
A[i]=aux ;
}
}
void burbuja(int B[],int tamBurb){
int i,j,aux;
for(i=1; i<tamBurb; i++){
for(j=0; j<tamBurb-i; j++){
if(B[ j]>B[ j+1]){
aux= B[ j];
B[ j] = B[ j+1];
B[ j+1]= aux;
}
}
}
}
void insercion(int C[], int tamInser){
int i, j, temp;
for(i=0; i<tamInser; i++){
temp=C[i];
j=i-1;
while(j>=0 && C[ j] >temp){
C[ j+1] = C[ j];
j--;
}
}
C[ j+1] = temp;
}
Se genera un archivo de texto plano de la siguiente forma, en la misma ruta que esta el
archivo.c
Tamanio
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
Insercion
0.000002
0.000002
0.000004
0.000006
0.000007
0.000010
0.000012
0.000015
0.000021
0.000021
0.000027
0.000032
0.000037
0.000043
0.000051
0.000054
0.000065
0.000067
0.000075
0.000079
0.000094
0.000105
0.000113
0.000120
0.000125
Sort
0.000002
0.000003
0.000006
0.000008
0.000011
0.000016
0.000020
0.000025
0.000031
0.000038
0.000044
0.000051
0.000060
0.000068
0.000076
0.000086
0.000096
0.000107
0.000118
0.000131
0.000143
0.000166
0.000169
0.000183
0.000194
Burbuja
0.000001
0.000004
0.000009
0.000015
0.000020
0.000027
0.000035
0.000052
0.000061
0.000080
0.000106
0.000108
0.000126
0.000159
0.000167
0.000189
0.000247
0.000250
0.000267
0.000308
0.000352
0.000380
0.000432
0.000453
0.000490
3. Grafica de los metodos de ordenamiento
El siguiente algoritmo genera nuevamente la tabla de tiempos, pero sin los encabezados
#include<stdio.h>
#include<time.h>
void sort (int A[], int tamSort);
void burbuja(int B[],int tamBurb);
void insercion(int C[], int tamInser);
void copiaArray(int arrayIni[],int arrayTemp[],int tamInter);
void imprimeArray(int D[],int tamImpr);
int main(){
FILE *mifile;
char* namefile="Data1.txt";
mifile=fopen(namefile,"w");
if(mifile==NULL){return -1;}
clock_t tiSort,tfSort,tiBur,tfBur,ti_1,tf_1; //tiInse,tfInse;
double timeSort,timeBurb,timeInser;
srand(time(NULL));
int tam,i,n;
for(n=10;n<=1000;n=n+10){
tam=n;
int array[tam],array2[tam];
for(i=0;i<tam;i++){ //llena array
array[i]=(rand()%300)+10;}
copiaArray(array2,array,tam);
ti_1=clock();//INSERCION
insercion(array,tam);
tf_1=clock();
timeInser=((double)(tf_1-ti_1)/(double)CLOCKS_PER_SEC); //
(double)CLOCKS_PER_SEC)
copiaArray(array,array2,tam);
tiSort=clock();//SORT
sort(array,tam);
tfSort=clock();
timeSort=((double)(tfSort-tiSort)/(double)CLOCKS_PER_SEC);
copiaArray(array,array2,tam);
tiBur=clock();//BURBUJA
burbuja(array,tam);
tfBur=clock();
timeBurb=((double)(tfBur-tiBur)/(double)CLOCKS_PER_SEC);
fprintf (mifile, "%d %c", tam, '\t');
fprintf (mifile, "%lf %c", timeInser, '\t');
fprintf (mifile, "%lf %c", timeSort, '\t');
fprintf (mifile, "%lf %c", timeBurb, '\n');
}
printf("Se genero el archivo de texto Data1.txt");
return 0;
}
void copiaArray(int arrayIni[],int arrayTemp[],int tamInter){
int i;
for(i=0;i<tamInter;i++){
arrayIni[i]=arrayTemp[i];
}
}
void imprimeArray(int D[],int tamImpr){
int i;
for(i=0;i<tamImpr;i++){ //imprime el array
printf("array[%i]:%i\n",i,D[i]);}
}
void sort(int A[], int tamSort) {
int min,i,j,aux;
for (i=0; i<tamSort-1; i++)
{
min=i;
for(j=i+1; j<tamSort; j++)
if(A[min] > A[ j])
min=j;
aux=A[min];
A[min]=A[i];
A[i]=aux ;
}
}
void burbuja(int B[],int tamBurb){
int i,j,aux;
for(i=1; i<tamBurb; i++){
for(j=0; j<tamBurb-i; j++){
if(B[ j]>B[ j+1]){
aux= B[ j];
B[ j] = B[ j+1];
B[ j+1]= aux;
}
}
}
}
void insercion(int C[], int tamInser){
int i, j, temp;
for(i=0; i<tamInser; i++){
temp=C[i];
j=i-1;
while(j>=0 && C[ j] >temp){
C[ j+1] = C[ j];
j--;
}
C[ j+1] = temp;
}
}
Se genera el archivo “Data1.txt” en la misma direccion del archivo.c , de la siguiente manera:
10
20
30
40
50
60
70
80
90
100
110
120
130
140
150
160
170
180
190
200
210
220
230
240
250
0.000002
0.000002
0.000003
0.000004
0.000006
0.000007
0.000010
0.000013
0.000017
0.000021
0.000025
0.000030
0.000034
0.000036
0.000041
0.000052
0.000053
0.000057
0.000066
0.000075
0.000086
0.000095
0.000098
0.000112
0.000117
0.000002
0.000002
0.000005
0.000006
0.000009
0.000013
0.000017
0.000020
0.000026
0.000032
0.000037
0.000044
0.000050
0.000059
0.000079
0.000074
0.000083
0.000091
0.000101
0.000113
0.000124
0.000137
0.000147
0.000158
0.000174
0.000002
0.000003
0.000007
0.000010
0.000016
0.000020
0.000028
0.000061
0.000052
0.000064
0.000072
0.000088
0.000095
0.000116
0.000133
0.000157
0.000170
0.000199
0.000219
0.000252
0.000280
0.000337
0.000340
0.000381
0.000382
260
270
280
290
300
310
320
330
340
0.000115
0.000128
0.000139
0.000140
0.000166
0.000174
0.000202
0.000187
0.000225
0.000180
0.000192
0.000231
0.000222
0.000240
0.000254
0.000286
0.000289
0.000299
0.000403
0.000436
0.000466
0.000501
0.000554
0.000600
0.000621
0.000678
0.000696
Una vez generado el archivo de texto plano, se crea en Octave el siguiente archivo con
extension “.m”:
M=load('Data1.txt');
%M(2,:) all elements de la fla 2
%M(:,1) all elemnt de la columna 1
x=M(:,1); %tamanio
y=M(:,2); %columna inserccion
z=M(:,3); %columna sort
w=M(:,4); %columna burbuja
plot(y,x,'k')
xlabel('Tiempo(s)'),ylabel('Tamanio(datos)');
hold on
plot(z,x,'r')
hold on
plot(w,x,'g')
El cual carga el archivo “Data1.txt” para poder graficarlo.
Se ejecuta el archivo.m y muestra lo siguiente
Descargar