Skip to content

Commit c4f1158

Browse files
authored
Create bubble_sort.cpp
1 parent 6f47f76 commit c4f1158

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

bubble_sort.cpp

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
6+
void bubbleSort(int arr[], int n)
7+
{
8+
int i, j;
9+
for (i = 0; i < n - 1; i++)
10+
11+
12+
for (j = 0; j < n - i - 1; j++)
13+
if (arr[j] > arr[j + 1])
14+
swap(arr[j], arr[j + 1]);
15+
}
16+
17+
18+
void printArray(int arr[], int size)
19+
{
20+
int i;
21+
for (i = 0; i < size; i++)
22+
cout << arr[i] << " ";
23+
cout << endl;
24+
}
25+
26+
27+
int main()
28+
{
29+
int arr[] = { 5, 1, 4, 2, 8};
30+
int N = sizeof(arr) / sizeof(arr[0]);
31+
bubbleSort(arr, N);
32+
cout << "Sorted array: \n";
33+
printArray(arr, N);
34+
return 0;
35+
}

0 commit comments

Comments
 (0)