forked from BitSails/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
88 lines (65 loc) · 1.61 KB
/
sort.cpp
File metadata and controls
88 lines (65 loc) · 1.61 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <cstdlib>//for "exit()" on some systems
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int selectionSort(auto &inputs);
void printVector(auto &inputs);
int main()
{
vector<string> inputs;
string input;
int count = 0;
cout<<"Welcome to \"Sort it\". We first need some input data."<<endl;
cout<<"We'll assume the inputs do not have any spaces."<<endl<<endl;
cout<<"To end input type the #-character (followed by Enter)"<<endl<<endl;
cin>>input;
while(input != "#")//read an unknown number of inputs from keyboard
{
inputs.push_back(input);
cin>>input;
}
cout<<endl<<"["<<inputs.size()<<" values read from input source]"<<endl<<endl;
if(inputs.size() == 0)//no input
{
cout<<endl<<"No input received, quiting..."<<endl<<endl;
exit(1);//nothing to do but quit program
}
count = selectionSort(inputs);
cout << "Search took: " <<count<< " passes of 20,000" << endl;
printVector(inputs);
return 0;
}
int selectionSort(auto &Data)
{
int counter = 0;
for (int i= 0; i < Data.size(); i++)
{
int min = i;
for (int j = i+1; j < Data.size(); j++)
{
if (Data[j] < Data[min])
{
min = j; //Updates min index
}
}
swap(Data[i],Data[min]);
if (i % 20000 == 0)
{
counter++;
cout << "Still working" << endl;
}
}
return counter;
}
void printVector(auto &newprintInput)
{
unsigned int vectorSize = newprintInput.size();
cout << "Results: ";
for (unsigned int i = 0; i < vectorSize; i++)
{
cout << newprintInput[i]<< ", ";
}
cout << endl;
}