-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcode
More file actions
43 lines (36 loc) · 791 Bytes
/
code
File metadata and controls
43 lines (36 loc) · 791 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 optimizedbubbleSort(int a[]) {
for (int i = 0; i < 5; i++) {
int flag = false;
for (int j = 0; j < (5 - i - 1); j++) {
if (a[j] > a[j + 1]) {
flag = true;
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
if (flag == false) {
break;
}
}
}
int main() {
int myarray[5];
cout << "Enter 5 integers in any order: " << endl;
for (int i = 0; i < 5; i++) {
cin >> myarray[i];
}
cout << "Before Sorting" << endl;
for (int i = 0; i < 5; i++) {
cout << myarray[i] << " ";
}
cout << endl;
optimizedbubbleSort(myarray); // sorting
cout << "After Sorting" << endl;
for (int i = 0; i < 5; i++) {
cout << myarray[i] << " ";
}
return 0;
}