-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubble-Sort.cpp
More file actions
38 lines (36 loc) · 806 Bytes
/
Bubble-Sort.cpp
File metadata and controls
38 lines (36 loc) · 806 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
#include<iostream>
using namespace std;
void doswap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
void bubblesort(int arr[], int size) {
//this loop will iterate from 0 to size-1
for (int i = 0; i < size; i++) {
bool isswapped = false;
//This loop will compare adjucent variables if greater than swap
for (int j = 0; j < size - i - 1; j++) {
//Ascending order
if (arr[j] > arr[j + 1]) {
doswap(arr[j], arr[j + 1]);
isswapped = true;
}
}
if (isswapped == false) {
break;
}
}
}
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
cout << arr[i] << ' ';
}
}
int main() {
int data[5] = { 0,-2,45,11,-9 };
int size = 5;
bubblesort(data, size);
cout << "Sorted array is\n ";
printArray(data, size);
}