-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctionsdone.cpp
More file actions
232 lines (202 loc) · 6.13 KB
/
functionsdone.cpp
File metadata and controls
232 lines (202 loc) · 6.13 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include<bits/stdc++.h>
#include<string.h>
#include<list>
using namespace std;
const int tablesize = 10; //Initializing the table size of the Hashtable
class Hash_table {
private:
int GRs[tablesize]; //declaring an integer array to store GR numbers
string Names[tablesize]; //declaring string array to store names
public:
int Hashfunct(int GR_No);
void insertKey(int GR_No , string name);
void deleteKey(int GR_No);
void modifyKey(int GR_No , string name);
int searchKey(int GR_No);
void print_table();
void print_key_value(int GR_No);
void sort_key(int l, int r);
void merge(int l, int m, int r);
void SortNames(int n);
Hash_table() //Constructor to initialize all GR array elements to 0
{
for (int i = 0; i<tablesize; i++)
{
GRs[i] = 0;
}
}
};
int Hash_table::Hashfunct(int GR_No) //Hash function(To find index of key to be inserted(or searched))
{
return (GR_No) % tablesize;
}
void Hash_table::insertKey(int GR_No, string name) //Insert function(The GR number is the the key and the name is the value)
{
int index = Hashfunct(GR_No);
while (GRs[index] != 0 && GRs[index] != GR_No) //Condition to avoid collisions
{
index = Hashfunct(index + 1);
}
GRs[index] = GR_No; //Insertion of key in array
Names[index] = name; //Insertion of value in array
}
int Hash_table::searchKey(int GR_No) //Function to search the index of given key
{
int count = 0;
for(int i=0; i<tablesize; i++)
{
if(GRs[i] == GR_No)
{
count++;
return i; //Returning the index of key
break;
}
}
if(count == 0)
{
cout<< "Sorry, this GR Number is not valid" <<endl;
}
}
void Hash_table::print_key_value(int GR_No) //Function to print a particular key-value pair
{
int x;
x = searchKey(GR_No);
cout << "GR Number :" <<GRs[x] << endl;
cout << "Name :" << Names[x] << endl;
}
void Hash_table::modifyKey(int GR_No , string name) //Function to change the value(name) of an existing key
{
int p;
p = searchKey(GR_No);
Names[p] = name;
cout << "Updated information:" << endl;
print_key_value(GR_No);
}
void Hash_table::deleteKey(int GR_No) //Function to delete the existing key
{
int index = searchKey(GR_No);
if (GRs[index] == 0){
cout << "Key not found" << endl;
}
else{
GRs[index] = 0;
cout << "Key-Value pair DELETED" << endl;
}
}
void Hash_table::print_table() //Function to print entire hash table
{
cout << "GR Number" << "\t" << "Name" << endl;
for(int i=0; i<tablesize; i++) {
if(GRs[i] != 0) {
cout << GRs[i] << "\t\t" << Names[i] << endl;
}
}
}
void Hash_table::merge(int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
// create temp arrays
int L[n1], R[n2];
string LS[n1], RS[n2];
// Copy data to temp arrays L[] and R[]
for (i = 0; i < n1; i++) {
L[i] =GRs [l + i];
LS[i] = Names[l + i];
}
for (j = 0; j < n2; j++) {
R[j] = GRs[m + 1 + j];
RS[j] = Names[m + 1 + j];
}
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
GRs[k] = L[i];
Names[k] = LS[i];
i++;
}
else {
GRs[k] = R[j];
Names[k] = RS[j];
j++;
}
k++;
}
// Copy the remaining elements of L[], if there are any
while (i < n1) {
GRs[k] = L[i];
Names[k] = LS[i];
i++;
k++;
}
// Copy the remaining elements of R[], if there are any
while (j < n2) {
GRs[k] = R[j];
Names[k] = RS[j];
j++;
k++;
}
}
// l is for left index and r is right index of the sub-array of arr to be sorted
void Hash_table::sort_key(int l, int r)
{
if (l < r) {
int m = l + (r - l) / 2;
// Sort first and second halves
sort_key(l, m);
sort_key(m + 1, r);
merge(l, m, r);
}
}
void Hash_table::SortNames(int n)
{
int index[n];
int i, j, min;
for (i=0; i<n; i++)
index[i] = i; //inserting index value at the corresponding index
// selection sort
for (i=0; i<n-1; i++)
{
min = i;
for (j=i+1; j<n; j++)
{
if (GRs[i] != 0 && GRs[j] != 0) //comparing only those indices which have GR Nos and Names into it
{
if (Names[index[min]].compare(Names[index[j]]) > 0)
min = j;
}
}
if (min != i)
{
//changing the elements of index array as per the alphabetical order of words
int temp = index[min];
index[min] = index[i];
index[i] = temp;
}
}
cout << "Sorted names and corresponding GRs---" << endl;
for (i=0; i<n; i++) {
if (GRs[i] != 0) {
cout << Names[index[i]] << " " << GRs[index[i]] << endl; //printing in sorted order
}
}
}
int main() {
int x,y;
Hash_table h1;
h1.insertKey(1191032,"Prapti");
h1.insertKey(1191026,"Juhi");
h1.insertKey(1191044,"Harshita");
h1.SortNames(10);
x=h1.searchKey(1191044);
cout<<"Index :"<<x<<endl;
h1.searchKey(119103433);
h1.deleteKey(1191044);
h1.searchKey(1191044);
h1.modifyKey(1191032,"Aarya");
h1.sort_key(0,9);
h1.print_table();
}