-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubbleSort.cpp
More file actions
36 lines (31 loc) · 821 Bytes
/
BubbleSort.cpp
File metadata and controls
36 lines (31 loc) · 821 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
/**
* Author: Hemant Tripathi
*/
#include <iostream>
using namespace std;
#define MAXSIZE 8
int main() {
int dataArray[MAXSIZE] = {25,57,48,37,12,92,86,33};
bool swapping = true;
for(int i=0; i < MAXSIZE && swapping; i++) {
swapping = false;
for(int j=0; j < MAXSIZE-i-1; j++) {
if(dataArray[j] > dataArray[j+1]) { //swap the data
dataArray[j] = dataArray[j] + dataArray[j+1];
dataArray[j+1] = dataArray[j] - dataArray[j+1];
dataArray[j] = dataArray[j] - dataArray[j+1];
swapping = true;
}
}
cout << "After " << i+1 << " iteration: ";
for(int x=0; x < MAXSIZE; x++) {
cout << "\t" << dataArray[x];
}
cout << endl;
}
cout << "Bubble sorting executed successfully >>>>>>>>>>>" << endl;
for(int i=0; i < MAXSIZE; i++) {
cout << "\t" << dataArray[i];
}
cout << endl;
}