Unit 1
Unit 1
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SEARCHING
⮚ Searching is an operation or a technique that
helps finds the place of a given element or value
in the list.
⮚ Linear Search
⮚ Binary Search
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
LINEAR SEARCH
⮚ traversing through the array from starting till the
desired element or value is found.
⮚ It compares the element to be searched with all
the elements present in the array and when the
element is matched successfully, it returns the
index of the element in the array. Else it returns -
1.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
BINARY SEARCH
⮚ Binary search looks for a particular item by
comparing the middle most item of the
collection.
⮚ If a match occurs, then the index of item is
returned.
⮚ If the middle item is greater than the item, then
the item is searched in the sub-array to the left of
the middle item.
⮚ Otherwise, the item is searched in the sub-array
to the right of the middle item.
⮚ This process continues on the sub-array as well
until the size of the sub array reduces to zero.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
BINARY SEARCH
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
BINARY SEARCH
Step 1 − Start searching data from middle of the
list.
Step 2 − If it is a match, return the index of the
item, and exit. Step 3 − If it is not a match, probe
position.
Step 4 − Divide the list using probing formula and
find the new middle.
Step 5 − If data is greater than middle, search in
higher sub- list.
Step 6 − If data is smaller than middle, search in
lower sub-list. Step 7 − Repeat until match.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
Sorting
• Refers to arranging data in a particular format or putting a
group of objects in a specific order.
• Applications
✔ database management
✔ data analysis
Types of sorting
▪ Insertion Sort,
▪ Selection Sort,
▪ Bubble Sort,
▪ Quick Sort,
▪ Heap Sort,
▪ Merge Sort.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
BUBBLE SORT
⮚ Bubble sort is also called as Sinking Sort or
Comparison Sort.
⮚ comparing each pair of adjacent items and
swapping them if they are in the wrong order.
⮚ This passing procedure is repeated until no
swaps are required, indicating that the list is
sorted.
⮚ Bubble sort gets its name because smaller
elements bubble toward the top of the list.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
BUBBLE
SORT
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SELECTION SORT
⮚ The selection sort algorithm sorts an array by
repeatedly finding the minimum element
(considering ascending order) from unsorted part
and putting it at the beginning.
⮚ The algorithm maintains two subarrays in a given
array.
⮚ The subarray which is already sorted.
⮚ Remaining subarray which is unsorted.
⮚ In every iteration of selection sort, the minimum
element (considering ascending order) from the
unsorted subarray is picked and moved to the
sorted subarray.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SELECTION SORT
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
INSERTION SORT
⮚ Insertion sort is a sorting algorithm in which the
elements are transferred one at a time to the
right position.
⮚ higher- ranked elements are moved to the right .
⮚ An insertion sort has the benefits of simplicity
and low overhead.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
INSERTION SORT
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SHELL SORT
⮚ In shell sort the whole array is first fragmented into K
segments where K is preferably a prime number.
⮚ After the first pass the whole array is partially sorted.
⮚ In the next pass, the value of K is reduced which increases
the size of each segment and reduces the number of
segments.
⮚ The next value of K is chosen so that it is relatively prime to
its previous value.
⮚ The process is repeated until K=1, while the array is sorted.
⮚ The insertion sort is applied to each segment so each
successive segment is partially sorted.
⮚ The shell sort is also called as “Diminishing Increment
Sort”, because the value of K decreases continuously.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SHELL SORT
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
RADIX SORT
⮚ Radix sort is one of the sorting algorithms used
to sort a list of integer numbers in order.
⮚ In radix sort algorithm, a list of integer
numbers will be sorted based on the digits of
individual numbers.
⮚ Sorting is performed from least significant
digit to the most significant digit.
⮚ Radix sort algorithm requires the number of
passes which are equal to the number of digits
present in the largest number among the list of
numbers.
⮚ For example, if the largest number is a 3 digit
SRI SHAKTHI
number then that list is sorted with 3 passes.
INSTITUTE OF ENGINEERING & TECHNOLOGY
RADIX SORT
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
HASHING AND HASH TABLE.
⮚ Hash Table: It is a data structure used for
storing and retrieving data quickly. Every
entry in the hash table is made using Hash
function.
⮚ Hash function is used to place data in the hash
table. It is also used to retrieve data from hash
table.
⮚ Hash function is Hash (Key) =Keyvalue %
TableSize
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
PROPERTIES OF HASH FUNCTION
⮚ The hash function should be simple to compute.
⮚ Number of collision should be less while placing
the record in the hash table. Ideally no collision
should occur. Such a function is called Perfect
Hash Function.
⮚ Hash function should produce such keys which
will get distributed uniformly over an array.
⮚ The hash function should depend on every bit of
the key. Thus the hash function that simply
extracts the portion of a key is not suitable.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
COLLISION AND COLLISION
RESOLUTION.
⮚ A Collision occurs when two or more elements are
hashed (mapped) to same value that is when two
key values hash to the same position.
⮚ Collision Resolution:
⮚ When two items hash to the same slot, there is a
systematic method for placing the second item in
the hash table. This process is called collision
resolution.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
COLLISION RESOLUTION
TECHNIQUES
Separate Chaining
Open Addressing (Closed Hashing)
Linear Probing
Quadratic Probing
Double Hashing
Multiple Hashing
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SEPARATE
CHAINING
▪ Separate Chaining is an open addressing.
▪ A pointer field is added to each location. When an
overflow occurs
this pointer is set to point to overflow blocks making a
linked list.
▪ In this method, the table can never overflow
Advantage:
⮚ More number of elements can be inserted.
Disadvantages
⮚ It requires pointers, which occupies more memory
space.
⮚ It takes more effort to perform a search. are only
extended
SRI SHAKTHIupon
INSTITUTE OF ENGINEERING & TECHNOLOGY
SEPARATE
CHAINING
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
OPEN ADDRESSING
• Open addressing also called as Closed
Hashing. It is
an alternative to resolve the collision.
• In this hashing if collision occurs, alternative cells
are tried
until an empty cell is found.
linear probing works:
➢In linear probing, the position in which a key to be
stored is found by sequentially searching all
position starting from the position calculated by the
hash function until an empty cell is found.
➢If the end of the table is reached and no empty cell
has
SRI been found, then the search is continued from
SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
QUADRATIC PROBING
• Quadratic probing is that if the element is
inserted into a space that is filled, then
1^2=1 element away then 2^2=4 element
away, then 3^2=9 elements away then
4^2=16 elements away and so on is
checked
h(k) mod Tablesize First element to Index2.
(h(k)+1) mod Second element to 3.
•Probe Sequence is For example Tablesize=16
Tablesize (h(k)+4) [(2+1)%16] Third element
mod Tablesize to 6.[(2+4)%16] Fourth
(h(k)+9) mod element to 11[(2+9)%16].
SRI SHAKTHI
Tablesize
INSTITUTE OF ENGINEERING & TECHNOLOGY
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
DOUBLE HASHING
One choice to choose a prime R< size and
hash2(x)=R-(X mod R)
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
REHASHING
➢If the table gets too full, then the rehashing
method builds a new table that is about twice as
big and scans down the entire original hash table,
computing the new hash value for each element
and inserting it in the new table.
Rehashing can be implemented in several ways such
as
• Rehash as soon as the table is full.
• Rehash only when an insertion fails.
• Rehash when the table reaches a certain load
factor.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY
EXTENDABLE HASHING
A hash table in which the hash function is the last
few bits of the key and the table refer to buckets.
Table entries with the same final bits may use the
same bucket. If a bucket overflows, it splits, and if
only one entry referred to it, the table doubles in
size.
If a bucket is emptied by deletion, entries using it
are changed to refer to an adjoining bucket, and
the table may be halved.
SRI SHAKTHI
INSTITUTE OF ENGINEERING & TECHNOLOGY