Skip to content

Instantly share code, notes, and snippets.

@porimol
Created May 30, 2017 07:18
Show Gist options
  • Save porimol/cf1cc3d347ecc51405e48adba7119bb6 to your computer and use it in GitHub Desktop.
Save porimol/cf1cc3d347ecc51405e48adba7119bb6 to your computer and use it in GitHub Desktop.
Bubble Sort Algorithm Implementation Using C++
/**
* @file bubble_sort.cpp
* @author Porimol Chandro, CSE 32D, World University of Bangladesh(WUB)
* @date 30/05/2017
*
* @brief Bubble Sort Algorithm Implementation.
*/
#include <iostream>
using namespace std;
int main()
{
int data[100], n, i, j, temp;
bool swapped = false;
cout << "Please enter the size of an array: ";
cin >> n;
for(i = 0; i<n; i++){
cout << "Please enter the value of index[" <<i <<"]: ";
cin >>data[i];
}
for(i = 0; i<n-1; i++){
for(j = 0; j<n-1-i; j++){
/* compare the adjacent elements */
if(data[j] > data[j+1]){
/* swapping */
temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
swapped = true;
}
}
if(!swapped)
break;
}
for(i = 0; i<n; i++){
cout << "The value of index[" <<i <<"] is " <<data[i] <<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment