-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort.cpp
More file actions
43 lines (42 loc) · 961 Bytes
/
bubble_sort.cpp
File metadata and controls
43 lines (42 loc) · 961 Bytes
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
#include <iostream>
using namespace std;
void bubblesort(int a[], int x)
{
int current = 0;
bool sorted = false;
for (current = 0; current <= x && sorted == false; current++)
{
sorted = true;
int walker = x;
for (walker = x; walker > current; walker--)
{
if (a[walker] < a[walker - 1])
{
int temp = a[walker];
a[walker] = a[walker - 1];
a[walker - 1] = temp;
sorted = false;
}
}
}
}
int main()
{
int n;
cout << "Enter no.of elements: ";
cin >> n;
int arr[n];
cout << "Enter the array elements\n";
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
bubblesort(arr, n - 1);
cout << "Array after sorting:\n";
for (int i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
cout << endl;
return 0;
}