forked from THUNDERANKUSH/SORTING-CODES
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bubble_Sort.cpp
45 lines (44 loc) · 895 Bytes
/
Bubble_Sort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace std;
void printarray(int *A, int n)
{
for (int i = 0; i < n; i++)
{
cout << A[i] << " ";
}
cout << "\n"
<< endl;
}
void bubblesort(int *A, int n)
{
int temp;
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (A[j] > A[j + 1])
{
temp = A[j];
A[j] = A[j + 1];
A[j + 1] = temp;
}
}
}
}
int main()
{
int n;
cout << "enter number elements to be sorted:";
cin >> n;
cout << "Enter the elements : \n";
int A[n];
for (int k = 0; k < n; k++)
{
cin >> A[k];
}
cout << "List Before Sorting:\n";
printarray(A, n);
bubblesort(A, n);
cout << "List After Sorting:\n";
printarray(A, n);
}