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
121 lines (93 loc) · 2.65 KB
/
sort.cpp
File metadata and controls
121 lines (93 loc) · 2.65 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
#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
*/
void bubbleSort(auto& data)
{
bool swapped = true;
int passes = 0;
string temp;
int remainder;
while(swapped)
{
swapped =false;
passes++;
for(int i=0; i < data.size() - passes; i++)
{
if (data[i] > data[i+1])
{
//swap values
temp = data[i];
data[i] = data[i+1];
data[i+1] = temp;
swapped=true;
}
}
remainder= passes%20000;
if (remainder == 0)
cout<< "Number of passes: "<< passes<<endl;
}
}
int linearSearch(auto data, auto key);//prototype
int linearSearch(auto data, auto key)
{
for (int i=0; i<data.size(); i++)
{
if (data[i]==key)
{
return i;
}
}
return -1;
}
int main()
{
vector<string> inputs;
string search_key, input;
int result;
cout<<"Welcome to \"search 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
}
cout<<endl<<"To end input type the #-character (followed by Enter)"<<endl<<endl;
/* cout<<"Enter a value to search for: ";
cin>>search_key;
while(search_key != "#")//perform searches until sentinel entered
{
result = linearSearch(inputs,search_key);
cout<<" '"<<search_key<<"' was ";
if (result == -1)
cout<<"not found";
else
cout<<"found at index "<<result;
cout<<endl<<endl<<"Enter a value to search for: ";
cin>>search_key;
}
cout<<endl<<"Program \"search it\" is now finished."<<endl<<endl; */
bubbleSort(inputs);
cout << "Sort list :"<< endl;
for (int i=0; i < inputs.size(); ++i)
{
cout << inputs[i] << endl;
}
return 0;
}