Skip to content

Instantly share code, notes, and snippets.

@DiracSpace
Created May 21, 2021 04:13
Show Gist options
  • Save DiracSpace/b2b6fbb2092650833fe4d6a87b34b80f to your computer and use it in GitHub Desktop.
Save DiracSpace/b2b6fbb2092650833fe4d6a87b34b80f to your computer and use it in GitHub Desktop.
#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