forked from dharmanshu1921/porject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bubbleSort.cpp
46 lines (39 loc) · 911 Bytes
/
bubbleSort.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
46
#include <iostream>
void printArray(int *array, int n)
{
for (int i = 0; i < n; ++i)
std::cout << array[i] << std::endl;
}
void bubbleSort(int *array, int n)
{
bool swapped = true;
int j = 0;
int temp;
while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < n - j; ++i)
{
if (array[i] > array[i + 1])
{
temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
}
}
int main()
{
int array[] = {95, 45, 48, 98, 485, 65, 54, 478, 1, 2325};
int n = sizeof(array)/sizeof(array[0]);
std::cout << "Before Bubble Sort :" << std::endl;
printArray(array, n);
bubbleSort(array, n);
std::cout << "After Bubble Sort :" << std::endl;
printArray(array, n);
return (0);
}
@anmolch24