SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
Unit - 5 Sorting and Hashing
1. Define sorting. List out Sorting Methods. Explain Bubble Sort method.
OR
Q. Write an algorithm for Bubble Sort.
Q. Write an algorithm for Selection Sort.
Q. Write an algorithm for Quick Sort.
Q. Write an algorithm for Insertion Sort.
Q. Write an algorithm for Merge Sort.
2. What is Hashing? Explain any one Hashing function in detail.
OR
Q. List four types of Hash Table. Explain any one in brief.
Q. ExplainHashing techniques.
3. Explain Collision.
OR
Q. Explain Collision resolution techniques
Q. Explain linear probing method with suitable example.
BHAUMIK GELANI Page 1
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
1. Define sorting. List out Sorting Methods. Explain Bubble Sort method.
OR
Q. Write an algorithm for Bubble Sort.
Q. Write an algorithm for Selection Sort.
Q. Write an algorithm for Quick Sort.
Q. Write an algorithm for Insertion Sort.
Q. Write an algorithm for Merge Sort.
Q. Write an algorithm for Radix Sort.
Ans:
Sorting:
o Sorting is the process of arranging the elements of an array so that they
can be placed either in ascending or descending order.
List out Sorting Methods:
1. Bubble Sort
2. Selection Sort
3. Quick Sort
4. Insertion Sort
5. Merge Sort
6. Radix Sort
BHAUMIK GELANI Page 2
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
1. Bubble Sort:
In Bubble sort, each element of the array is compared with its adjacent element.
The algorithm processes the list in passes.
A list with n elements requires n-1 passes for sorting.
Algorithm:
BHAUMIK GELANI Page 3
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
2. Selection Sort:
“A selection sort algorithm will find the smallest unsorted element in each
iteration, and add it to the sorted list of elements.”
Algorithm:
BHAUMIK GELANI Page 4
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
Example:
BHAUMIK GELANI Page 5
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
3. Quick Sort:
Here we will see the quick sort technique but we will use three-way quick sort.
The basic quick sort technique is just finding an element as pivot then partition
the array around pivot, after that, recurs for sub arrays on left and right of the
pivot.
The three-way quick sort is similar, but there are three sections.
array arr[1 to n] is divided into three parts.
o arr[1 to i]
o arr[i + 1, j]
o arr[j + 1, n]
BHAUMIK GELANI Page 6
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
Algorithm:
BHAUMIK GELANI Page 7
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
4. Insertion Sort:
Insertion sort is based on the idea of consuming one element from unsorted
array and inserting it at the correct position in the sorted array.
In this algorithm, we insert each element onto its proper place in the sorted
array.
This is less efficient than the other sort algorithms like quick sort, merge sort,
etc.
Algorithm:
BHAUMIK GELANI Page 8
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
Example:
BHAUMIK GELANI Page 9
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
5. Merge Sort:
The merge sort algorithm closely follows the divide-and-conquer paradigm.
o Divide:
Divide the n-elements sequence to be sorted into two sub-sequences
of n/2 elements each recursively.
o Conquer:
Sort the two sub-sequences recursively using merge sort.
o Combine:
Merge the two sorted sub-sequences to produce the sorted answer.
Algorithm:
BHAUMIK GELANI Page 10
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
BHAUMIK GELANI Page 11
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
Example:
o Now let’s take the array with elements as 5,2,4,7,1,3,2,6 and use the Merge
sort visualization to sort these elements.
BHAUMIK GELANI Page 12
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
4. What is Hashing? Explain any one Hashing function in detail.
OR
Q. List four types of Hash Table. Explain any one in brief.
Q. Explain Hashing techniques.
Ans:
Hashing is a technique to convert a range of key values into a range of indexes
of an array.
Hash table is a type of data structure which is used for storing and accessing data
very quickly. Insertion of data in a table is based on a key value.
Hash function is a function which is applied on a key by which it produces an
integer, which can be used as an address of hash table.
Types of hash function:
o There are various types of hash function which are used to place the data in
a hash table,
BHAUMIK GELANI Page 13
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
1. Division method
2. Mid square method
3. Digit folding method
1. Division method:
In this the hash function is dependent upon the remainder of a division.
For example: if the record 52,68,99,84 is to be placed in a hash table and let us
take the table size is 10.
h(key)= record % table size.
2=52%10
8=68%10
9=99%10
4=84%10
BHAUMIK GELANI Page 14
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
2. Mid square method:
In this method firstly key is squared and then mid part of the result is taken as
the index.
For example: consider that if we want to place a record of 3101 and the size of
table is 1000.
o So 3101*3101=9616201 i.e. h (3101)
= 162 (middle 3 digit)
3. Digit folding method:
In this method the key is divided into separate parts and by using some simple
operations these parts are combined to produce a hash key.
For example: consider a record of 12465512 then it will be divided into parts
i.e. 124, 655, 12.
After dividing the parts combine these parts by adding it.
H(key)=124+655+12
=791
BHAUMIK GELANI Page 15
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
5. Explain Collision.
OR
Q. Explain Collision resolution techniques
Q. Explain Linear Probing method with suitable example.
Ans:
Collision:
o It is a situation in which the hash function returns the same hash key for
more than one record, it is called as collision.
Collision Resolution Techniques:
1. Chaining
2. Linear probing
3. Quadratic probing
4. Double hashing
BHAUMIK GELANI Page 16
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
1. Chaining:
It is a method in which additional field with data i.e. chain is introduced.
A chain is maintained at the home bucket.
In this when a collision occurs then a linked list is maintained for colliding data.
Example:
o Let us consider a hash table of size 10 and we apply a hash function of
H(key)=key % size of table.
o Let us take the keys to be inserted are 31,33,77,61.
BHAUMIK GELANI Page 17
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
2. Linear probing:
It is very easy and simple method to resolve or to handle the collision.
In this collision can be solved by placing the second record linearly down,
whenever the empty place is found.
In this method there is a problem of clustering which means at some place block
of a data is formed in a hash table.
Example:
o Let us consider a hash table of size 10 and hash function is defined as
H(key)=key % table size.
o Consider that following keys are to be inserted that are 56,64,36,71.
BHAUMIK GELANI Page 18
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
3. Quadratic probing:
This is a method in which solving of clustering problem is done.
In this method the hash function is defined by the
H(key)=(H(key)+x*x)%tablesize.
Let us consider we have to insert following elements that are:-67, 90,55,17,49.
In this we can see if we insert 67, 90, and 55 it can be inserted easily but at case
of 17 hash function is used in such a manner that :-(17+0*0)%10=17 (when x=0
it provide the index value 7 only) by making the increment in value of x. let x =1
so (17+1*1)%10=[Link] this case bucket 8 is empty hence we will place 17 at
index 8.
BHAUMIK GELANI Page 19
SAL EDUCATION CAMPUS
SAL INSTITUTE OF DIPLOMA STUDIES (SIDS)
DEPARTMENT Computer Engineering / Information Technology
SUBJECT NAME DATA STRUCTURES
SUBJECT CODE 3330704
SEMESTER 3rd
4. Double hashing
It is techniques in which two hash function are used when there is an occurrence
of collision.
In this method 1 hash function is simple as same as division method. But for the
second hash function there are two important rules which are
1. It must never evaluate to zero.
2. Must sure about the buckets, that they are probed.
The hash functions for this technique are:
H1(key)=key % table size
H2(key)=P-(key mod P)
o Where, p is a prime number which should be taken smaller than the size
of a hash table.
Example: Let us consider we have to insert 67, 90,55,17,49.
BHAUMIK GELANI Page 20