Docsity

Bubble Sort - C plus plus Code, Study Guides, Projects, Research of Computer Programming

Toggle sidebar
bg1
//Bubble Sort
#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();
}
๎˜ƒ
docsity.com

Partial preview of the text

Download Bubble Sort - C plus plus Code and more Study Guides, Projects, Research Computer Programming in PDF only on Docsity!

//Bubble Sort

#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(); }

docsity.com