Monday, 8 June 2015

Program of bubble sort


Code for Program of bubble sort in C++ Programming



 



 
#include <iostream.h>
#include <conio.h>
#define MAX 10

class bubsort{
    int arr[MAX],n;
    public:
    void getdata();
    void showdata();
    void sortLogic();
};

void bubsort :: getdata(){
    cout<<"How many elements you require : ";
    cin>>n;
    for(int i=0;i<n;i++)
        cin>>arr[i];
}

void bubsort :: showdata(){
    cout<<"\n--Display--\n";
    for(int i=0;i<n;i++)
        cout<<arr[i]<<"   ";
}

void bubsort :: sortLogic(){
    int temp;
    for(int i=0;i<n;i++){
        for(int j=0,exchange=0;j<n;j++){
            if(arr[j] > arr[j+1]){
                temp = arr[j];
                arr[j] = arr[j+1];
                arr[j+1] = temp;
                exchange++;
                cout<<"\n arr[j] = "<<arr[j]<<"  arr[j+1] = "<<arr[j+1];
            }
        }
        cout<<endl;
        if(exchange==0)
            break;
    }
}

void main(){
    clrscr();
    cout<<"\n*****Bubble Sort*****\n";
    bubsort obj;
    obj.getdata();
    obj.sortLogic();
    obj.showdata();
    getch();
}

No comments:

Post a Comment