SlideShare a Scribd company logo
Pune Vidyarthi Griha’s
COLLEGE OF ENGINEERING, NASHIK – 3.
“ Searching & Sorting ”
By
Prof. Anand N. Gharu
(Assistant Professor)
PVGCOE Computer Dept.
10 September 2019
.
Introduction
Searching :
“ Searching is a techniques of finding an element in a
given list of elements.”
List of element could be represented using an
1. Array
2. Linked list
3. Binary tree
4. B-tree
5. Heap
Why do we need searching?
Searching is one of the core computer science
algorithms.
We know that today’s computers store a lot of
information.
To retrieve this information proficiently we need
very efficient searching algorithms.
Types of Searching
• Linear search
• Binary search
• Sentinel search
 Sequential search
 Binary search
 Fibonacci search
 Hashed search
 Index sequential
searchearch
4
SEARCH TECHNIQUES
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.
 If the value being searched is not in the array, the
algorithm will unsuccessfully search to the end of the
array.
Linear Search
 Since the array elements are stored in linear order
searching the element in the linear order make it easy and
efficient.
 The search may be successful or unsuccessfully. That is, if
the required element is found them the search is
successful other wise it is unsuccessfully.
Advantages Linear Search
 easy to understand.
It operates on both sorted and unsorted list
It doest not require array to be in order
Easy to implement
Time complexity O(n)
Disadvantages Linear Search
 Linear search is not efficient when list is large
Maximum no. of comparision are N(n Element).
Not suitable for large problem.
You need to search whole list.
Linear search is slower.
Linear Search Algorithm
Consider an integer type array A with size n. So list of elements
from that array are,
A[0], A[1], A[2], A[3],………………, A[n-1]
1. Declare and initialize one variable which contents the number
to be search in an array A.
( variable key is declared)
2. Start Comparing each element from array A with the key
LOOP: A[size] == key
Repeat step no 2 while A[size] key
3. if key is found, display the location of element(index+1)
or else display message KEY NOT FOUND
4. Terminate the program successfully
Linear Search Algorithm
printf(“accept number to search”);
scanf( key );
for( i=0 ;i<n ;i++)
{
if( A [ i ] == key )
{
printf( key is FOUND);
break;
}
}
if( i==n )
{
printf(NOT FOUND);
}
Analysis of Linear Search
Complexity of linear search :
1.Best Case = O(1)
2.Average Case = O(n)
3.Worst case = O(n)
Binary Search
“Binary search is an searching algorithm which is used
to find element from the sorted list”
Concepts :
- Algorithm can be applied only on sorted data
- Mid = lower/upper - formula used to find mid
- Given element is compared with middle element of
the list.
- If key=mid then element is found
- Otherwise list divide into two part.(key <mid) (>mid)
- First to mid-1 or mid+1 to last.
Binary Search
Concepts :
- If given element is less than middle element then
continue searching in first part (first+mid-1)
otherwise searching is second part(mid+1 to last).
- Repeat above step still element is found.
Binary Search
Assume that two variables are declared, variable first and last,
they denotes beginning and ending indices of the list under
consideration respectively.
Step 1. Algorithm compares key with middle element from
list ( A[middle] == key ), if true go to step 4 or else go to next.
Step 2. if key < A[ middle ], search in left half of the list or
else go to step 3
Step 3. if key > A[ middle ], search in right half of the list or go
to step 1
Step 4. display the position of key else display message “NOT
FOUND”
Binary Search algorithm
int i, first=0, last=n-1, middle;
while( last>=first )
{
middle = (first + last)/2;
if( key > A[middle] )
{ first = middle + 1; }
else if ( key < A[middle] )
{ last= middle – 1; }
else
{ printf( FOUND ) }
}
if( last < first )
{
printf( NOT FOUND );
}
Advatages Binary Search
1.Binary search is optimal searching algorithms
2.Excellent time efficiency
3.Suitable for large list.
4.Faster because no need to check all element.
5.Most suitable for sorted array
6.It can be search quickly
7.Time complexity O(log n)
Disadvatages Binary Search
1.Element must be sorted
2.Need to find mid element
3.Bit more complicated to implement and test
4.It does not support random access.
5.Key element require to compare with middle.
• Element is searched by
scanning the entire list
from first element to the
last
• Many times entire list is
search
• Simple to implementation
• Time complexity is O(n)
• Less efficient sort
• First list is divided into
two sub-lists. Then middle
element is compared with
key element and then
accordingly left or right
sub-list is searched
• Only sub-list is search
• Complex to implement,
since it involves
computation for finding
the middle element
• Time complexity is O(log2
n)
• More efficient sort
Linear Search Vs Binary Search
17
Binary Search
© Reem Al-
Attas
Binary Search
2. Calculate middle = (low + high) / 2.
= (0 + 8) / 2 =
4.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (0 + 3) / 2 =
1.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1
9/6/201
7
© Reem Al-
Attas
Binary Search
Repeat 2. Calculate middle = (low + high) / 2.
= (2 + 3) / 2 =
2.
If 37 == array[middle] return middle
Else if 37 < array[middle] high = middle -1
Else if 37 > array[middle] low = middle +1
9/6/201
7
© Reem Al-
Attas
Binary Search
9/6/201
7
© Reem Al-
Attas
Binary Search
9/6/201
7
© Reem Al-
Attas
Binary Search
9/6/201
7
© Reem Al-
Attas
Binary Search
9/6/201
7
© Reem Al-
Attas
Binary Search
9/6/201
7
© Reem Al-
Attas
Binary Search Routine
}
Binary Search Routine
}
 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
Sentinel 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
 Instead of halving the index for a search, a Fibonacci
number is subtracted from it
 The Fibonacci number to be subtracted decreases as the
 size of the listdecreases
 Note that Fibonacci search sorts a list in a non decreasing
order
 Fibonacci search starts searching the target by comparing
 it with the element at Fkth location 32
Fibonacci search
33
Cases in 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
Fibonacci search
 “Sorting is the process ordering a list of element in either
ascending or descending order.”
 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
35
SORTING
 Any sort algorithm that uses main memory exclusively during
the sorting is called as internal sort algorithm
 Internal sorting is faster than externalsorting
Internal Sorting techniques :
1. Bubble sort
2. Selection sort
3. Insertion sort
4. Quick sort
5. Shell sort
6. Heap sort
7. Radix sort
8. Bucket sort 36
INTERNAL SORTING(types)
 Any sort algorithm that uses external memory, such
as tape or disk, during the sorting is called as
external sort algorithm
 Merge sort is used in externalsorting
37
EXTERNAL 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
STABILITY OF SORTING
 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
SORT EFFICIENCY
 During the sorted process, the data is traversed many times
 Each traversal of the data is referred to as a sort pass
 In addition, the characteristic of a sort pass is the
placement of one or more elements in a sorted list
40
PASSES IN 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
BUBBLE SORTING
42
Algorithm Bubble sorting
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Insertion Sort
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
ALGORITHM OF INSERTION SORT
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Selection Sort
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Quick Sort Algorithm
 Quick sort is based on divide-and-conquer
strategy
 Quick sort is thus in-place, divide-and-conquer based
massively recursive sort technique
 This technique reduces unnecessary swaps and
moves the element at great distance in one move
Quick Sort Algorithm
The recursive algorithm consists of four steps:
 If there is one or less element in the array to be sorted,
return immediately
 Pick an element in the array to serve as a ‘pivot’
usually the left-most element in the list)
 Partition the array into two parts—one with elements
smaller than the pivot and the other with elements
larger than the pivot by traversing from both the ends
and performing swaps if needed
 Recursively repeat the algorithm for bothpartitions
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Quick Sort Algorithm
Merge Sort Algorithm
• Merge sort is a sorting technique based on
divide and conquer technique. With Average
case and worst-case time complexity being
Ο(n log n), it is one of the most respected
algorithms.
• Merge sort first divides the array into equal
halves and then combines them in a sorted
manner.
 The most common algorithm used in external
sorting is the merge sort
 Merging is the process of combiningtwo or more
sorted files into the third sortedfile
 We can use a techniqueof merging two sorted lists
 Divide and conquer is a general algorithm design
paradigm that is used for mergesort
89
Merge Sort
 Time Complexity T(n) = O(n logn)
90
Merge Sort
How merge sort works
take
a
n
• To understand merge sort, we
unsorted array as depicted below −
• We know that merge sort first divides the
whole array iteratively into equal halves
unless the atomic values are achieved. We see
here that an array of 8 items is divided into
two arrays of size 4.
• This does not change the sequence of
appearance of items in the original. Now we
divide these two arrays into halves.
• We further divide these arrays and we achieve
atomic value which can no more be divided.
• Now, we combine them in exactly same
manner they were broken down.
• We first compare the element for each list and
then combine them into another list in sorted
manner. We see that 14 and 33 are in sorted
positions. We compare 27 and 10 and in the
target list of 2 values we put 10 first, followed
by 27. We change the order 19 and 35. 42 and
44 are placed sequentially.
• In next iteration of combining phase, we
compare lists of two data values, and merge
them into a list of four data values placing all
in sorted order.
• After final merging, the list should look like
this −
Algorithm of merge sort
• Merge sort keeps on dividing the list into equal
halves until it can no more be divided. By
definition, if it is only one element in the list, it
is sorted. Then merge sort combines smaller
sorted lists keeping the new list sorted too.
– Step 1 − divide the list recursively into two halves
until it can no more be divided.
– Step 2 − if it is only one element in the list it is
already sorted, return.
– Step 3 − merge the smaller lists into new list in
sorted order.
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Shell Sort
• Shell sort is a highly efficient sorting algorithm
and is based on insertion sort algorithm. This
algorithm avoids large shifts as in case of
insertion sort if smaller value is very far right and
have to move to far left.
• This algorithm uses insertion sort on widely
spread elements first to sort them and then sorts
the less widely spaced elements. This spacing is
termed as interval. This interval is calculated
based on Knuth's formula as −
• h = h * 3 + 1
where − h is interval with initial value 1
This algorithm is quite efficient for medium
sized data sets as its average and worst case
complexity are of O(n^2) where n are no. of
items.
How shell sort works
• We take the below example to have an idea, how
shell sort works?
• We take the same array we have used in our
previous examples. {35,33,42,10,14,19,27,44}
• For our example and ease of understanding we
take the interval of 4.
• And make a virtual sublist of all values located at
the interval of 4 positions. Here these values are
{35, 14}, {33, 19}, {42, 27} and {10, 14}
We compare values in each sub-list and swap them (if necessary) in
the original array. After this step, new array should look like this −
Then we take interval of 2 and this gap generates two sublists - {14, 27,
35,
42}, {19, 10, 33, 44}
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 −
Unit 6 dsa SEARCHING AND SORTING
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
 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
 The ith counter/bucket keeps track of the number of
occurrences of the ith element of the list
11
0
Bucket Sort
Illustration of how this is done for m = 9
11
1
Bucket Sort
Illustration of how this is done for m = 9
11
2
Bucket Sort
Radix Sort
• Radix Sort is generalization of Bucket Sort
• To sort Decimal Numbers radix/base will be used
as 10. so we need 10 buckets.
• Buckets are numbered as 0,1,2,3,…,9
• Sorting is Done in the passes
• Number of Passes required for sorting is number
of digits in the largest number in the list.
 Radix sort is a generalization of bucket sorting
 Radix sort works in threesteps:
 Distribute all elements into m buckets
 Here m is a suitable integer, for example, to sort
decimal numbers with radix 10
 We take 10 buckets numbered as 0, 1, 2, …, 9
 For sorting strings, we may need 26 buckets, and
so on
 Sort each bucket individually
 Finally, combine all buckets
11
4
Radix Sort
Ex.
Range
0 to 99
0 to 999
0 to 9999
Passes
2 Passes
3 Passes
4 Passes
• In First Pass number sorted based on Least
Significant Digit and number will be kept in same
bucket.
• In 2nd Pass, Numbers are sorted on second least
significant bit and process continues.
• At the end of every pass, numbers in buckets are
merged to produce common list.
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
RADIX SORT EXAMPLE
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
• Radix Sort is very simple, and a computer can do it fast. When it is
programmed properly, Radix Sort is in fact one of the fastest
sorting algorithms for numbers or strings of letters.
• Average case and Worst case Complexity - O(n)
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 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
 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 amount of space apart from the
list being sorted
12
4
HEAP SORT
 The steps for building heap sort are as follows:
 Build the heap tree
Start delete heap operation storing each deleted
element at the end of the heaparray
12
5
Heap Sort
 ALGORITHM
 1. Build a heap tree with a given set of data
 (a) Delete root node fromheap
 (b) Rebuild the heap afterdeletion
 (c) Place the deleted node in the output
12
6
Continue with step (2) until the heap tree is
empty
Heap Sort
Analysis of HeapSort
12
7
 The time complexity is stated as follows:
 Best case O(n logn)
 Average case O(nlogn)
 Worst case O(nlogn)
HeapSort
12
8
HeapSort
12
9
HeapSort
13
0
HeapSort
13
1
HeapSort
13
2
HeapSort
13
3
HeapSort
13
4
HeapSort
13
5
HeapSort
13
6
HeapSort
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
14
9
BUBBLE SORT
15
0
INSERTION SORT
15
1
INSERTION SORT
BUCKET SORT
Thanks ……!!!!
15
2
Prof. ANAND GHARU
ASSISTANT PROFESSOR
Blog : anandgharu.wordpress.com
Ad

More Related Content

What's hot (20)

Selection sort
Selection sortSelection sort
Selection sort
smlagustin
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
International Islamic University
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Binary search
Binary searchBinary search
Binary search
Gaurav Solanki
 
Selection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexitySelection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexity
Computer_ at_home
 
Merge Sort vs Quick Sort presentation
Merge Sort vs Quick Sort presentationMerge Sort vs Quick Sort presentation
Merge Sort vs Quick Sort presentation
maharajdey
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Threaded Binary Tree.pptx
Threaded Binary Tree.pptxThreaded Binary Tree.pptx
Threaded Binary Tree.pptx
pavankumarjakkepalli
 
Bubble sort
Bubble sortBubble sort
Bubble sort
Ayush Pandey
 
Binary search
Binary searchBinary search
Binary search
AparnaKumari31
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
Heap_Sort1.pptx
Heap_Sort1.pptxHeap_Sort1.pptx
Heap_Sort1.pptx
sandeep54552
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
A* algorithm
A* algorithmA* algorithm
A* algorithm
Komal Samdariya
 
parallel Merging
parallel Mergingparallel Merging
parallel Merging
Richa Kumari
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Quick sort
Quick sortQuick sort
Quick sort
amar kakde
 
Informed Search in Artifical Intelligence
Informed Search in Artifical IntelligenceInformed Search in Artifical Intelligence
Informed Search in Artifical Intelligence
Dr. Anand Bihari
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
Drishti Bhalla
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Selection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexitySelection sort(sorting algorithm in data structure) and its time complexity
Selection sort(sorting algorithm in data structure) and its time complexity
Computer_ at_home
 
Merge Sort vs Quick Sort presentation
Merge Sort vs Quick Sort presentationMerge Sort vs Quick Sort presentation
Merge Sort vs Quick Sort presentation
maharajdey
 
Linear search algorithm
Linear search algorithmLinear search algorithm
Linear search algorithm
NeoClassical
 
Searching & Sorting Algorithms
Searching & Sorting AlgorithmsSearching & Sorting Algorithms
Searching & Sorting Algorithms
Rahul Jamwal
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
Linear and Binary search
Linear and Binary searchLinear and Binary search
Linear and Binary search
Nisha Soms
 
Informed Search in Artifical Intelligence
Informed Search in Artifical IntelligenceInformed Search in Artifical Intelligence
Informed Search in Artifical Intelligence
Dr. Anand Bihari
 

Similar to Unit 6 dsa SEARCHING AND SORTING (20)

Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.pptChapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
All Searching and Sorting Techniques in Data Structures
All Searching and Sorting Techniques in Data StructuresAll Searching and Sorting Techniques in Data Structures
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi
 
Chapter 2. data structure and algorithm
Chapter  2. data structure and algorithmChapter  2. data structure and algorithm
Chapter 2. data structure and algorithm
SolomonEndalu
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
Prashant Rai
 
Searching and Sorting algorithms and working
Searching and Sorting algorithms and workingSearching and Sorting algorithms and working
Searching and Sorting algorithms and working
RitikaLohiya2
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
Unit 8 searching and hashing
Unit   8 searching and hashingUnit   8 searching and hashing
Unit 8 searching and hashing
Dabbal Singh Mahara
 
Searching
SearchingSearching
Searching
A. S. M. Shafi
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjkAJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Binary Search Algorithm.pptx
Binary Search Algorithm.pptxBinary Search Algorithm.pptx
Binary Search Algorithm.pptx
BhagyashreeMadan1
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
Searching and Sorting Algorithms in Data Structures
Searching and Sorting Algorithms  in Data StructuresSearching and Sorting Algorithms  in Data Structures
Searching and Sorting Algorithms in Data Structures
poongothai11
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
nikshaikh786
 
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.pptChapter 3 - Elementary Searching and Sorting Algorithms.ppt
Chapter 3 - Elementary Searching and Sorting Algorithms.ppt
AbdisaAwel
 
All Searching and Sorting Techniques in Data Structures
All Searching and Sorting Techniques in Data StructuresAll Searching and Sorting Techniques in Data Structures
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
haramaya university
 
Chapter 2. data structure and algorithm
Chapter  2. data structure and algorithmChapter  2. data structure and algorithm
Chapter 2. data structure and algorithm
SolomonEndalu
 
searching in data structure.pptx
searching in data structure.pptxsearching in data structure.pptx
searching in data structure.pptx
chouguleamruta24
 
Ocw chp6 2searchbinary
Ocw chp6 2searchbinaryOcw chp6 2searchbinary
Ocw chp6 2searchbinary
Prashant Rai
 
Searching and Sorting algorithms and working
Searching and Sorting algorithms and workingSearching and Sorting algorithms and working
Searching and Sorting algorithms and working
RitikaLohiya2
 
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...PPS 5.5.BASIC ALGORITHMS  SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
PPS 5.5.BASIC ALGORITHMS SEARCHING (LINEAR SEARCH, BINARY SEARCH ETC.), BASI...
Sitamarhi Institute of Technology
 
DSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdfDSA Lec 5+6(Search+Sort) (1).pdf
DSA Lec 5+6(Search+Sort) (1).pdf
MustafaJutt4
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjkAJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
Binary Search Algorithm.pptx
Binary Search Algorithm.pptxBinary Search Algorithm.pptx
Binary Search Algorithm.pptx
BhagyashreeMadan1
 
data_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptxdata_structure_Chapter two_computer.pptx
data_structure_Chapter two_computer.pptx
Mohammed472103
 
Ad

More from PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK (20)

BASICS OF COMPUTER
BASICS OF COMPUTERBASICS OF COMPUTER
BASICS OF COMPUTER
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 6 ppts web services
Wt unit 6 ppts web servicesWt unit 6 ppts web services
Wt unit 6 ppts web services
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side frameworkWt unit 5 client &amp; server side framework
Wt unit 5 client &amp; server side framework
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 4 server side technology-2
Wt unit 4 server side technology-2Wt unit 4 server side technology-2
Wt unit 4 server side technology-2
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technologyWt unit 2 ppts client sied technology
Wt unit 2 ppts client sied technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
web development process WT
web development process WTweb development process WT
web development process WT
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 5 dsa QUEUE
Unit 5 dsa QUEUEUnit 5 dsa QUEUE
Unit 5 dsa QUEUE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 3 dsa LINKED LIST
Unit 3 dsa LINKED LISTUnit 3 dsa LINKED LIST
Unit 3 dsa LINKED LIST
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTUREUnit 2 dsa LINEAR DATA STRUCTURE
Unit 2 dsa LINEAR DATA STRUCTURE
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Unit 1 dsa
Unit 1 dsaUnit 1 dsa
Unit 1 dsa
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 2 ppts client side technology
Wt unit 2 ppts client side technologyWt unit 2 ppts client side technology
Wt unit 2 ppts client side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 1 ppts web development process
Wt unit 1 ppts web development processWt unit 1 ppts web development process
Wt unit 1 ppts web development process
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Wt unit 3 server side technology
Wt unit 3 server side technologyWt unit 3 server side technology
Wt unit 3 server side technology
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
OPERATING SYSTEM
OPERATING SYSTEMOPERATING SYSTEM
OPERATING SYSTEM
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
LEX & YACC TOOL
LEX & YACC TOOLLEX & YACC TOOL
LEX & YACC TOOL
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
PL-3 LAB MANUAL
PL-3 LAB MANUALPL-3 LAB MANUAL
PL-3 LAB MANUAL
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERINGCOMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
COMPUTER LABORATORY-4 LAB MANUAL BE COMPUTER ENGINEERING
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Deld model answer nov 2017
Deld model answer nov 2017Deld model answer nov 2017
Deld model answer nov 2017
PUNE VIDYARTHI GRIHA'S COLLEGE OF ENGINEERING, NASHIK
 
Ad

Recently uploaded (20)

Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control Monthly May 2025
Water Industry Process Automation & Control
 
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic AlgorithmDesign Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Design Optimization of Reinforced Concrete Waffle Slab Using Genetic Algorithm
Journal of Soft Computing in Civil Engineering
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Using the Artificial Neural Network to Predict the Axial Strength and Strain ...
Journal of Soft Computing in Civil Engineering
 
Working with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to ImplementationWorking with USDOT UTCs: From Conception to Implementation
Working with USDOT UTCs: From Conception to Implementation
Alabama Transportation Assistance Program
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 
Machine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATIONMachine Learning basics POWERPOINT PRESENETATION
Machine Learning basics POWERPOINT PRESENETATION
DarrinBright1
 
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
01.คุณลักษณะเฉพาะของอุปกรณ์_pagenumber.pdf
PawachMetharattanara
 
Design of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdfDesign of Variable Depth Single-Span Post.pdf
Design of Variable Depth Single-Span Post.pdf
Kamel Farid
 
Machine foundation notes for civil engineering students
Machine foundation notes for civil engineering studentsMachine foundation notes for civil engineering students
Machine foundation notes for civil engineering students
DYPCET
 
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdfATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ATAL 6 Days Online FDP Scheme Document 2025-26.pdf
ssuserda39791
 
Evonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdfEvonik Overview Visiomer Specialty Methacrylates.pdf
Evonik Overview Visiomer Specialty Methacrylates.pdf
szhang13
 
acid base ppt and their specific application in food
acid base ppt and their specific application in foodacid base ppt and their specific application in food
acid base ppt and their specific application in food
Fatehatun Noor
 
twin tower attack 2001 new york city
twin  tower  attack  2001 new  york citytwin  tower  attack  2001 new  york city
twin tower attack 2001 new york city
harishreemavs
 
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
6th International Conference on Big Data, Machine Learning and IoT (BMLI 2025)
ijflsjournal087
 
2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt2.3 Genetically Modified Organisms (1).ppt
2.3 Genetically Modified Organisms (1).ppt
rakshaiya16
 
hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .hypermedia_system_revisit_roy_fielding .
hypermedia_system_revisit_roy_fielding .
NABLAS株式会社
 
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjjseninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
seninarppt.pptx1bhjiikjhggghjykoirgjuyhhhjj
AjijahamadKhaji
 
SICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introductionSICPA: Fabien Keller - background introduction
SICPA: Fabien Keller - background introduction
fabienklr
 
Agents chapter of Artificial intelligence
Agents chapter of Artificial intelligenceAgents chapter of Artificial intelligence
Agents chapter of Artificial intelligence
DebdeepMukherjee9
 
Applications of Centroid in Structural Engineering
Applications of Centroid in Structural EngineeringApplications of Centroid in Structural Engineering
Applications of Centroid in Structural Engineering
suvrojyotihalder2006
 
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software ApplicationsJacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia - Excels In Optimizing Software Applications
Jacob Murphy Australia
 

Unit 6 dsa SEARCHING AND SORTING

  • 1. Pune Vidyarthi Griha’s COLLEGE OF ENGINEERING, NASHIK – 3. “ Searching & Sorting ” By Prof. Anand N. Gharu (Assistant Professor) PVGCOE Computer Dept. 10 September 2019 .
  • 2. Introduction Searching : “ Searching is a techniques of finding an element in a given list of elements.” List of element could be represented using an 1. Array 2. Linked list 3. Binary tree 4. B-tree 5. Heap
  • 3. Why do we need searching? Searching is one of the core computer science algorithms. We know that today’s computers store a lot of information. To retrieve this information proficiently we need very efficient searching algorithms. Types of Searching • Linear search • Binary search • Sentinel search
  • 4.  Sequential search  Binary search  Fibonacci search  Hashed search  Index sequential searchearch 4 SEARCH TECHNIQUES
  • 5. 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.  If the value being searched is not in the array, the algorithm will unsuccessfully search to the end of the array.
  • 6. Linear Search  Since the array elements are stored in linear order searching the element in the linear order make it easy and efficient.  The search may be successful or unsuccessfully. That is, if the required element is found them the search is successful other wise it is unsuccessfully.
  • 7. Advantages Linear Search  easy to understand. It operates on both sorted and unsorted list It doest not require array to be in order Easy to implement Time complexity O(n)
  • 8. Disadvantages Linear Search  Linear search is not efficient when list is large Maximum no. of comparision are N(n Element). Not suitable for large problem. You need to search whole list. Linear search is slower.
  • 9. Linear Search Algorithm Consider an integer type array A with size n. So list of elements from that array are, A[0], A[1], A[2], A[3],………………, A[n-1] 1. Declare and initialize one variable which contents the number to be search in an array A. ( variable key is declared) 2. Start Comparing each element from array A with the key LOOP: A[size] == key Repeat step no 2 while A[size] key 3. if key is found, display the location of element(index+1) or else display message KEY NOT FOUND 4. Terminate the program successfully
  • 10. Linear Search Algorithm printf(“accept number to search”); scanf( key ); for( i=0 ;i<n ;i++) { if( A [ i ] == key ) { printf( key is FOUND); break; } } if( i==n ) { printf(NOT FOUND); }
  • 11. Analysis of Linear Search Complexity of linear search : 1.Best Case = O(1) 2.Average Case = O(n) 3.Worst case = O(n)
  • 12. Binary Search “Binary search is an searching algorithm which is used to find element from the sorted list” Concepts : - Algorithm can be applied only on sorted data - Mid = lower/upper - formula used to find mid - Given element is compared with middle element of the list. - If key=mid then element is found - Otherwise list divide into two part.(key <mid) (>mid) - First to mid-1 or mid+1 to last.
  • 13. Binary Search Concepts : - If given element is less than middle element then continue searching in first part (first+mid-1) otherwise searching is second part(mid+1 to last). - Repeat above step still element is found.
  • 14. Binary Search Assume that two variables are declared, variable first and last, they denotes beginning and ending indices of the list under consideration respectively. Step 1. Algorithm compares key with middle element from list ( A[middle] == key ), if true go to step 4 or else go to next. Step 2. if key < A[ middle ], search in left half of the list or else go to step 3 Step 3. if key > A[ middle ], search in right half of the list or go to step 1 Step 4. display the position of key else display message “NOT FOUND”
  • 15. Binary Search algorithm int i, first=0, last=n-1, middle; while( last>=first ) { middle = (first + last)/2; if( key > A[middle] ) { first = middle + 1; } else if ( key < A[middle] ) { last= middle – 1; } else { printf( FOUND ) } } if( last < first ) { printf( NOT FOUND ); }
  • 16. Advatages Binary Search 1.Binary search is optimal searching algorithms 2.Excellent time efficiency 3.Suitable for large list. 4.Faster because no need to check all element. 5.Most suitable for sorted array 6.It can be search quickly 7.Time complexity O(log n)
  • 17. Disadvatages Binary Search 1.Element must be sorted 2.Need to find mid element 3.Bit more complicated to implement and test 4.It does not support random access. 5.Key element require to compare with middle.
  • 18. • Element is searched by scanning the entire list from first element to the last • Many times entire list is search • Simple to implementation • Time complexity is O(n) • Less efficient sort • First list is divided into two sub-lists. Then middle element is compared with key element and then accordingly left or right sub-list is searched • Only sub-list is search • Complex to implement, since it involves computation for finding the middle element • Time complexity is O(log2 n) • More efficient sort Linear Search Vs Binary Search
  • 20. © Reem Al- Attas Binary Search 2. Calculate middle = (low + high) / 2. = (0 + 8) / 2 = 4. If 37 == array[middle] return middle Else if 37 < array[middle] high = middle -1 Else if 37 > array[middle] low = middle +1
  • 21. Binary Search Repeat 2. Calculate middle = (low + high) / 2. = (0 + 3) / 2 = 1. If 37 == array[middle] return middle Else if 37 < array[middle] high = middle -1 Else if 37 > array[middle] low = middle +1 9/6/201 7 © Reem Al- Attas
  • 22. Binary Search Repeat 2. Calculate middle = (low + high) / 2. = (2 + 3) / 2 = 2. If 37 == array[middle] return middle Else if 37 < array[middle] high = middle -1 Else if 37 > array[middle] low = middle +1 9/6/201 7 © Reem Al- Attas
  • 30.  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 Sentinel search
  • 31.  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
  • 32.  Fibonacci search changes the binary search algorithm slightly  Instead of halving the index for a search, a Fibonacci number is subtracted from it  The Fibonacci number to be subtracted decreases as the  size of the listdecreases  Note that Fibonacci search sorts a list in a non decreasing order  Fibonacci search starts searching the target by comparing  it with the element at Fkth location 32 Fibonacci search
  • 34. 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 Fibonacci search
  • 35.  “Sorting is the process ordering a list of element in either ascending or descending order.”  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 35 SORTING
  • 36.  Any sort algorithm that uses main memory exclusively during the sorting is called as internal sort algorithm  Internal sorting is faster than externalsorting Internal Sorting techniques : 1. Bubble sort 2. Selection sort 3. Insertion sort 4. Quick sort 5. Shell sort 6. Heap sort 7. Radix sort 8. Bucket sort 36 INTERNAL SORTING(types)
  • 37.  Any sort algorithm that uses external memory, such as tape or disk, during the sorting is called as external sort algorithm  Merge sort is used in externalsorting 37 EXTERNAL SORTING
  • 38.  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 STABILITY OF SORTING
  • 39.  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 SORT EFFICIENCY
  • 40.  During the sorted process, the data is traversed many times  Each traversal of the data is referred to as a sort pass  In addition, the characteristic of a sort pass is the placement of one or more elements in a sorted list 40 PASSES IN SORTING
  • 41.  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 BUBBLE SORTING
  • 77. Quick Sort Algorithm  Quick sort is based on divide-and-conquer strategy  Quick sort is thus in-place, divide-and-conquer based massively recursive sort technique  This technique reduces unnecessary swaps and moves the element at great distance in one move
  • 78. Quick Sort Algorithm The recursive algorithm consists of four steps:  If there is one or less element in the array to be sorted, return immediately  Pick an element in the array to serve as a ‘pivot’ usually the left-most element in the list)  Partition the array into two parts—one with elements smaller than the pivot and the other with elements larger than the pivot by traversing from both the ends and performing swaps if needed  Recursively repeat the algorithm for bothpartitions
  • 88. Merge Sort Algorithm • Merge sort is a sorting technique based on divide and conquer technique. With Average case and worst-case time complexity being Ο(n log n), it is one of the most respected algorithms. • Merge sort first divides the array into equal halves and then combines them in a sorted manner.
  • 89.  The most common algorithm used in external sorting is the merge sort  Merging is the process of combiningtwo or more sorted files into the third sortedfile  We can use a techniqueof merging two sorted lists  Divide and conquer is a general algorithm design paradigm that is used for mergesort 89 Merge Sort
  • 90.  Time Complexity T(n) = O(n logn) 90 Merge Sort
  • 91. How merge sort works take a n • To understand merge sort, we unsorted array as depicted below − • We know that merge sort first divides the whole array iteratively into equal halves unless the atomic values are achieved. We see here that an array of 8 items is divided into two arrays of size 4.
  • 92. • This does not change the sequence of appearance of items in the original. Now we divide these two arrays into halves. • We further divide these arrays and we achieve atomic value which can no more be divided.
  • 93. • Now, we combine them in exactly same manner they were broken down. • We first compare the element for each list and then combine them into another list in sorted manner. We see that 14 and 33 are in sorted positions. We compare 27 and 10 and in the target list of 2 values we put 10 first, followed by 27. We change the order 19 and 35. 42 and 44 are placed sequentially.
  • 94. • In next iteration of combining phase, we compare lists of two data values, and merge them into a list of four data values placing all in sorted order. • After final merging, the list should look like this −
  • 95. Algorithm of merge sort • Merge sort keeps on dividing the list into equal halves until it can no more be divided. By definition, if it is only one element in the list, it is sorted. Then merge sort combines smaller sorted lists keeping the new list sorted too. – Step 1 − divide the list recursively into two halves until it can no more be divided. – Step 2 − if it is only one element in the list it is already sorted, return. – Step 3 − merge the smaller lists into new list in sorted order.
  • 101. Shell Sort • Shell sort is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm avoids large shifts as in case of insertion sort if smaller value is very far right and have to move to far left. • This algorithm uses insertion sort on widely spread elements first to sort them and then sorts the less widely spaced elements. This spacing is termed as interval. This interval is calculated based on Knuth's formula as −
  • 102. • h = h * 3 + 1 where − h is interval with initial value 1 This algorithm is quite efficient for medium sized data sets as its average and worst case complexity are of O(n^2) where n are no. of items.
  • 103. How shell sort works • We take the below example to have an idea, how shell sort works? • We take the same array we have used in our previous examples. {35,33,42,10,14,19,27,44} • For our example and ease of understanding we take the interval of 4. • And make a virtual sublist of all values located at the interval of 4 positions. Here these values are {35, 14}, {33, 19}, {42, 27} and {10, 14}
  • 104. We compare values in each sub-list and swap them (if necessary) in the original array. After this step, new array should look like this −
  • 105. Then we take interval of 2 and this gap generates two sublists - {14, 27, 35, 42}, {19, 10, 33, 44} 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 −
  • 107. 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
  • 109. 8, 3, 2, 11, 5 , 14, 00, 9, 4, 20 Perform shell sort
  • 110.  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  The ith counter/bucket keeps track of the number of occurrences of the ith element of the list 11 0 Bucket Sort
  • 111. Illustration of how this is done for m = 9 11 1 Bucket Sort
  • 112. Illustration of how this is done for m = 9 11 2 Bucket Sort
  • 113. Radix Sort • Radix Sort is generalization of Bucket Sort • To sort Decimal Numbers radix/base will be used as 10. so we need 10 buckets. • Buckets are numbered as 0,1,2,3,…,9 • Sorting is Done in the passes • Number of Passes required for sorting is number of digits in the largest number in the list.
  • 114.  Radix sort is a generalization of bucket sorting  Radix sort works in threesteps:  Distribute all elements into m buckets  Here m is a suitable integer, for example, to sort decimal numbers with radix 10  We take 10 buckets numbered as 0, 1, 2, …, 9  For sorting strings, we may need 26 buckets, and so on  Sort each bucket individually  Finally, combine all buckets 11 4 Radix Sort
  • 115. Ex. Range 0 to 99 0 to 999 0 to 9999 Passes 2 Passes 3 Passes 4 Passes • In First Pass number sorted based on Least Significant Digit and number will be kept in same bucket. • In 2nd Pass, Numbers are sorted on second least significant bit and process continues. • At the end of every pass, numbers in buckets are merged to produce common list.
  • 123. • Radix Sort is very simple, and a computer can do it fast. When it is programmed properly, Radix Sort is in fact one of the fastest sorting algorithms for numbers or strings of letters. • Average case and Worst case Complexity - O(n) 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.
  • 124.  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  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 amount of space apart from the list being sorted 12 4 HEAP SORT
  • 125.  The steps for building heap sort are as follows:  Build the heap tree Start delete heap operation storing each deleted element at the end of the heaparray 12 5 Heap Sort
  • 126.  ALGORITHM  1. Build a heap tree with a given set of data  (a) Delete root node fromheap  (b) Rebuild the heap afterdeletion  (c) Place the deleted node in the output 12 6 Continue with step (2) until the heap tree is empty Heap Sort
  • 127. Analysis of HeapSort 12 7  The time complexity is stated as follows:  Best case O(n logn)  Average case O(nlogn)  Worst case O(nlogn)
  • 141. COMPARISON OF ALL SORTING METHODS 14 1
  • 142. 14 2
  • 143. 14 3
  • 144. 14 4
  • 145. 14 5
  • 146. 14 6
  • 147. 14 7
  • 148. 14 8
  • 152. Thanks ……!!!! 15 2 Prof. ANAND GHARU ASSISTANT PROFESSOR Blog : anandgharu.wordpress.com
  翻译: