SlideShare a Scribd company logo
SEARCHING & SORTINGSEARCHING & SORTING
ALGORITHMSALGORITHMS
GOKUL H
SEARCHING ALGORITHMS
BINARY INTERPOLATIONLINEAR
LINEAR SEARCH

Basic and simplest search algorithm

Searches an element or value from a list in a sequential order
till the desired element or value is found or the end of the list is
encountered

On successful search it returns the index of the value else it
returns -1

Search is applied on unsorted list when there are fewer
elements in the list

Time consuming and Inefficient for big lists
LINEAR SEARCH
Loop
start
Initialize array
and search KEY
If key==array[i]
stop
KEY found at i
yes
No
yes
No
8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9
8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9
8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9
8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9
8 7 15 12 4 9
0 1 2 3 4 5
Search KEY=12
KEY ‘12’ found @ 3
BINARY SEARCH

Improvement over the sequential search

Work only if the list is sorted

Approach employed is divide and conquer

Here, the approximate middle element of the array is located and its
value is examined

If its value is too high, then the value of the middle element of the first
half of the array is examined and the procedure is repeated until the
required item is found

If the value is too low, then the value of the middle element of the
second half of the array is checked and the procedure is repeated
SEARCHING AND SORTING ALGORITHMS
INTERPOLATION SEARCH
 In Binary search, mid = (high + low)/2
Why the middle element?
What if we had a better estimate of the location of the key?
Binary Search always checks the value at middle index. But Interpolation
search may check at different locations based on the value of element
being searched
Interpolation search uses the values of arr[low] and arr[high] to estimate
the position of the key, also known as probing
INTERPOLATION SEARCH
1 2 3 4 5 6 7 8 9 10
Assuming a linear distribution of keys in the array
mid = low + ((key - arr[low]) * (high - low)) / (arr[high] -
arr[low])
key
mid = 0+[(3-1)*(9-0)] / (10-1)=2
So arr[2]=3 which is the key value
Search is success:
1 2 3 4 5 6 7 8 9 10
SORTING
Insertion Quick Shell HeapBubble Selection Merge
BUBBLE SORT

Bubble sort works by comparing each item in the list with the
item next to it and swapping them if required

The algorithm repeats this process until it makes a pass all the
way through the list without swapping any items (in other words,
all items are in the correct order)

This causes larger values to ‘bubble’ to the end of the list while
smaller values ‘sink’ towards the beginning of the list

In brief, the bubble sort derives its name from the fact that the
smallest data item bubbles up to the top of the sorted array
BUBBLE SORT
First iteration
Second Iteration
Third Iteration
Sorted array
SELECTION SORT

Selection sort algorithm sorts an array by repeatedly finding the
minimum element (considering ascending order) from unsorted
part and putting it at the beginning

Algorithm maintains two subarrays in a given array
a.sorted array
b.unsorted array

In every iteration of selection sort, the minimum element
(considering ascending order)from the unsorted subarray is
picked and moved to the sorted subarray
SELECTION SORT
ALGORITHM
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element
in the list
Step 3 − Swap with value at location
MIN
Step 4 − Increment MIN to point to next
element
Step 5 − Repeat until list is sorted
SELECTION SORT
Index<length-1
Find smallest element
In array between Index
and length-1
Swap smallest element
found above with
element at Index
Index++
start
end
Index=0
TRUE
FALSE
INSERTION SORT

In-place comparison-based sorting algorithm

Here, a sub-list is maintained which is always sorted

For example, the lower part of an array is maintained to be
sorted

An element which is to be 'insert'ed in this sorted sub-list, has to
find its appropriate place and then it has to be inserted there.
Hence the name, insertion sort.

The array is searched sequentially and unsorted items are
moved and inserted into the sorted sub-list (in the same array)
ALGORITHM
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
This process goes on…….
QUICK SORT

Based on partitioning of array of data into smaller arrays

A large array is partitioned into two arrays

One of which holds values smaller than the specified value, say
pivot, based on which the partition is made

Another array holds values greater than the pivot value
ALGORITHM
Step 1 − Choose the highest index value has pivot
Step 2 − Take two variables to point left and right of the list excluding pivot
Step 3 − left points to the low index
Step 4 − right points to the high
Step 5 − while value at left is less than pivot move right
Step 6 − while value at right is greater than pivot move left
Step 7 − if both step 5 and step 6 does not match swap left and right
Step 8 − if left right, the point where they met is new pivot≥
SEARCHING AND SORTING ALGORITHMS
SHELL SORT

Highly efficient sorting algorithm and is based on insertion sort
algorithm

Avoids large shifts as in case of insertion sort, if the smaller
value is to the far right and has to be moved to the far left

Uses insertion sort on a widely spread elements, first to sort
them and then sorts the less widely spaced elements
ALGORITHM
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 3 − Repeat until complete list is sorted
HEAP SORT

Heap Sort is one of the best sorting methods being in-place and
with no quadratic worst-case scenarios. Heap sort algorithm is
divided into two basic parts

Creating a Heap of the unsorted list

Then a sorted array is created by repeatedly removing the
largest/smallest element from the heap, and inserting it into the
array

The heap is reconstructed after each removal
Max Heap Construction Algorithm
Step 1 Create a new node at the end of heap.
Step 2 Assign new value to the node.
Step 3 Compare the value of this child node with its
parent.
Step 4 If value of parent is less than child,then
swap them.
Step 5 Repeat step 3 & 4 until Heap property
holds.
Max Heap Deletion Algorithm
Step 1 − Remove root node.
Step 2 − Move the last element of last level to root.
Step 3 − Compare the value of this child node with
its parent.
Step 4 − If value of parent is less than child, then
swap them.
Step 5 − Re peat step 3 & 4 until Heap property
holds.
SEARCHING AND SORTING ALGORITHMS
MERGE SORT

Sorting technique based on divide and conquer technique.

Merge sort first divides the array into equal halves and then combines
them in a sorted manner.
ALGORITHM
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more
be divided.
Step 3 − merge the smaller lists into new list in sorted order.
ALGORITHM
Step 1 − if it is only one element in the list it is already sorted, return.
Step 2 − divide the list recursively into two halves until it can no more be divided.
Step 3 − merge the smaller lists into new list in sorted order.
THANKK YOUUU!!!!!!

More Related Content

PPTX
Sorting Algorithms
Pranay Neema
 
PDF
Sorting Algorithms
Mohammed Hussein
 
PDF
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
PPTX
Searching techniques in Data Structure And Algorithm
03446940736
 
PPTX
Searching and sorting
PoojithaBollikonda
 
PPTX
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
PPTX
Sorting
Ashim Lamichhane
 
PPT
Searching in c language
CHANDAN KUMAR
 
Sorting Algorithms
Pranay Neema
 
Sorting Algorithms
Mohammed Hussein
 
Searching and Sorting Techniques in Data Structure
Balwant Gorad
 
Searching techniques in Data Structure And Algorithm
03446940736
 
Searching and sorting
PoojithaBollikonda
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Searching in c language
CHANDAN KUMAR
 

What's hot (20)

PPTX
Linear search-and-binary-search
International Islamic University
 
PPT
Bubble sort
Manek Ar
 
PPTX
Bubble sort | Data structure |
MdSaiful14
 
PPTX
stack & queue
manju rani
 
PPTX
Linear Search Presentation
Markajul Hasnain Alif
 
PPTX
Data structure - Graph
Madhu Bala
 
DOCX
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
PDF
linear search and binary search
Zia Ush Shamszaman
 
PPTX
Insertion sort
almaqboli
 
PPT
1.1 binary tree
Krish_ver2
 
PPTX
single linked list
Sathasivam Rangasamy
 
PPTX
Binary search
AparnaKumari31
 
PPTX
Sparse matrix and its representation data structure
Vardhil Patel
 
PPTX
The Relational Database Model
Shishir Aryal
 
PPTX
Tree - Data Structure
Ashim Lamichhane
 
PPT
Data structures using c
Prof. Dr. K. Adisesha
 
PPTX
Data structures and algorithms
Julie Iskander
 
PPT
Binary Search
kunj desai
 
PDF
Linear search algorithm
NeoClassical
 
PPT
Linked List
CHANDAN KUMAR
 
Linear search-and-binary-search
International Islamic University
 
Bubble sort
Manek Ar
 
Bubble sort | Data structure |
MdSaiful14
 
stack & queue
manju rani
 
Linear Search Presentation
Markajul Hasnain Alif
 
Data structure - Graph
Madhu Bala
 
Data Structure Question Bank(2 marks)
pushpalathakrishnan
 
linear search and binary search
Zia Ush Shamszaman
 
Insertion sort
almaqboli
 
1.1 binary tree
Krish_ver2
 
single linked list
Sathasivam Rangasamy
 
Binary search
AparnaKumari31
 
Sparse matrix and its representation data structure
Vardhil Patel
 
The Relational Database Model
Shishir Aryal
 
Tree - Data Structure
Ashim Lamichhane
 
Data structures using c
Prof. Dr. K. Adisesha
 
Data structures and algorithms
Julie Iskander
 
Binary Search
kunj desai
 
Linear search algorithm
NeoClassical
 
Linked List
CHANDAN KUMAR
 
Ad

Similar to SEARCHING AND SORTING ALGORITHMS (20)

PPTX
Searching,sorting
LavanyaJ28
 
PPTX
Unit 5 dsuc
Sweta Singh
 
PPTX
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
PPTX
Searching and Sorting algorithms and working
RitikaLohiya2
 
PPTX
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
PPTX
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
PPTX
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
PPTX
sorting and searching.pptx
ParagAhir1
 
ODP
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
PPTX
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
PPTX
Chapter 2. data structure and algorithm
SolomonEndalu
 
PDF
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
PPTX
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
PPTX
data structures and algorithms Unit 3
infanciaj
 
PPTX
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
PPTX
Data Structures_ Sorting & Searching
ThenmozhiK5
 
PPTX
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Searching,sorting
LavanyaJ28
 
Unit 5 dsuc
Sweta Singh
 
All Searching and Sorting Techniques in Data Structures
sonalishinge2015
 
Searching and Sorting algorithms and working
RitikaLohiya2
 
Sorting Algorithms to arrange data in particular format
itsusamazahid
 
AJisthewewrtyuiojhghfdfsgvhjhklopi87ytrytfghjk
PradipTadme
 
Chapter 3 - Data Structure and Algorithms.pptx
tarrebulehora
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
sorting and searching.pptx
ParagAhir1
 
Data operatons & searching and sorting algorithms
Anushdika Jeganathan
 
Weak 11-12 Sorting update.pptxbhjiiuuuuu
baloch4551701
 
Searching, Sorting and Hashing Techniques
Selvaraj Seerangan
 
Chapter 2. data structure and algorithm
SolomonEndalu
 
Chapter 1 - Introduction to Searching and Sorting Algorithms - Student.pdf
mylinhbangus
 
DFC30233_CHAPTER 6 (1).pptxxxxxxxxxxxxxxxxxxxxxxxx
rajinevitable05
 
data structures and algorithms Unit 3
infanciaj
 
DS - Unit 2 FINAL (2).pptx
prakashvs7
 
Data Structures_ Sorting & Searching
ThenmozhiK5
 
Different Searching and Sorting Methods.pptx
Minakshee Patil
 
Ad

Recently uploaded (20)

PPTX
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
PDF
Doc9.....................................
SofiaCollazos
 
PDF
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
PPTX
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
PPTX
Coupa-Overview _Assumptions presentation
annapureddyn
 
PDF
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
PDF
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
PPTX
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
PPT
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
PDF
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
PDF
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Software Development Methodologies in 2025
KodekX
 
PDF
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 
Comunidade Salesforce São Paulo - Desmistificando o Omnistudio (Vlocity)
Francisco Vieira Júnior
 
Doc9.....................................
SofiaCollazos
 
Orbitly Pitch Deck|A Mission-Driven Platform for Side Project Collaboration (...
zz41354899
 
Stamford - Community User Group Leaders_ Agentblazer Status, AI Sustainabilit...
Amol Dixit
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Revolutionize Operations with Intelligent IoT Monitoring and Control
Rejig Digital
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Coupa-Overview _Assumptions presentation
annapureddyn
 
Presentation about Hardware and Software in Computer
snehamodhawadiya
 
Oracle AI Vector Search- Getting Started and what's new in 2025- AIOUG Yatra ...
Sandesh Rao
 
Smart Infrastructure and Automation through IoT Sensors
Rejig Digital
 
L2 Rules of Netiquette in Empowerment technology
Archibal2
 
SparkLabs Primer on Artificial Intelligence 2025
SparkLabs Group
 
Using Anchore and DefectDojo to Stand Up Your DevSecOps Function
Anchore
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Automating ArcGIS Content Discovery with FME: A Real World Use Case
Safe Software
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Software Development Methodologies in 2025
KodekX
 
BLW VOCATIONAL TRAINING SUMMER INTERNSHIP REPORT
codernjn73
 

SEARCHING AND SORTING ALGORITHMS

  • 1. SEARCHING & SORTINGSEARCHING & SORTING ALGORITHMSALGORITHMS GOKUL H
  • 3. LINEAR SEARCH  Basic and simplest search algorithm  Searches an element or value from a list in a sequential order till the desired element or value is found or the end of the list is encountered  On successful search it returns the index of the value else it returns -1  Search is applied on unsorted list when there are fewer elements in the list  Time consuming and Inefficient for big lists
  • 4. LINEAR SEARCH Loop start Initialize array and search KEY If key==array[i] stop KEY found at i yes No yes No 8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9 8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9 8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9 8 7 15 12 4 98 7 15 12 4 98 7 15 12 4 98 7 15 12 4 9 8 7 15 12 4 9 0 1 2 3 4 5 Search KEY=12 KEY ‘12’ found @ 3
  • 5. BINARY SEARCH  Improvement over the sequential search  Work only if the list is sorted  Approach employed is divide and conquer  Here, the approximate middle element of the array is located and its value is examined  If its value is too high, then the value of the middle element of the first half of the array is examined and the procedure is repeated until the required item is found  If the value is too low, then the value of the middle element of the second half of the array is checked and the procedure is repeated
  • 7. INTERPOLATION SEARCH  In Binary search, mid = (high + low)/2 Why the middle element? What if we had a better estimate of the location of the key? Binary Search always checks the value at middle index. But Interpolation search may check at different locations based on the value of element being searched Interpolation search uses the values of arr[low] and arr[high] to estimate the position of the key, also known as probing
  • 8. INTERPOLATION SEARCH 1 2 3 4 5 6 7 8 9 10 Assuming a linear distribution of keys in the array mid = low + ((key - arr[low]) * (high - low)) / (arr[high] - arr[low]) key mid = 0+[(3-1)*(9-0)] / (10-1)=2 So arr[2]=3 which is the key value Search is success: 1 2 3 4 5 6 7 8 9 10
  • 9. SORTING Insertion Quick Shell HeapBubble Selection Merge
  • 10. BUBBLE SORT  Bubble sort works by comparing each item in the list with the item next to it and swapping them if required  The algorithm repeats this process until it makes a pass all the way through the list without swapping any items (in other words, all items are in the correct order)  This causes larger values to ‘bubble’ to the end of the list while smaller values ‘sink’ towards the beginning of the list  In brief, the bubble sort derives its name from the fact that the smallest data item bubbles up to the top of the sorted array
  • 11. BUBBLE SORT First iteration Second Iteration Third Iteration Sorted array
  • 12. SELECTION SORT  Selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning  Algorithm maintains two subarrays in a given array a.sorted array b.unsorted array  In every iteration of selection sort, the minimum element (considering ascending order)from the unsorted subarray is picked and moved to the sorted subarray
  • 13. SELECTION SORT ALGORITHM Step 1 − Set MIN to location 0 Step 2 − Search the minimum element in the list Step 3 − Swap with value at location MIN Step 4 − Increment MIN to point to next element Step 5 − Repeat until list is sorted
  • 14. SELECTION SORT Index<length-1 Find smallest element In array between Index and length-1 Swap smallest element found above with element at Index Index++ start end Index=0 TRUE FALSE
  • 15. INSERTION SORT  In-place comparison-based sorting algorithm  Here, a sub-list is maintained which is always sorted  For example, the lower part of an array is maintained to be sorted  An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort.  The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array)
  • 16. ALGORITHM Step 1 − If it is the first element, it is already sorted. return 1; Step 2 − Pick next element Step 3 − Compare with all elements in the sorted sub-list Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted Step 5 − Insert the value Step 6 − Repeat until list is sorted This process goes on…….
  • 17. QUICK SORT  Based on partitioning of array of data into smaller arrays  A large array is partitioned into two arrays  One of which holds values smaller than the specified value, say pivot, based on which the partition is made  Another array holds values greater than the pivot value
  • 18. ALGORITHM Step 1 − Choose the highest index value has pivot Step 2 − Take two variables to point left and right of the list excluding pivot Step 3 − left points to the low index Step 4 − right points to the high Step 5 − while value at left is less than pivot move right Step 6 − while value at right is greater than pivot move left Step 7 − if both step 5 and step 6 does not match swap left and right Step 8 − if left right, the point where they met is new pivot≥
  • 20. SHELL SORT  Highly efficient sorting algorithm and is based on insertion sort algorithm  Avoids large shifts as in case of insertion sort, if the smaller value is to the far right and has to be moved to the far left  Uses insertion sort on a widely spread elements, first to sort them and then sorts the less widely spaced elements
  • 21. ALGORITHM 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 3 − Repeat until complete list is sorted
  • 22. HEAP SORT  Heap Sort is one of the best sorting methods being in-place and with no quadratic worst-case scenarios. Heap sort algorithm is divided into two basic parts  Creating a Heap of the unsorted list  Then a sorted array is created by repeatedly removing the largest/smallest element from the heap, and inserting it into the array  The heap is reconstructed after each removal
  • 23. Max Heap Construction Algorithm Step 1 Create a new node at the end of heap. Step 2 Assign new value to the node. Step 3 Compare the value of this child node with its parent. Step 4 If value of parent is less than child,then swap them. Step 5 Repeat step 3 & 4 until Heap property holds. Max Heap Deletion Algorithm Step 1 − Remove root node. Step 2 − Move the last element of last level to root. Step 3 − Compare the value of this child node with its parent. Step 4 − If value of parent is less than child, then swap them. Step 5 − Re peat step 3 & 4 until Heap property holds.
  • 25. MERGE SORT  Sorting technique based on divide and conquer technique.  Merge sort first divides the array into equal halves and then combines them in a sorted manner. ALGORITHM Step 1 − if it is only one element in the list it is already sorted, return. Step 2 − divide the list recursively into two halves until it can no more be divided. Step 3 − merge the smaller lists into new list in sorted order.
  • 26. ALGORITHM Step 1 − if it is only one element in the list it is already sorted, return. Step 2 − divide the list recursively into two halves until it can no more be divided. Step 3 − merge the smaller lists into new list in sorted order.