Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2aabf72

Browse files
Merge pull request fnplus#498 from Rex1911/bubblesort-cpp
Added Bubble Sort in CPP
2 parents 259c0cc + 6052456 commit 2aabf72

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
int main()
6+
{
7+
int n, arr[100];
8+
cout << "Enter no of elements: ";
9+
cin >> n;
10+
cout << "Enter " << n << " elements now: ";
11+
for (int i = 0; i < n; i++)
12+
{
13+
cin >> arr[i];
14+
}
15+
16+
cout << "Before the sort: ";
17+
for (int i = 0; i < n; i++)
18+
{
19+
cout << arr[i] << " ";
20+
}
21+
22+
//Bubble sort algo
23+
for (int i = 0; i < n - 1; i++)
24+
{
25+
for (int j = 0; j < n - i - 1; j++)
26+
{
27+
if (arr[j] > arr[j + 1])
28+
{
29+
//Swapping the elements
30+
int temp = arr[j];
31+
arr[j] = arr[j + 1];
32+
arr[j + 1] = temp;
33+
}
34+
}
35+
}
36+
37+
cout << "\nAfter the sort: ";
38+
for (int i = 0; i < n; i++)
39+
{
40+
cout << arr[i] << " ";
41+
}
42+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /