Bubble sort algorithm using user defined function by C++
#include <bits/stdc++.h>
using namespace std;
void sorting (int a[50],int n){
int i,j,temp;
for(i=0;i<n;i++){
cin >> a[i];
}
for(i=0;i<n-1;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
for(i=0;i<n;i++){
cout << a[i] << " ";
}
}
int main (){
int n;
cin >> n;
int arr[n];
sorting(arr,n);
return 0;
}
Comments
Post a Comment