Created
February 2, 2012 19:49
-
-
Save jjfajardo/1725371 to your computer and use it in GitHub Desktop.
Implementación del algoritmo de ordenación burbuja en C++.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Códigos correspondientes al trabajo realizado para el ISUM 2012. | |
* Test de rendimiento de los algoritmos de ordenamiento Quicksort, | |
* Mezcla y burbuja implementados en C++, Fortran y Python. | |
* Guanajuato, Guanajuato, México (14-16 de Marzo 2012) | |
* | |
* Programa: burbuja.cpp | |
* compilar: gcc -Wall -O burbuja.cpp -o burbuja | |
* Uso: $./burbuja 1000.dat | |
* El tamaño del array se toma del nombre del archivo (1000.dat) | |
* Salida: | |
* $ Tamaño_array Tiempo_de_ejecución_del_algoritmo | |
*/ | |
#include <iostream> | |
#include <fstream> | |
#include <cstdlib> | |
#include <ctime> | |
using namespace std; | |
void burbuja(int n, int *v) | |
{ | |
int i, j, aux; | |
int *arreg = v; | |
if (n < 2) return; | |
for (i = 0; i < n; i++) | |
for ( j = 0; j < n-1-i; j++) | |
if (arreg[j] > arreg[j+1]) | |
{ | |
aux = arreg[j]; | |
arreg[j] = arreg[j+1]; | |
arreg[j+1] = aux; | |
} | |
//Imprime vector ordenado | |
// for (int l = 0; l < n; l++) | |
// cout << arreg[l] << endl; | |
} | |
//=====================================// | |
int main(int argc, char *argv[]) | |
{ | |
int n; | |
clock_t ini, fin; | |
double total; | |
if(argc ==1) | |
{ | |
cout << "Al menos inserte un argumento" << endl; | |
return 0; | |
} | |
char numero[12]; | |
char* p, *q; | |
for(p = argv[1], q = numero; *p != '\0'; ++p) | |
{ | |
if(*p == '.') | |
{ | |
break; | |
} | |
else | |
{ | |
*q = *p; | |
++q; | |
} | |
} | |
*q = '\0'; | |
n = atol(numero); | |
int *v = new int[n]; | |
ifstream inFile; | |
inFile.open( argv[1] ); | |
if (!inFile) | |
{ | |
cout << "Al menos inserte un argumento" << endl; | |
return 0; | |
} | |
for (int k = 0; k < n; k++) | |
{ | |
inFile >> v[k]; | |
} | |
inFile.close(); | |
ini = clock(); | |
burbuja(n, v); | |
fin = clock(); | |
total=((double)(fin - ini)) / CLOCKS_PER_SEC; | |
printf ("%d \t",n); | |
printf ("%lf \n", total); | |
delete v; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment