0% found this document useful (0 votes)
116 views

Program of Bubble Sort Using C++

This C++ program implements bubble sort to sort an array of integers. It defines a class called bublsort with methods to get user input for the number of elements and values, store them in an array, and perform the sorting operation. The dosorting method uses nested for loops to iterate through the array, compare adjacent elements, swap them if out of order, and print the array after each pass until it is fully sorted. It takes user input, stores it in an array using the getdata method, then calls dosorting to perform the bubble sort and output the sorted array.

Uploaded by

piyush gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
116 views

Program of Bubble Sort Using C++

This C++ program implements bubble sort to sort an array of integers. It defines a class called bublsort with methods to get user input for the number of elements and values, store them in an array, and perform the sorting operation. The dosorting method uses nested for loops to iterate through the array, compare adjacent elements, swap them if out of order, and print the array after each pass until it is fully sorted. It takes user input, stores it in an array using the getdata method, then calls dosorting to perform the bubble sort and output the sorted array.

Uploaded by

piyush gupta
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

BUBWCL.

CPP
#include<iostream.h> #include<conio.h> #include<string.h> class bublsort { int n,a[50],t,k,counter,swapped; public : bublsort(); void getdata(); void dosorting(); }; bublsort::bublsort() { k=0; counter=0; swapped=0; }

September 13, 2013

Page 1

void bublsort::getdata() { cout<<"Enter the number of elements to sort:"; cin>>n; cout<<"\nEnter the elements to sort:\n"; for ( int i=0;i<n;i++) { counter++; cin>>a[i]; } } void bublsort::dosorting() { if (counter>n) { cout<<"Entered number exceed the "<<n; } else { for ( int i=1;i<n;i++) { for ( int j=0;j<n-i;j++) { if (a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; swapped=1; } } if (!swapped) { break ; //swapped flag is used to check if the array is already sorted. } cout<<"\n\nOutput of Pass "<<i<<" of bubble sort is :\n"; for (k=0;k<n;k++) cout<<" "<<a[k]<<" "; } cout<<"\n\nSorted array by bubble sort:\n"; for (k=0;k<n;k++) cout<<" "<<a[k]<<" "; } } int main() { cout<<"\n----------BUBBLE SORT----------\n\n"; bublsort obj;

BUBWCL.CPP
obj.getdata(); obj.dosorting();

September 13, 2013

Page 2

cout<<"\n\n-------------END-------------"; return 0; }

You might also like