Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
#include<iostream>
// Bubblesort
void bubblesort(int A[], int n)
{
for (int i = 0; i < n; i++) {
for (int j = n - 1; j > 0; j--) {
if (A[j] < A[j - 1]) {
int tmp = A[j];
A[j] = A[j - 1];
A[j - 1] = tmp;
}
}
}
}
// MAIN FUNCTION
int main()
{
// Input
int n;
int A[100];
std::cin >> n;
for (int i = 0; i < n; i++) {
std::cin >> A[i];
}
// Bubble sort
bubblesort(A, n);
for (int i = 0; i < n; i++) {
if (i)
std::cout << " ";
std::cout << A[i];
}
std::cout << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment