Tuesday, February 21, 2012

C++ Code of Bubble Sort

Bubble sort, often incorrectly referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.


Here is the C++ implantation of Bubble sort

#include <iostream>

using namespace std;

int main()
{
    int i,j, n;
    cout<< "How much number you will sort?" << endl;
    cin>>n;
    int a[n];
    for(int x=1; x<=n;x++)
    {
        cout<< "Enter "<< " Number "  << x <<": "  << endl;
        cin>>a[x];

    }
for(int i=1; i<=n;i++)
    for(int j=i+1; j<=n;j++)
        {
        if (a[i]>a[j]){
            int temp;
            temp = a[i];
            a[i]=a[j];
            a[j]=temp;
        }

}

cout<< "Sorted numbers are:" << endl;
for(int x=1; x<=n;x++)
    {
        cout<< a[x] << ", ";

    }

    return 0;
}
Thanks, Jakir Hossain

12 comments em “C++ Code of Bubble Sort”

Post a Comment

Give your valuable Comment...

 

Programming, Data Structures & Algorithms Copyright © 2011 -- Template created by JAK -- Powered by Blogger