Created
August 8, 2012 23:00
-
-
Save GianpaMX/3299580 to your computer and use it in GitHub Desktop.
C++ Bubble
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
#include <iostream> // para usar cout | |
#include <cstdlib> // para usar EXIT_SUCCESS | |
#include <vector> // para usar vectores (son como arrays) | |
// Para no tener que escribir std::cout, vector cada vez que se usen | |
using namespace std; | |
// Definición de funciones útiles | |
void printData(const vector<int> &data); | |
void swap(int *a, int *b); | |
// Definición de las funciones de ordenamiento | |
vector<int> bubble_for(const vector<int> &data); | |
vector<int> bubble_while(const vector<int> &data); | |
vector<int> bubble_do_while(const vector<int> &data); | |
/** | |
* Declara un array, crea un vector apartir de este, lo imprimir desordenado y luego lo ordena e imprime tres veces | |
*/ | |
int main(int argc, char **argv) { | |
int array[] = {91, 61, 64, 70, 84, 35, 55, 18, 6, 94, 16, 12}; | |
vector<int> data(array, array + sizeof(array) / sizeof(int)); | |
printData(data); | |
printData( bubble_for(data) ); | |
printData( bubble_while(data) ); | |
printData( bubble_do_while(data) ); | |
return EXIT_SUCCESS; | |
} | |
/** | |
* Imprime un vector entre corchetes y sus elementos separados por comas | |
**/ | |
void printData(const vector<int> &data) { | |
cout << "["; | |
for(unsigned int i = 0; i < data.size(); i++) { | |
cout << data[i] << (i+1==data.size()?"":", "); | |
} | |
cout << "]" << endl; | |
} | |
/** | |
* Intercambia los valores de dos variables | |
**/ | |
void swap(int *a, int *b) { | |
int c; | |
c = *a; | |
*a = *b; | |
*b = c; | |
} | |
/** | |
* Ordena por el método de la burbuja usando solo ciclos for | |
**/ | |
vector<int> bubble_for(const vector<int> &data) { | |
vector<int> result = data; | |
for(unsigned int i = 0; i < result.size(); i++) { | |
for(unsigned int j = 0; j < result.size()-1; j++) { | |
if(result[j] > result[j+1]) { | |
swap(&result[j], &result[j+1]); | |
} | |
} | |
} | |
return result; | |
} | |
/** | |
* Ordena por el método de la burbuja usando el ciclo while | |
**/ | |
vector<int> bubble_while(const vector<int> &data) { | |
vector<int> result = data; | |
bool swapped = true; | |
while(swapped) { | |
swapped = false; | |
for(unsigned int i = 0; i < result.size()-1; i++) { | |
if(result[i] > result[i+1]) { | |
swap(&result[i], &result[i+1]); | |
swapped = true; | |
} | |
} | |
} | |
return result; | |
} | |
/** | |
* Ordena por el método de la burbuja usando el ciclo do-while | |
**/ | |
vector<int> bubble_do_while(const vector<int> &data) { | |
vector<int> result = data; | |
bool swapped; | |
do { | |
swapped = false; | |
for(unsigned int i = 0; i < result.size()-1; i++) { | |
if(result[i] > result[i+1]) { | |
swap(&result[i], &result[i+1]); | |
swapped = true; | |
} | |
} | |
} while(swapped); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment