Professional Documents
Culture Documents
Data Structure Lec43 Handout
Data Structure Lec43 Handout
Data Structure Lec43 Handout
Lecture No. 43
___________________________________________________________________
Data Structures
Lecture No. 43
Reading Material
Data Structures and Algorithm Analysis in C++
Chapter. 5, 7
5.4, 5.5, 5.6, 7.1
Summary
Hashing Animation
Applications of Hashing
When Hashing is Suitable?
Sorting
Elementary Selection Algorithms
Selection Sort
Hashing Animation
In the previous lecture, we discussed about collision strategies in hashing. We studied
three solutions, Linear Probing, Quadratic Probing and Linked List chaining. Hashing is
vast research field, which covers hash functions, storage and collision issues etc. At the
moment, we will see hashing in implementation of table ADT. Operations of insert,
delete and find are performed in constant time using this hashing strategy. Constant time
means the time does not increase with the increase in data volume. However, if collisions
start happening then the time does not remain constant. Especially if we see linear
probing, we had to insert the data by sorting the array sequentially. Similar was the case
with quadratic. In case of linked list, we start constructing linked list that takes time and
memory. But later we will see some situations where hashing is very useful.
Today, we will study these three strategies of hash implementation using animations.
These animations will be provided to you in a Java program. It is important to mention
here that all the data structures and algorithms we have studied already can be
implemented using any of the languages of C/C++ or Java. However, it is an important
decision to choose the programming language because every language has its strong area,
where it has better application. Java has become very popular because of its facilities for
Internet. As you already know C++, therefore, Java is easy to learn for you. The syntax
is quite similar. If we show you the java code you will say it is C++.
Lets see the hashing animation. This animation will be shown in the browser. This is an
applet written in java language. We will see linear probing, quadratic probing and link list
chaining in it. This is an example of how do we solve collision.
Page 1 of 7
Applications of Hashing
Lets see few examples of those applications where hashing is highly useful. The hashing
can be applied in table ADT or you can apply hashing using your array to store and
retrieve data.
Compilers use hash tables to keep track of declared variables (symbol table).
Compilers use hash tables in order to implement symbol tables. A symbol table is an
important part of compilation process. Compiler puts variables inside symbol table during
this process. Compiler has to keep track of different attributes of variables. The name of
the variable, its type, scope and function name where it is declared etc is put into the
symbol table. If you consider the operations on symbol table, those can be insertion of
variable information, search of a variable information or deletion of a variable. Two of
these insert and find are mostly used operations. You might have understood already that
a variable name will be parameter (or the key) to these operations. But there is one slight
problem that if you named a variable as x outside of a code block and inside that code
block, you declared another variable of the similar type and name then only name cannot
be the key and scope is the only differentiating factor. Supposing that all the variables
inside the program have unique names, variable name can be used as the key. Compiler
will insert the variable information by calling the insert function and by passing in the
variable information. It retrieves the variable value by passing in the variable name. Well,
this exercise is related to your Compiler Construction course where you will construct
you own language compiler.
Another usage of hashing is given below:
A hash table can be used for on-line spelling checkers if misspelling detection
(rather than correction) is important, an entire dictionary can be hashed and words
checked in constant time.
You must have used spell checkers in a word processing program like MS Word. That
spell checker finds out mistakes, provides you correct options and prompts you to choose
any of the synonyms. You can also set the correct the words automatically.
Hashing can be used to find the spelling mistakes. For that you first take all the words
from the dictionary of spoken English and construct a hash table of those. To find the
spelling mistakes, you will take first word from the text that is being checked and
compare it with all the words present inside the hash table. If the word is not found in the
hash table then there is a high probability that the word is incorrect, although there is a
low probability that the word is correct but it is not present in the dictionary. Based on the
high probability a message can be displayed to the user of the application that the word is
wrong. MS Word does the same. As far the automatic correct feature is concerned, it is
another algorithm, which we are not going to discuss here.
Lets see few more examples in this connection.
Game playing programs use hash tables to store seen positions, thereby saving
Page 3 of 7
Sorting
Sorting means to put the data in a certain order or sequence. We have discussed sorting
before in different scattered through topics in this course but it has not been discussed so
far as a separate topic. You must be remembering that when we traverse the binary search
tree in in-order way, the obtained data happens to be sorted. Similarly, we saw other data
structures, where we used to keep data in sorted order. In case of min-heap if we keep on
removing elements one by one, we get data in sorted order.
Sorting is so useful that in 80-90% of computer applications, sorting is there in one form
or the other. Normally, sorting and searching go together. Lot of research has been done
on sorting; you can get lot of stuff on it from different sources. Very efficient algorithms
have already been developed for it. Moreover, a vast Mathematical analysis has been
performed of these algorithms. If you want to expose yourself, how these analyses are
performed and what Mathematical tools and procedures are employed for performing
analysis then sorting is very useful topic for you.
Sorting Integers
How to sort integers in this array?
Page 5 of 7
20
10
10 20
Fig 43.1
We want to sort the numbers given in the above array. Apparently, this operation may
seem very simple. But think about it, if you are given a very large volume of data (may
be million of numbers) then you may realize that there has to be an efficient mechanism
to perform this operation. Firstly, lets put the problem in words:
We have a very large array of numbers. We want to sort the numbers inside the array in
ascending order such that the minimum number of the array will be the first element of it
and the largest element will be the last element at the end of the array.
Lets go to the algorithms of sorting. Point to be noted here that we are going to study
algorithms of sorting; we are not talking about data structures. Until now, you might have
realized that algorithms go along data structures. We use a data structure to contain data
and we use algorithms to perform certain operations or actions on that data.
Selection Sort
Main idea:
o find the smallest element
o put it in the first position
o find the next smallest element
o put it in the second position
Page 6 of 7
Page 7 of 7