-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection_sort
More file actions
59 lines (54 loc) · 1.3 KB
/
selection_sort
File metadata and controls
59 lines (54 loc) · 1.3 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 selection_sort(double * array, unsigned int size) {
unsigned int i, j, k;
double x;
for( i=0; i < size; i++)
{
k=i; x=array[i];
for( j=i+1; j < size; j++)
if ( array[j] < x )
{
k=j; x=array[j];
}
swap(array[k],array[i]); array[i] = x;
}
}
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)) {
selection_sort (array, size);
write (cout, array, size);
}
else {
cout << "An error has occured while reading numbers from line";
}
delete[] array;
}