Skip to content

Commit

Permalink
Adding Bubble Sort Program which displays Passes
Browse files Browse the repository at this point in the history
  • Loading branch information
ar33h committed Oct 17, 2020
1 parent ee55b38 commit 1432020
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions C++/BubbleSort_withPasses.cpp
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<<"]";
}

0 comments on commit 1432020

Please sign in to comment.