forked from BitSails/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsortorigin.cpp
More file actions
178 lines (142 loc) · 3.95 KB
/
sortorigin.cpp
File metadata and controls
178 lines (142 loc) · 3.95 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <iostream>
#include <cstdlib>//for "exit()" on some systems
#include <vector>
#include <string>
using namespace std;
/**
\fn linearSearch
\brief Search data for the first occurrence of key
\param [in] key The value being searched for
\param [in] data The data set that will be searched
\returns location of key if found or -1 if not found
*/
int linearSearch(auto data, auto key);//prototype
void BubbleSort(auto& data);
void SelectionSort(auto& data);
void InsertionSort(auto& data);
int main()
{
vector<int>inputs;
int search_key, input;
int result;
cout<<"Welcome to \"sort it\". We first need to know which sort you want to use."<<endl;
cout<<"1. BubbleSort."<< endl;
cout << "2. Selection Sort" << endl;
cout<< "3. Insertion Sort" << endl;
cin >> result;
switch(result){
case 1: cout << "Let us begin" << endl;
cout << "Please input data and when finished, type '0' to stop" << endl;
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
}
else
{
BubbleSort(inputs);
for (int i = 0; i < inputs.size(); i++)
{
cout<<inputs[i]<<" , ";
}
}
cout<<endl<<"Program \"sort it\" is now finished."<<endl<<endl;
break;
case 2: cout << "Let us begin" << endl;
cout << "Please input data and when finished, type '#' to stop" << endl;
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
}
else
{
BubbleSort(inputs);
for (int i = 0; i < inputs.size(); i++)
{
cout<<inputs[i]<<" , ";
}
}
break;
case 3: cout << "Let us begin" << endl;
cout << "Please input data and when finished, type '#' to stop" << endl;
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
}
else
{
BubbleSort(inputs);
for (int i = 0; i < inputs.size(); i++)
{
cout<<inputs[i]<<" , ";
}
}
break;
default: cout << "Invalid attempt" << endl;
}
return 0;
}
int linearSearch(auto data, auto key){
for(int i = 0; i < data.size();i++){
if(data[i] == key)
return i;
}
return -1;
}
void BubbleSort(auto& data){
for (int k = 1; k < data.size();k++){
for (int i = 0; i < data.size()-1-k;i++){
if(data[i] > data[i+1])
swap(data[i], data[i+1]);
}
}
}
void SelectionSort(auto& data){
int i, j, minIndex, tmp;
for(i = 0; i < data.size()-1; i++){
minIndex = i;
//find smallest in unsorted part
for(j = i+1; j < data.size(); j++){
if(data[j] < data[minIndex])
minIndex = j;
}
if(minIndex != i){
tmp = data[i];
data[i] = data[minIndex];
data[minIndex] = tmp;
}
}
}
void InsertionSort(auto& data){
int nextIndex, moveItem, insertVal;
//looping through elements of vector
for(nextIndex = 1; nextIndex < data.size(); nextIndex++){
insertVal = data[nextIndex];
moveItem = nextIndex;
while (moveItem > 0 && data[moveItem -1] > insertVal){
data[moveItem] = data[moveItem + 1];
moveItem--;
}
data[moveItem] = insertVal;
}
}