Skip to content

Commit 016bb3a

Browse files
shubhendra-20MadhavBahl
authored andcommitted
Create day31_solved (#223)
Bubble Sort
1 parent ae5352f commit 016bb3a

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

day31/CPP/day31_solved

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
Author: Shubhendra Singh
3+
Github: shubhendra-20
4+
*/
5+
6+
#include <iostream>
7+
using namespace std;
8+
9+
void bubbleSort(int arr[], int n)
10+
{
11+
int i,j,t;
12+
bool swap;
13+
for (i = 0; i < n-1; i++)
14+
{
15+
swap = false;
16+
for (j = 0; j < n-i-1; j++)
17+
{
18+
if (arr[j] > arr[j+1])
19+
{
20+
t=arr[j];
21+
arr[j]=arr[j+1];
22+
arr[j+1]=t;
23+
swap=true;
24+
}
25+
}
26+
if (swap == false)
27+
break;
28+
}
29+
}
30+
31+
void printArray(int arr[], int n)
32+
{
33+
int i;
34+
for (i=0; i < n; i++)
35+
cout<<arr[i]<<" ";
36+
37+
}
38+
39+
int main()
40+
{
41+
int n,i;
42+
cin>>n;
43+
int arr[n];
44+
for(i=0;i<n;i++)
45+
{
46+
cin>>arr[i];
47+
}
48+
bubbleSort(arr, n);
49+
cout<<"Sorted array : ";
50+
printArray(arr, n);
51+
return 0;
52+
}

0 commit comments

Comments
 (0)