unit-ii
unit-ii
Searching :
“ Searching is a techniques of finding an element in a
given list of elements.”
Types of Searching
• Linear search
• Binary search
• Sentinel search
SEARCH TECHNIQUES
Sequential search
Binary search
Fibonacci search
Hashed search
Index sequential
searchearch
4
Linear Search
The linear search is a sequential search, which uses a loop
to step through an array, starting with the first element.
for, and stops when either the value is found or the end of
the array is encountered.
17
Binary Search
2. Calculate middle = (low + high) / 2.
= (0 + 8) / 2 =
4.
}
Binary Search Routine
}
Sentinel search
This additional entry at the end of the list is called as
Sentinel.
The speed of sequential search can be improved by storing the key
being searched at end of the array.
This will eliminate extra comparision inside the loop for number od
element in the array.
30
Fibonacci search
Fibonacci search technique is a method of searching a sorted
array using a divide and conquer algorithm that narrows down
possible locations with the aid of Fibonacci numbers.
Compared to binary search where the sorted array is divided
into two equal-sized parts, one of which is examined further,
Fibonacci search divides the array into two parts that have sizes
that are consecutive Fibonacci numbers.
31
Fibonacci search
Fibonacci search changes the binary search algorithm
slightly
33
Fibonacci search
Algorithm
Given a table of records R1, R2, …, RN whose keys are in
increasing order K1 < K2 < … < KN, the algorithm searches for a
given argument K. Assume N+1 = Fk+1
Step 1. [Initialize] i ← Fk, p ← Fk-1, q ← Fk-2 (throughout the
algorithm, p and q will be consecutive Fibonacci numbers)
Step 2. [Compare] If K < Ki, go to Step 3; if K > Ki go to Step 4; and
if K = Ki, the algorithm terminates successfully.
Step 3. [Decrease i] If q=0, the algorithm terminates
unsuccessfully. Otherwise set (i, p, q) ← (p, q, p - q) (which moves
p and q one position back in the Fibonacci sequence); then return
to Step 2
Step 4. [Increase i] If p=1, the algorithm terminates unsuccessfully.
Otherwise set (i,p,q) ← (i + q, p - q, 2q - p) (which moves p and q
two positions back in the Fibonacci sequence); and return to Step 234
SORTING
“Sorting is the process ordering a list of element in either
ascending or descending order.”
37
STABILITY OF SORTING
A sorting method is said to be stable if at the end of
the method, identical elements occur in the same
relative order as in the original unsorted set
1. EXAMPLE :
38
SORT EFFICIENCY
Sort efficiency is a measure of the relative
efficiency of a sort
It is usually an estimate of the number of
comparisons and data movement required to sort
the data
39
PASSES IN SORTING
During the sorted process, the data is traversed many times
40
BUBBLE SORTING
Bubble sort is a simple sorting algorithm.
This sorting algorithm is comparison-based algorithm in which
each pair of adjacent elements is compared and the elements
are swapped if they are not in order.
This algorithm is not suitable for large data sets as its average
and worst case complexity are of Ο(n2) where n is the number
of items.
How Bubble Sort Works?
We take an unsorted array for our example. Bubble sort takes
Ο(n2) time so we're keeping it short and precise.
Bubble sort start with first two element, compare them to
check which one is greater. And swap it.
41
Algorithm Bubble sorting
42
Insertion Sort
ALGORITHM OF INSERTION SORT
Selection Sort
Quick Sort Algorithm
Quick sort is based on divide-and-conquer
strategy
90
How merge sort works
• To understand merge sort, we take
unsorted array as depicted below − a
n
We compare and swap the values, if required, in the original array. After
this step, this array should look like this −
And finally, we sort the rest of the array using interval of value 1.
Shell sort uses insertion sort to sort the array. The step by step
depiction is shown below −
Shell sort Algorithm
• We shall now see the algorithm for shell sort.
• Step 1 − Initialize the value of h
• Step 2 − Divide the list into smaller sub-list of
equal interval h
• Step 3 − Sort these sub-lists using
insertion sort
• Step 4 − Repeat until complete list is sorted
Shell sort Algorithm
8, 3, 2, 11, 5 , 14, 00, 9, 4, 20
Perform shell sort
Bucket Sort
For example, suppose that we are sorting elements
from the set of integers in the interval [0, m − 1]. The
bucket sort uses m buckets or counters
11
0
Bucket Sort
11
1
Bucket Sort
11
2
Radix Sort
• Radix Sort is generalization of Bucket Sort
Disadvantages
• Still, there are some tradeoffs for Radix Sort that can make it less
preferable than other sorts.
• The speed of Radix Sort largely depends on the inner basic
operations, and if the operations are not efficient enough, Radix
Sort can be slower than some other algorithms such as Quick Sort
and Merge Sort.
• In the example above, the numbers were all of equal length, but
many times, this is not the case. If the numbers are not of the same
length, then a test is needed to check for additional digits that need
sorting. This can be one of the slowest parts of Radix Sort, and it is
one of the hardest to make efficient.
• Radix Sort can also take up more space than other sorting
algorithms, since in addition to the array that will be sorted, you
need to have a sublist for each of the possible digits or letters.
HEAP SORT
Heap sort is one of the fastest sorting algorithms, which
achieves the speed as that of quick sort and merge sort
The advantages of heap sort are as follows: it does not use
recursion, and it is efficient for any data order
And for the list, it is better than merge sort since it needs
only a small and constant amount of space apart from the
list being sorted
12
4
Heap Sort
12
5
Heap Sort
ALGORITHM
1. Build a heap tree with a given set of data
12
6
Analysis of Heap Sort
12
7
Heap Sort
12
8
Heap Sort
12
9
Heap Sort
13
0
Heap Sort
13
1
Heap Sort
13
2
Heap Sort
13
3
Heap Sort
13
4
Heap Sort
13
5
Heap Sort
13
6
Heap Sort
13
7
Comparisoin of sorting
13
8
Comparisoin of sorting
13
9
Comparisoin of sorting
14
0
COMPARISON OF ALL
SORTING METHODS
14
1
14
2
14
3
14
4
14
5
14
6
14
7
14
8
BUBBLE SORT
14
9
INSERTION SORT
15
0
INSERTION SORT
BUCKET SORT
15
1