Created
April 12, 2023 18:01
-
-
Save rambabu-patidar/3f7c9d983defa3ce1ae037a0a6a3074d to your computer and use it in GitHub Desktop.
code for bubble sorting algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<iostream> | |
using namespace std; | |
void bubbleSort( int arr[], int n){ | |
for( int i=1;i<=n-1;i++){ | |
for(int j=0;j<=n-i;j++){//if u don't want to start the j loop from 1 | |
//then start it from 1 but theu arr[j] will be replaced by arr[j-1] | |
if( arr[j]>arr[j+1]){ | |
int temp = arr[j]; | |
arr[j]= arr[j+1]; | |
arr[j+1]=temp; | |
} | |
} | |
} | |
for( int i=0;i<n;i++){ | |
cout<<arr[i]<<" "; | |
} | |
} | |
int main(){ | |
int n; | |
cin>>n; | |
int arr[n]; | |
for( int i=0;i<n;i++){ | |
cin>>arr[i]; | |
} | |
bubbleSort( arr , n); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment