-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble_sort
More file actions
59 lines (54 loc) · 1.27 KB
/
bubble_sort
File metadata and controls
59 lines (54 loc) · 1.27 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <utility>
#include <sstream>
using namespace std;
void bubble_sort(double * array, unsigned int size) {
unsigned int i, j;
bool sorted = false;
while(!sorted) {
sorted = true
for( i=0; i < size; i++)
{
if ( array[i+1] < array[i] )
{
swap(array[i+1],array[i]);
sorted = false;
}
}
}
bool read (istream & stream, double* &array, unsigned int &size) {
bool res = true;
for (unsigned int i=0; i < size; i++) {
if (!(stream >> array[i])) {
res = false;
break;
}
}
return res;
}
void write(ostream & stream,double* array, unsigned int size) {
for (unsigned int i = 0; i < size; i++) {
stream << array[i] << ' ';
}
}
int main() {
unsigned int size;
double* array;
if(!(cin >> size)) {
cout << "An error has occured while reading numbers from line";
return 1;
}
cin.get();
string string;
getline(cin, string);
istringstream stream(string);
array = new double [size];
if( read(stream,array, size)) {
bubble_sort (array, size);
write (cout, array, size);
}
else {
cout << "An error has occured while reading numbers from line";
}
delete[] array;
}