-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.h
95 lines (89 loc) · 2.77 KB
/
search.h
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
// =================================================================
//
// File: search.h
// Author: Pedro Perez
// Description: This file contains the implementations of the
// sequential and binary search algorithms.
//
// Copyright (c) 2020 by Tecnologico de Monterrey.
// All Rights Reserved. May be reproduced for any non-commercial
// purpose.
// =================================================================
#ifndef SEARCH_H
#define SEARCH_H
#include "header.h"
#include <vector>
// =================================================================
// Performs a sequential search for an element within a vector.
//
// @param A, a vector of T elements.
// @param key, the element to search.
// @return the index of the searched element, -1 in case the element
// is not found in the vector.
// =================================================================
template <class T>
pair<int, int> sequentialSearch(const std::vector<T> &v, T key) {
int counter = 0;
for (int i = 0; i < v.size(); i++) {
counter++;
if (v[i] == key) {
return pair<int,int> (i,counter);
}
}
return pair<int,int> (-1,counter);
}
// =================================================================
// Performs a binary search for an element within a vector
//
// @param A, a vector of T elements.
// @param key, the element to search.
// @return the index of the searched element, -1 in case the element
// is not found in the vector.
// =================================================================
template <class T>
pair<int,int> binarySearch(const std::vector<T> &v, T key) {
int low, high, mid;
int counter = 0;
low = 0;
high = v.size() - 1;
while (low <= high) {
counter++;
mid = low + ((high - low) / 2); // mid = (high + low) / 2;
if (key == v[mid]) {
return pair<int,int> (mid,counter);
} else if (key < v[mid]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return pair<int,int> (-1,counter);
}
// =================================================================
// Performs a binary search for an element within a vector
//
// @param A, a vector of T elements.
// @param low, lower limit of the search.
// @param high, upper limit of the search.
// @param key, the element to search.
// @return the index of the searched element, -1 in case the element
// is not found in the vector.
// =================================================================
template <class T>
int binaryRSearch(const std::vector<T> &v, int low, int high, T key) {
int mid;
int counter=0;
if (low > high) {
return -1;
} else {
mid = low + ((high - low) / 2); // mid = (high + low) / 2;
if (key == v[mid]) {
return mid;
} else if (key < v[mid]) {
return binaryRSearch(v, low, mid - 1, key);
} else {
return binaryRSearch(v, mid + 1, high, key);
}
}
}
#endif /* SEARCH_H */