Docsity

Hashing Table - Data Structures - Lecture Slides, Slides of Data Structures and Algorithms

Partial preview of the text

Download Hashing Table - Data Structures - Lecture Slides and more Slides Data Structures and Algorithms in PDF only on Docsity!

Hashing Table

What is Hashing?

  • Hashing is another approach to storing and searching for values.
  • The technique, called hashing, has a worst case behavior that is linear for finding a target, but with some care, hashing can be dramatically fast in the average case.

TABLES: Hashing

  • Hash functions balance the efficiency of direct access with better space efficiency. For example, hash function will take numbers in the domain of SSN’s, and map them into the range of 0 to 10,000.

3482 1201

546208102

541253562

f(x) f(x)

Hash Function Map: The function f(x) will take SSNs and return indexes in a rangewe can use for a practical array.

Where hashing is helpful?

  • Any where from schools to department stores or manufactures can use hashing method to simple and easy to insert and delete or search for a particular record.

How does hashing work?

  • Example: suppose, the Tractor company sell all kind of tractors with various stock numbers, prices, and other details. They want us to store information about each tractor in an inventory so that they can later retrieve information about any particular tractor simply by entering its stock number.
  • Suppose the information about each tractor is an object of the following form, with the stock number stored in the key field:
  • struct Tractor
  • {
  • int key; // The stock number
  • double cost; // The price, in dollar
  • int horsepower; // Size of engine
  • };
  • It is bad to use an array with 5000 components to store and search for a particular elements among only 50 elements.
  • If we are clever, we can store the records in a relatively small array and yet retrieve particular stock numbers much faster than we would by serial search.
  • Suppose the stock numbers will be these: 0, 100, 200, 300, … 4800, 4900
  • In this case we can store the records in an array called data with only 50 components. The record with stock number “j” can be stored at this location:
  • data[ j / 100]
  • The record for stock number 4900 is stored in array component data[49]. This general technique is called HASHING.