forked from TARANG0503/DSA-Practice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbubblesort.cpp
46 lines (36 loc) · 1.05 KB
/
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>
using namespace std;
// function for bubble sort
void bubbleSort(int array[], int size) {
// running a loop to access each array element
for (int step = 0; step < (size-1); ++step) {
// running a loop to compare the array elements
for (int i = 0; i < size - (step-1); ++i) {
// after the comparison between two consecutive array elements
// modify > to < for descending order sorting
if (array[i] > array[i + 1]) {
// if elements are not in order
// swap
int tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
}
}
}
}
// function to print the array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
cout << " " << array[i];
}
cout << "\n";
}
// main function
int main() {
int data[] = {-15, 5, 20, -2, 104, -98, 50};
// evaluate length of the array
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
cout << "Sorted Array in Ascending Order:\n";
printArray(data, size);
}