Loading...

BubbleSort.cpp


/*/////////////////////////////////////////////////////////////////////////////
 * Bubble sort
 * Shubham Kumar
 * sshubhamk1@hotmail.com
/////////////////////////////////////////////////////////////////////////////*/

#include <iostream>
using namespace std;

int bubble_sort(int *arr, int size);
int main(void)
{
    int size;
    do
    {
        cout << "Enter the size of the array: ";
        cin >>size;
        if(size<1)
        {
            cout << "Size must be positive number(size>0). Try again..."<<endl;
        }
    }while(size<1);
    int *arr = new int[size];
    cout << "Enter the elements:"<<endl;
    for(int i=0;i< size;i++)
    {
        cout << "arr["<<i+1<<"]: ";
        cin >> arr[i];
    }
    cout << "Bubble sorting..."<<endl;
    bubble_sort(arr,size);
    cout << "Sorted array is: "<<endl;
    for(int i=0;i< size;i++)
    {
        cout << "arr["<<i+1<<"]: "<<arr[i]<<endl;;
    }
    return 0;
}

int bubble_sort(int *arr, int size)
{
    for(int i=0;i<size;i++)
    {
        for(int j=size-1;j>i;j--)
        {
            if(arr[j]<arr[j-1])
            {
                int temp = arr[j-1];
                arr[j-1] = arr[j];
                arr[j] = temp;
            }
        }
    }
    return 0;
}


123 Sample Rd, Samplington, SA 12345
(555) 0123