forked from argonautica/sorting-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Bubble Sort Program which displays Passes
- Loading branch information
Showing
1 changed file
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// By Arshdeep (github.com/ar33h) | ||
#include<iostream> | ||
using namespace std; | ||
|
||
int arr[10]; | ||
int n, i; | ||
|
||
void bubble_sort(int arr[], int n); | ||
void display(int arr[], int n); | ||
void passes(int arr[], int n); | ||
|
||
int main() | ||
{ | ||
cout<<"\n------BUBBLE SORT------"; | ||
cout<<"\n\nEnter no. of Array elements: "; | ||
cin>>n; | ||
cout<<"\nEnter Array elements: "; | ||
for(i=0; i<n; i++) | ||
{ | ||
cin>>arr[i]; | ||
} | ||
cout<<"\nUnsorted Array: ["; | ||
for(i=0; i<n; i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
cout<<"]\n"; | ||
bubble_sort(arr,n); | ||
display(arr, n); | ||
} | ||
|
||
void bubble_sort(int arr[], int n) | ||
{ | ||
int j, k; | ||
int temp; | ||
|
||
for(i=0; i<n-1; i++) | ||
{ | ||
for(j=0; j<n-(i+1); j++) | ||
{ | ||
if(arr[j]>arr[j+1]) | ||
{ | ||
temp=arr[j]; | ||
arr[j]=arr[j+1]; | ||
arr[j+1]=temp; | ||
passes(arr, n); | ||
} | ||
|
||
} | ||
} | ||
|
||
} | ||
|
||
void display(int arr[], int n) | ||
{ | ||
cout<<"\n\nSorted Array: ["; | ||
for(i=0; i<n; i++) | ||
{ | ||
cout<<arr[i]<<" "; | ||
} | ||
cout<<"]"; | ||
} | ||
|
||
void passes(int arr[], int n) | ||
{ | ||
int x; | ||
cout<<"\nPass "<<i+1<<": ["; | ||
for(x=0; x<n; x++) | ||
{ | ||
cout<<arr[x]<<" "; | ||
} | ||
cout<<"]"; | ||
} |