BubbleSort C++
#include<iostream>
using namespace std;
void printArray(int *arr, int n);
class BubbleSort{
public:
void bSort(int *arr, int n){
for(int i = 0; i<n-1; i++){
for(int j = 0; j<n-1; j++){
if(arr[j] > arr[j+1]){
swap(&arr[j],&arr[j+1]);
}
}
}
}
void swap(int *a, int *b){
int temp = *a;
*a = *b;
*b = temp;
}
};
int main(){
int arr[100];
int n = 5;
cout<<"\t**** Bubble Sort ****\n";
cout<<"\nenter size of array: ";
cin>>n;
cout<<"enter "<<n<<" elements: \n";
for(int i = 0; i<n; i++){
cout<<"enter arr["<<i<<"] : ";
cin>>arr[i];
}
cout<<"\n**** Before Sorting ***\n";
printArray(arr,n);
BubbleSort sort;
sort.bSort(arr,n);
cout<<"\n\n**** After Sorting ****\n";
printArray(arr,n);
}
void printArray(int *arr, int n){
for(int i = 0; i<n; i++){
cout<<arr[i]<<"\t";
}
}
Comments
Post a Comment