Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions bubble_sort_with_comparator.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include<bits/stdc++.h>
using namespace std;

bool compare( int a, int b){
return a>b;
}

void bubble_sort(int a[],int n, bool (&cmp)( int a,int b)){

for( int itr = 1;itr <= n-1;itr++){
for( int j = 0; j<=(n-itr-1);j++)
{
if (cmp(a[j], a[j+1])){
swap(a[j],a[j+1]);
}
}
}
}

int main(){
int n;
int a[1000];

cout<<"Enter the length of the array : "<<"\n";
cin>>n;

cout<<"Enter the elements in the array : "<<"\n";
for(int i =0; i<n ; i++){
cin>>a[i];
}

bubble_sort(a,n,compare);
cout<<"Elements after bubble sort are: "<<"\n";
for(int i = 0 ; i< n ; i++){
cout<<a[i]<<" ";
}

return 0 ;

}