SlideShare a Scribd company logo
Data Structure And
Algorithms
UNIT-5
Searching, hashing and sorting
25/4/2020
1
Submitted by
Bhanupratap Singh Hirwani
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
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
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
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
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
Example
• Assume the following array
• Search for 40
• Compare X=40
25/4/2020
7
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
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
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
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
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
Examples:
25/4/2020
13
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
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
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
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
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
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
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
Ad

More Related Content

What's hot (20)

Hashing
HashingHashing
Hashing
invertis university
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
sajinis3
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
Data Structures 7
Data Structures 7Data Structures 7
Data Structures 7
Dr.Umadevi V
 
Binary search python
Binary search pythonBinary search python
Binary search python
MaryamAnwar10
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
NagendraK18
 
Data Structures 8
Data Structures 8Data Structures 8
Data Structures 8
Dr.Umadevi V
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Data Structures 6
Data Structures 6Data Structures 6
Data Structures 6
Dr.Umadevi V
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
Haitham El-Ghareeb
 
Week 2 - Data Structures and Algorithms
Week 2 - Data Structures and AlgorithmsWeek 2 - Data Structures and Algorithms
Week 2 - Data Structures and Algorithms
Ferdin Joe John Joseph PhD
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
Senthil Murugan
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
Non-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library SortNon-Uniform Gap Distribution Library Sort
Non-Uniform Gap Distribution Library Sort
IJCSIS Research Publications
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Searching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And AlgorithmSearching techniques in Data Structure And Algorithm
Searching techniques in Data Structure And Algorithm
03446940736
 
Dsa – data structure and algorithms sorting
Dsa – data structure and algorithms  sortingDsa – data structure and algorithms  sorting
Dsa – data structure and algorithms sorting
sajinis3
 
linear search and binary search
linear search and binary searchlinear search and binary search
linear search and binary search
Zia Ush Shamszaman
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
Sowmya Jyothi
 
Binary search python
Binary search pythonBinary search python
Binary search python
MaryamAnwar10
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
ADS Introduction
ADS IntroductionADS Introduction
ADS Introduction
NagendraK18
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Lecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic SortingLecture 07 Data Structures - Basic Sorting
Lecture 07 Data Structures - Basic Sorting
Haitham El-Ghareeb
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
Senthil Murugan
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
IRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching AlgorithmsIRJET- A Survey on Different Searching Algorithms
IRJET- A Survey on Different Searching Algorithms
IRJET Journal
 
Sequential & binary, linear search
Sequential & binary, linear searchSequential & binary, linear search
Sequential & binary, linear search
montazur420
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 

Similar to Data structure and algorithms (20)

Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxxDFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
Working with python Nice PPT must try very good
Working with python Nice PPT must try very goodWorking with python Nice PPT must try very good
Working with python Nice PPT must try very good
MuhammadChala
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
LavanyaJ28
 
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSALGRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
mohanrajm63
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
sunilchute1
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
sunilchute1
 
Searching_Sorting.pptx
Searching_Sorting.pptxSearching_Sorting.pptx
Searching_Sorting.pptx
21BD1A058RSahithi
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
Data Structures Unit 2 FINAL presentation.pptx
Data Structures   Unit 2 FINAL presentation.pptxData Structures   Unit 2 FINAL presentation.pptx
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
Chapter 2. data structure and algorithm
Chapter  2. data structure and algorithmChapter  2. data structure and algorithm
Chapter 2. data structure and algorithm
SolomonEndalu
 
3-Searching and Sortingsdsdsdssssssssss.pdf
3-Searching and Sortingsdsdsdssssssssss.pdf3-Searching and Sortingsdsdsdssssssssss.pdf
3-Searching and Sortingsdsdsdssssssssss.pdf
NGUYNTHNHQUC2
 
Data Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdfData Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Sorting and hashing concepts
Sorting and hashing conceptsSorting and hashing concepts
Sorting and hashing concepts
LJ Projects
 
Searching, Sorting and Hashing Techniques
Searching, Sorting and Hashing TechniquesSearching, Sorting and Hashing Techniques
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxxDFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
Working with python Nice PPT must try very good
Working with python Nice PPT must try very goodWorking with python Nice PPT must try very good
Working with python Nice PPT must try very good
MuhammadChala
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
LavanyaJ28
 
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSALGRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
GRAPHS, BREADTH FIRST TRAVERSAL AND DEPTH FIRST TRAVERSAL
mohanrajm63
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
sunilchute1
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
sunilchute1
 
searching techniques.pptx
searching techniques.pptxsearching techniques.pptx
searching techniques.pptx
Dr.Shweta
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
ThenmozhiK5
 
Data Structures Unit 2 FINAL presentation.pptx
Data Structures   Unit 2 FINAL presentation.pptxData Structures   Unit 2 FINAL presentation.pptx
Data Structures Unit 2 FINAL presentation.pptx
dilipd20
 
Chapter 2. data structure and algorithm
Chapter  2. data structure and algorithmChapter  2. data structure and algorithm
Chapter 2. data structure and algorithm
SolomonEndalu
 
3-Searching and Sortingsdsdsdssssssssss.pdf
3-Searching and Sortingsdsdsdssssssssss.pdf3-Searching and Sortingsdsdsdssssssssss.pdf
3-Searching and Sortingsdsdsdssssssssss.pdf
NGUYNTHNHQUC2
 
Data Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdfData Structures & Algorithms - Spring 2025.pdf
Data Structures & Algorithms - Spring 2025.pdf
Syed Zaid Irshad
 
SEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMSSEARCHING AND SORTING ALGORITHMS
SEARCHING AND SORTING ALGORITHMS
Gokul Hari
 
2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx2.Problem Solving Techniques and Data Structures.pptx
2.Problem Solving Techniques and Data Structures.pptx
Ganesh Bhosale
 
PPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting TechniquesPPT.pptx Searching and Sorting Techniques
PPT.pptx Searching and Sorting Techniques
Vaibhav Parjane
 
Ad

Recently uploaded (20)

Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...
Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...
Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...
Helena Celeste Mata Rico
 
Layered Architecture for IOT Health System
Layered Architecture for IOT Health SystemLayered Architecture for IOT Health System
Layered Architecture for IOT Health System
MHTadayon
 
Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...
Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...
Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...
AnahNajam
 
Lec 2 organization of data.ppt biostatistics
Lec 2 organization of data.ppt biostatisticsLec 2 organization of data.ppt biostatistics
Lec 2 organization of data.ppt biostatistics
MuhammadGul20
 
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdfForestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
ChalaKelbessa
 
Final chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVA
Final chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVAFinal chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVA
Final chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVA
japanbabu2
 
antifungal plantss .HAJIRA CROP PPT.pptx
antifungal plantss .HAJIRA CROP PPT.pptxantifungal plantss .HAJIRA CROP PPT.pptx
antifungal plantss .HAJIRA CROP PPT.pptx
hajirashami
 
Confronting the food and climate crisis - Almost two decades of research in s...
Confronting the food and climate crisis - Almost two decades of research in s...Confronting the food and climate crisis - Almost two decades of research in s...
Confronting the food and climate crisis - Almost two decades of research in s...
Gatien Falconnier
 
cdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptxcdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptx
jatinjadon777
 
Beryllium _20250506_210712 foxxx_0000.pptx
Beryllium _20250506_210712 foxxx_0000.pptxBeryllium _20250506_210712 foxxx_0000.pptx
Beryllium _20250506_210712 foxxx_0000.pptx
JordanRifandani
 
2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx
fafyfskhan251kmf
 
Chapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.pptChapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.ppt
uniyaladiti914
 
Not Here, Go There: Analyzing Redirection Patterns on the Web
Not Here, Go There: Analyzing Redirection Patterns on the WebNot Here, Go There: Analyzing Redirection Patterns on the Web
Not Here, Go There: Analyzing Redirection Patterns on the Web
Kritika Garg
 
Rahul Preparation of permanent slide.pptx
Rahul Preparation of permanent slide.pptxRahul Preparation of permanent slide.pptx
Rahul Preparation of permanent slide.pptx
RahulRajai
 
Current Environmental issues- greenhouse gases, global warming, ocean acidifi...
Current Environmental issues- greenhouse gases, global warming, ocean acidifi...Current Environmental issues- greenhouse gases, global warming, ocean acidifi...
Current Environmental issues- greenhouse gases, global warming, ocean acidifi...
Hemashree519671
 
Motion and time - 7th grade (1) (1).pptx
Motion and time - 7th grade (1) (1).pptxMotion and time - 7th grade (1) (1).pptx
Motion and time - 7th grade (1) (1).pptx
anaghathaliakkattu
 
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
Sérgio Sacani
 
Title Management of Food Hazards for Ensuring Food Safety.pdf
Title Management of Food Hazards for Ensuring Food Safety.pdfTitle Management of Food Hazards for Ensuring Food Safety.pdf
Title Management of Food Hazards for Ensuring Food Safety.pdf
SUTITHI HAZRA
 
Application of Ultrasonography in pet animal .pptx
Application of Ultrasonography in pet animal .pptxApplication of Ultrasonography in pet animal .pptx
Application of Ultrasonography in pet animal .pptx
ANJANSAHOO9
 
Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...
Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...
Chaos and Psychology: Modeling the Human Mind through Nonlinear Dynamical Sys...
Helena Celeste Mata Rico
 
Layered Architecture for IOT Health System
Layered Architecture for IOT Health SystemLayered Architecture for IOT Health System
Layered Architecture for IOT Health System
MHTadayon
 
Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...
Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...
Drought Resistant Plants are a crucial focus in modern Agriculture and Enviro...
AnahNajam
 
Lec 2 organization of data.ppt biostatistics
Lec 2 organization of data.ppt biostatisticsLec 2 organization of data.ppt biostatistics
Lec 2 organization of data.ppt biostatistics
MuhammadGul20
 
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdfForestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
Forestry_Exit_Exam_Wollega University_Gimbi Campus.pdf
ChalaKelbessa
 
Final chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVA
Final chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVAFinal chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVA
Final chapters Vikalp sir.pptx Dr VIKALP SHRIVASTAVA
japanbabu2
 
antifungal plantss .HAJIRA CROP PPT.pptx
antifungal plantss .HAJIRA CROP PPT.pptxantifungal plantss .HAJIRA CROP PPT.pptx
antifungal plantss .HAJIRA CROP PPT.pptx
hajirashami
 
Confronting the food and climate crisis - Almost two decades of research in s...
Confronting the food and climate crisis - Almost two decades of research in s...Confronting the food and climate crisis - Almost two decades of research in s...
Confronting the food and climate crisis - Almost two decades of research in s...
Gatien Falconnier
 
cdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptxcdna synthesis and construction of gene libraries.pptx
cdna synthesis and construction of gene libraries.pptx
jatinjadon777
 
Beryllium _20250506_210712 foxxx_0000.pptx
Beryllium _20250506_210712 foxxx_0000.pptxBeryllium _20250506_210712 foxxx_0000.pptx
Beryllium _20250506_210712 foxxx_0000.pptx
JordanRifandani
 
2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx2. peptic ulcer (1) (1) for Pharm D .pptx
2. peptic ulcer (1) (1) for Pharm D .pptx
fafyfskhan251kmf
 
Chapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.pptChapter-10-Light-reflection-and-refraction.ppt
Chapter-10-Light-reflection-and-refraction.ppt
uniyaladiti914
 
Not Here, Go There: Analyzing Redirection Patterns on the Web
Not Here, Go There: Analyzing Redirection Patterns on the WebNot Here, Go There: Analyzing Redirection Patterns on the Web
Not Here, Go There: Analyzing Redirection Patterns on the Web
Kritika Garg
 
Rahul Preparation of permanent slide.pptx
Rahul Preparation of permanent slide.pptxRahul Preparation of permanent slide.pptx
Rahul Preparation of permanent slide.pptx
RahulRajai
 
Current Environmental issues- greenhouse gases, global warming, ocean acidifi...
Current Environmental issues- greenhouse gases, global warming, ocean acidifi...Current Environmental issues- greenhouse gases, global warming, ocean acidifi...
Current Environmental issues- greenhouse gases, global warming, ocean acidifi...
Hemashree519671
 
Motion and time - 7th grade (1) (1).pptx
Motion and time - 7th grade (1) (1).pptxMotion and time - 7th grade (1) (1).pptx
Motion and time - 7th grade (1) (1).pptx
anaghathaliakkattu
 
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
The Link Between Subsurface Rheology and EjectaMobility: The Case of Small Ne...
Sérgio Sacani
 
Title Management of Food Hazards for Ensuring Food Safety.pdf
Title Management of Food Hazards for Ensuring Food Safety.pdfTitle Management of Food Hazards for Ensuring Food Safety.pdf
Title Management of Food Hazards for Ensuring Food Safety.pdf
SUTITHI HAZRA
 
Application of Ultrasonography in pet animal .pptx
Application of Ultrasonography in pet animal .pptxApplication of Ultrasonography in pet animal .pptx
Application of Ultrasonography in pet animal .pptx
ANJANSAHOO9
 
Ad

Data structure and algorithms

  • 1. Data Structure And Algorithms UNIT-5 Searching, hashing and sorting 25/4/2020 1 Submitted by Bhanupratap Singh Hirwani
  • 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
  • 7. Example • Assume the following array • Search for 40 • Compare X=40 25/4/2020 7
  • 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