CSC 344 - Algorithms and Complexity: Lecture #5 - Searching
CSC 344 - Algorithms and Complexity: Lecture #5 - Searching
Complexity
Lecture #5 – Searching
Why Search?
• Everyday life -We are always looking for something –
in the yellow pages, universities, hairdressers
• Computers can search for us
• World wide web provides different searching
mechanisms such as yahoo.com, bing.com, google.com
• Spreadsheet – list of names – searching mechanism to
find a name
• Databases – use to search for a record
• Searching thousands of records takes time the large
number of comparisons slows the system
Sequential Search
• Best case?
• Worst case?
• Average case?
Sequential Search
int linearsearch(int x[], int n, int key)
{
int i;
return(-1);
}
Improved Sequential Search
int linearsearch(int x[], int n, int key)
{
int i;
//This assumes an ordered array
for (i = 0; i < n && x[i] <= key; i++)
if (x[i] == key)
return(i);
return(-1);
}
low = 0;
high = n -1;
while (low <= high) {
mid = (low + high) / 2;
if (x[mid] == key)
return(mid);
return(-1);
}
Searching Problem
Problem: Given a (multi)set S of keys and a search key
K, find an occurrence of K in S, if any.
• Searching must be considered in the context of:
– File size (internal vs. external)
– Dynamics of data (static vs. dynamic)
• Dictionary operations (dynamic data):
– Find (search)
– Insert
– Delete
<K >K
1 2
10 10
0 1 0 0
5 20 5 20
1 -1 0 1 -1
4 7 12 4 7
0 0 0 0
2 8 2 8
(a) (b)
Rotations
2 0 2 0
3 2 3 2
1 0 0 -1 0 0
R LR
2 > 1 3 1 > 1 3
0 0
1 2
(a) (c)
0
8
1 2 1
6 6 6
1 0 2 0 R (5) 0 0
5 8 5 8 > 3 8
0 1 0 0
3 3 2 5
0
2
0 1 0 0 0
2 5 2 4 8
0
4
-1 0
5 5
0 -2 0 0
3 6 3 7
RL (6)
0 0 1 > 0 0 0 0
2 4 8 2 4 6 8
0
7
Analysis of AVL trees
• h ≤ 1.4404 log2 (n + 2) - 1.3277
– Average height: 1.01 log2n + 0.1 for large n (found
empirically)
• Search and insertion are O(log n)
Deletion is more complicated but is also O(log n)
• Disadvantages:
– frequent rotations
– complexity
• A similar idea: red-black trees (height of subtrees is
allowed to differ by up to a factor of 2)
2-3 Tree
2-node 3-node
K K1, K 2
dataType dataArray[arraySize];
Example – Array of 100 items (continued)
dataArray[0] 0
dataArray[1] 1
dataArray[2] 2
dataArray[3] 3
……
……
dataArray[96] 96
dataArray[97] 97
dataArray[98] 98
dataArray[99] 99
hash[11] 34
dataRec[34]
easyHash.h
const int stringLength = 20;
const int arraySize = 100;
typedef struct {
keyString *key;
// Other Stuff can go here;
} dataRecord;
easyHash.cc
#include "easyHash.h"
#include <iostream>
#include <cstring>
using namespace std;
int i, sum = 0;
for (i = 0; value[i] != '\0'; i++)
sum += value[i];
return(sum % (arraySize+1));
}
float frac(float x);
float frac(float x) {
float fraction = x - (int) x;
return (fraction);
}
int main(void) {
hashArray x = new dataRecord[4];
keyString value = new char[stringLength];
Linear Quadratic
Probing Probing
Chaining
Separate Chaining
140 116 48
Hash Table and the Name Table
Hash Table Name Table
2 6
17 51 2 17 -1
3 -1
18 53 8 18 -1
4 -1
5 -1
19 61 3 19 18
6 -1
7 -1 20 64 3 20 -1
8 5
9 -1
10 19
HashTable.h
#include <cstring>
#pragma once
const int dataArraySize = 50;
const int hashTableSize = 10000;
const int stringSize = 80;
typedef struct {
int index;
char keyValue[stringSize];
} hashRecord;
typedef struct {
char keyField[stringSize];
// Other fields go here
} dataRecord;
class HashTable {
public:
HashTable(void);
int search(char *keyValue);
void insert(dataRecord data, dataRecord
dataTable[], bool &dup, bool &full);
void remove();
private:
int findHashCode(char *k);
hashRecord table[hashTableSize];
int dataArrayLength;
};
HashTable.cpp
#include "HashTable.h"
ix = findHashCode(k);
oldIx = ix;
do {
if (strcmp(table[ix].keyValue, k)== 0)
found = true;
else
ix = (ix +1 ) % hashTableSize;
} while (!found
&& table[ix].keyValue[0] != '\0'
&& ix != oldIx);
if (found)
return (ix);
else
return(ix);
}
//hashInsert() - Inserting into a hash table and
// resolving hash collision using
// linear probing
void HashTable::insert(dataRecord data,
dataRecord dataTable[],
bool &dup, bool &full) {
int ix, oldIx;
int idx;
TestHash.cpp
#include "HashTable.h"
#include <cstring>
#include <iostream>
int main(void) {
int i, index;
bool full, dup;
dataRecord data;
dataRecord dataArray[dataArraySize];
HashTable ht;