-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorted_string_binary.cpp
113 lines (52 loc) · 1.34 KB
/
sorted_string_binary.cpp
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
#include<iostream>
#include<vector>
using namespace std;
// to return the index of where the name is
int binary(vector<string> v, string test )
{
int left = 0, right = v.size() - 1;
int m, result;
while(left <= right)
{
m = left + (right - left)/2;
if(v[m] == test)
{
result = m;
break;
}
else if(v[m] < test)
{
left = m + 1;
}
else if(v[m] > test)
{
right = m - 1;
}
}
return result;
}
int main()
{
string a[] = {"Collins, Bill", "Smith, Bart", "Allen, Jim", "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri", "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill", "James, Jean", "Weaver, Jim", "Pore, Bob", "Rutherford, Greg", "Javens, Renee", "Harrison, Rose", "Setzer, Cathy","Pike, Gordon", "Holland, Beth" };
vector<string> v(a, a + sizeof(a)/sizeof(string));
string temp;
for(int i = 1; i < v.size(); i++ )
{
int j = i;
while(v[j] < v[j - 1])
{
temp = v[j - 1];
v[j - 1] = v[j];
v[j] = temp;
j--;
}
}
for(int i = 0; i < v.size(); i++)
{
cout << v[i] << endl;
}
//implement binary search to see if a name is present in list
cout << "Last index = " << v.size() - 1 << endl;
cout << binary(v, "Allison, Jeff") << endl;
return 0;
}