Created
May 21, 2021 04:13
-
-
Save DiracSpace/b2b6fbb2092650833fe4d6a87b34b80f to your computer and use it in GitHub Desktop.
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> | |
using namespace std; | |
/* | |
Ordenamiento por el algoritmo Burbuja en C++ | |
Autor: Jayson Roberto De León Martínez | |
*/ | |
void cambio(int &a, int &b) | |
{ | |
int temp; | |
temp = a; | |
a = b; | |
b = temp; | |
} | |
void muestra(int *array, int size) | |
{ | |
for(int i = 0; i < size; i++) | |
cout << array[i] << " "; | |
cout << endl; | |
} | |
void bubbleSort(int *array, int size) | |
{ | |
for(int i = 0; i<size; i++) | |
{ | |
int swaps = 0; | |
for(int j = 0; j < size - i - 1; j++) | |
{ | |
if(array[j] > array[j+1]) | |
{ | |
cambio(array[j], array[j+1]); | |
swaps = 1; | |
} | |
} | |
if(!swaps) | |
break; | |
} | |
} | |
int main() { | |
int n; | |
cout << "Numero de elementos: "; | |
cin >> n; | |
int arr[n]; | |
cout << "Ingrese los datos:" << endl; | |
for(int i = 0; i < n; i++) | |
cin >> arr[i]; | |
cout << "Antes: "; | |
muestra(arr, n); | |
bubbleSort(arr, n); | |
cout << "Bubblesort: "; | |
muestra(arr, n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment