2. Searching
• The process of locating target data is known as searching.
• Searching is the process of finding the location of the target
among a list of object.
• The two basic search techniques are the followings:
-sequential search
-binary search
25/4/2020
2
3. Linear Search
• The linear search is a sequential search which uses a loop to step
through an array, starting with the first element.
• It compares each element with the value being searched for, and
stops when either the value is found or the end of the array is
encountered.
• A search will be unsuccessful if all the elements are read and
desired element is not found
25/4/2020
3
4. Linear Search Algorithm
• Linear Search( Array A, Values X)
• Step 1: set i to 1
• Step 2: if i > n then go to step 7
• Step 3: if A[i] = X then go to step 6
• Step 4: set i to i+1
• Step 5: go to step 2
• Step 6: print element x found at index i and go to step 8
• Step 7: print element not found
• Step 8: exit
25/4/2020
4
5. Advantages and Disadvantages
• The linear search is a simple- it is very easy to understand and
implement
• It does not require the data in the array to be stored in any
particular order
• DISADVANTAGES
• It has very poor efficiency because it takes lots of comparisons to
find a particular record in big files
• Linear search is slower than other searching algorithms
25/4/2020
5
6. Binary Search
What is Binary Search?
• Binary search is an extremely
efficient algorithm when it is
compared to linear search.
• It searches data in minimum possible
comparisons.
• Binary search use sorted array by
repeatedly dividing the search
interval in half.
Algorithm
• Step 1: compare x with the middle
element.
• Step 2:If x matches with middle
element, we return the mid index.
• Step 3: Else if x is greater than the
mid element, search on right half.
• Step 4: Else if x is smaller than the
mid element, search on left half
25/4/2020
6
8. Hashing
• Hashing is a technique where we can compute the location of the
desired record in order to retrieve it in a single access (or
comparison) .
• Hashing involves less key comparison and searching can
performed in constant time.
• The goal of hashed search is to find the target data in only one
test i.e. O(1)(best complexity).
• Hashing is an efficient method to store and retrieve elements.
• In hashing function the keys are stored in array which is called
hash table.
25/4/2020
8
9. Hashing function
• The basic idea of hash function Is the transformation of the key into the
corresponding location in the hash table.
• A Hash function H can be defined as a function as a function that takes key as
input and transforms it into a hash table index.
• The values returned by hash function are called hash values , hash codes, hash
sums, or simply hashes.
• Hash function are two types: 1) distribution –independent function ,2)
distribution-dependent function.
• The distribution independent hash functions are : a) division method, b) mid
square method, c) digit folding method.
25/4/2020
9
10. Hash Collision
• When an element is inserted, if it hashes to the same value as an
already inserted element, then we have a collision.
• A situation in which the hash function returns the same hash key
for more then one record , it is called as collision.
25/4/2020
10
11. Collision resolution technique
• If there is a problem of collision occurs then it can be handled by apply
some technique.these techniques are called as collision resolution
techniques.
• If the element to be inserted is mapped to the same location, where an
element is already inserted then we have a collision and it must be
resolved.
• There are several strategies for collision resolution. The most commonly
used are:
1) separate chaining- it used with open hashing.
2) open addressing- it used with closed hashing.
25/4/2020
11
12. Separate Chaining(open hashing)
• Separate chaining based on collision avoidance.
• The idea is to keep a list of all elements that hash to the same value.
-the array elements are pointers to the first nodes of the lists.
-A new item is inserted to the front of the list.
• Advantages:
- batter space utilization for large items.
-simple collision handling : searching link list.
-deletion is quick and easy : deletion from the linked list.
25/4/2020
12
14. Sorting
• Sorting is the operation of arranging the records of a table according to the key
value of each record, or it can be defined as the process of converting an
unordered set of elements to an ordered set of elements.
• Sorting is a process of organizing data in a certain order to help retrieve it more
efficiently.
• Sorting techniques can be divided into two categories. These are:
1) Internal sorting , 2) external sorting.
• Any sort algorithm that uses main memory exclusively during the sorting is
called as internal sort algorithm
• Any sort algorithm that uses external memory, such as tape or disk, during the
sorting is called as external sorting.
• Internal sorting is faster then external sorting. 25/4/2020
14
15. Internal Sorting |External Sorting
• The various internal sorting techniques are
the following:
1. Bubble sort
2. Selection sort
3. Insertion sort
4. Quick sort
5. Shell sort
6. Heap sort
7. Radix sort
8. Bucket sort
• Merge sort is used in external
sorting
25/4/2020
15
16. Bubble Sort
What is bubble sort?
• Bubble sort is simple algorithm
which is used to sort a given set of n
elements provided in form of an
array with n number of elements.
• Bubble sort compares all the
element one by one and sort them
based on their values.
• The bubble sort derives its name
from the fact that the smallest data
item bubbles up to the top of the
sorted array.
Algorithm for bubble sort
1. Starting with the first element(index=0),
compare the current element with the
next element of the array.
2. If the current element is greater than the
next element of the array, swap them.
3. If the current element is less than the
next element, move to the next element.
Repeat step 1
25/4/2020
16
17. Selection Sort
What is selection sort?
• Selection sort is a simple sorting algorithm.
• In this technique, the smallest element is
interchanged with the first element of the array.
• Then the next smallest element is interchanged with
the second element of the array.
• This process of searching the next smallest element
and placing it in its proper position continues until
all records have been sorted in ascending order.
Algorithm
For i<-1 to n-1 do
min<-i
for j<-i+1 to n do
if A[j]<A[i] then
min<-j
if min!=i then
temp<-A[i]
A[i]<-A[min]
A[min]<-temp
25/4/2020
17
18. Radix sort
• Radix sort algorithm different than other sorting algorithms .
• It does not use key comparisons to sort an array.
• The radix sort treats each data items as a character string.
• First it groups data items according to their rightmost character and put
these groups into order with respect to this right most character.
• Then combine these groups.
• We repeat these groupings and combining operation for all other
character positions in the data items from the right most to the left
most character position.
• At the end, the sort operation will be completed
25/4/2020
18
19. Heap Sort
• Heap is a special tree based data structure, that satisfies the
following special heap properties: 1)shape property , 2)heap
property
• Heap sort is the one of the fastest sorting algorithm, which
achieves the speed as that of quick sort and merge sort.
• The advantages of heap sort are as follows:
• It dose not use recursion, and it is efficient for any data order.
• It achieves the worst-case bounds better than those of quick sort.
• And for the list, it is better than merge sort since it needs only a
small and constant of space apart from the list being sorted
25/4/2020
19
20. Merge Sort(external sort)
• The most common algorithm used in external sorting is the merge sort.
• Merging is the process of combining two or more sorted files into the third
sorted file.
• The merge sort algorithm is base on the classical divide-and-conquer paradigm.
• Divide-and-conquer algorithm works in three steps:
1. Divide the problem into multiple small problems.
2. Conquer the subproblems by solving them. The idea is to break down the
problem into atomic subproblems, where they are actually solved.
3. Combine the solutions of the subproblems to find the solution of the actual
problem.
25/4/2020
20