SlideShare une entreprise Scribd logo
1  sur  27
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
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≥
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.
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!!!!!!

Contenu connexe

Tendances

Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
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 Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort AlgorithmLemia Algmri
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |MdSaiful14
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applicationssomendra kumar
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applicationsJsaddam Hussain
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data StructureJeanie Arnoco
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data StructuresSHAKOOR AB
 

Tendances (20)

Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Data Structure (Queue)
Data Structure (Queue)Data Structure (Queue)
Data Structure (Queue)
 
Linked list
Linked listLinked list
Linked list
 
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...
 
Selection sorting
Selection sortingSelection sorting
Selection sorting
 
Stack
StackStack
Stack
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
heap Sort Algorithm
heap  Sort Algorithmheap  Sort Algorithm
heap Sort Algorithm
 
Graph traversals in Data Structures
Graph traversals in Data StructuresGraph traversals in Data Structures
Graph traversals in Data Structures
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Bubble sort | Data structure |
Bubble sort | Data structure |Bubble sort | Data structure |
Bubble sort | Data structure |
 
B and B+ tree
B and B+ treeB and B+ tree
B and B+ tree
 
Binary Search
Binary SearchBinary Search
Binary Search
 
queue & its applications
queue & its applicationsqueue & its applications
queue & its applications
 
Deque and its applications
Deque and its applicationsDeque and its applications
Deque and its applications
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Hashing Technique In Data Structures
Hashing Technique In Data StructuresHashing Technique In Data Structures
Hashing Technique In Data Structures
 

Similaire à SEARCHING & SORTING ALGORITHMS

Similaire à SEARCHING & SORTING ALGORITHMS (20)

data structures and algorithms Unit 3
data structures and algorithms Unit 3data structures and algorithms Unit 3
data structures and algorithms Unit 3
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
DS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptxDS - Unit 2 FINAL (2).pptx
DS - Unit 2 FINAL (2).pptx
 
Data Structures_ Sorting & Searching
Data Structures_ Sorting & SearchingData Structures_ Sorting & Searching
Data Structures_ Sorting & Searching
 
Heap, quick and merge sort
Heap, quick and merge sortHeap, quick and merge sort
Heap, quick and merge sort
 
advanced searching and sorting.pdf
advanced searching and sorting.pdfadvanced searching and sorting.pdf
advanced searching and sorting.pdf
 
MODULE 5-Searching and-sorting
MODULE 5-Searching and-sortingMODULE 5-Searching and-sorting
MODULE 5-Searching and-sorting
 
Unit v data structure-converted
Unit  v data structure-convertedUnit  v data structure-converted
Unit v data structure-converted
 
Searching Sorting
Searching SortingSearching Sorting
Searching Sorting
 
Searching,sorting
Searching,sortingSearching,sorting
Searching,sorting
 
Data structures arrays
Data structures   arraysData structures   arrays
Data structures arrays
 
Unit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTINGUnit 6 dsa SEARCHING AND SORTING
Unit 6 dsa SEARCHING AND SORTING
 
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
 
Sorting
SortingSorting
Sorting
 
Sorting types and Algorithms
Sorting types and AlgorithmsSorting types and Algorithms
Sorting types and Algorithms
 
Data structure using c module 3
Data structure using c module 3Data structure using c module 3
Data structure using c module 3
 
Arrays
ArraysArrays
Arrays
 
Lecture_Oct26.pptx
Lecture_Oct26.pptxLecture_Oct26.pptx
Lecture_Oct26.pptx
 
Sorting
SortingSorting
Sorting
 
366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)366 it elective 4 (analysis of algoritm)
366 it elective 4 (analysis of algoritm)
 

Dernier

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Dernier (20)

Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

SEARCHING & 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
  • 6.
  • 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≥
  • 19.
  • 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.
  • 24.
  • 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.