This document discusses search algorithms, including linear search and binary search. It covers:
1) Linear search sequentially scans an array to find a match, working for both sorted and unsorted arrays.
2) Binary search uses recursion to search a sorted array, eliminating half the search space each iteration, making it faster than linear search.
3) Implementation examples of linear and binary search in Java are provided, along with analysis of the time efficiency of each method.
Linear Search Swapping Bubble Sort Binary Search.pptxJeoJoyA
- Searching refers to finding the location of a given search item in a collection of items. Linear search is a straightforward method to find a particular element in an unordered list by comparing each element sequentially until the desired element is found.
- Bubble sort is a sorting algorithm that works by repeatedly stepping through the list, comparing adjacent item pairs and swapping them if in the wrong order. This causes larger items to "bubble" to the top of the list.
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
This document summarizes a lecture on searching techniques, including linear search and binary search. It describes how linear search sequentially checks each item in a list to find a match, having time complexity of O(n) in average and worst cases. Binary search uses a divide and conquer approach, comparing the middle element of a sorted list to determine if the target is in the upper or lower half, narrowing the search space and having time complexity of O(log n). The advantages and drawbacks of each method are also outlined.
The document discusses various searching algorithms like linear search and binary search. It provides details on how linear search works by sequentially checking each element of an unsorted array to find a target value. Binary search is described as more efficient for sorted arrays, as it repeatedly divides the search space in half by comparing the target to the middle element. Implementation of both iterative and recursive binary search algorithms is demonstrated through pseudocode.
Sequential and interval searching algorithms are used to search for elements in data structures. Sequential searches, like linear search, sequentially check each element until finding a match. Interval searches, like binary search, target the center of the search structure and divide the search space in half with each iteration. Other searching techniques include sentinel search, which adds a sentinel value to reduce comparisons, and Fibonacci search, which divides the search space into unequal parts based on Fibonacci numbers.
This document discusses sequential and interval searching techniques in data structures. It provides examples of linear search and binary search algorithms. Linear search sequentially checks each element to find a match, having a worst-case complexity of O(n). Binary search repeatedly targets the midpoint of a sorted array, dividing the search space in half, with a worst-case complexity of O(log n). Selection sort is also discussed as an example of an iterative sorting algorithm, which finds the minimum element and places it in the first position in each pass.
This document discusses search algorithms, including linear search and binary search. It covers:
1) Linear search sequentially scans an array to find a match, working for both sorted and unsorted arrays.
2) Binary search uses recursion to search a sorted array, eliminating half the search space each iteration, making it faster than linear search.
3) Implementation examples of linear and binary search in Java are provided, along with analysis of the time efficiency of each method.
Linear Search Swapping Bubble Sort Binary Search.pptxJeoJoyA
- Searching refers to finding the location of a given search item in a collection of items. Linear search is a straightforward method to find a particular element in an unordered list by comparing each element sequentially until the desired element is found.
- Bubble sort is a sorting algorithm that works by repeatedly stepping through the list, comparing adjacent item pairs and swapping them if in the wrong order. This causes larger items to "bubble" to the top of the list.
Searching in Data Structure(Linear search and Binary search)SURBHI SAROHA
This document summarizes a lecture on searching techniques, including linear search and binary search. It describes how linear search sequentially checks each item in a list to find a match, having time complexity of O(n) in average and worst cases. Binary search uses a divide and conquer approach, comparing the middle element of a sorted list to determine if the target is in the upper or lower half, narrowing the search space and having time complexity of O(log n). The advantages and drawbacks of each method are also outlined.
The document discusses various searching algorithms like linear search and binary search. It provides details on how linear search works by sequentially checking each element of an unsorted array to find a target value. Binary search is described as more efficient for sorted arrays, as it repeatedly divides the search space in half by comparing the target to the middle element. Implementation of both iterative and recursive binary search algorithms is demonstrated through pseudocode.
Sequential and interval searching algorithms are used to search for elements in data structures. Sequential searches, like linear search, sequentially check each element until finding a match. Interval searches, like binary search, target the center of the search structure and divide the search space in half with each iteration. Other searching techniques include sentinel search, which adds a sentinel value to reduce comparisons, and Fibonacci search, which divides the search space into unequal parts based on Fibonacci numbers.
This document discusses sequential and interval searching techniques in data structures. It provides examples of linear search and binary search algorithms. Linear search sequentially checks each element to find a match, having a worst-case complexity of O(n). Binary search repeatedly targets the midpoint of a sorted array, dividing the search space in half, with a worst-case complexity of O(log n). Selection sort is also discussed as an example of an iterative sorting algorithm, which finds the minimum element and places it in the first position in each pass.
IRJET- A Survey on Different Searching AlgorithmsIRJET Journal
The document summarizes and compares several common search algorithms:
- Binary search has the best average time complexity of O(log n) but only works on sorted data. Linear search has average time complexity of O(n) and works on any data but is less efficient.
- Hybrid search combines linear and binary search to search unsorted arrays more efficiently than linear search. Interpolation search is an improvement on binary search that may search in different locations based on the search key value.
- Jump search works on sorted data by jumping in blocks of size sqrt(n) and doing a linear search within blocks. It has better average performance than linear search but only works on sorted data.
The greedy choice at each step is to select the talk that ends earliest among the compatible options. This maximizes the chance of fitting additional talks into the schedule.
The document discusses various searching techniques used in computer science. It describes linear search, binary search, jump search, interpolation search, and Fibonacci search. For each search method, it provides details on the algorithm, time complexity, and examples. It also presents problems to solve using jump search and interpolation search and concludes with questions about the different search techniques.
linear search and binary search, Class lecture of Data Structure and Algorithms and Python.
Stack, Queue, Tree, Python, Python Code, Computer Science, Data, Data Analysis, Machine Learning, Artificial Intellegence, Deep Learning, Programming, Information Technology, Psuedocide, Tree, pseudocode, Binary Tree, Binary Search Tree, implementation, Binary search, linear search, Binary search operation, real-life example of binary search, linear search operation, real-life example of linear search, example bubble sort, sorting, insertion sort example, stack implementation, queue implementation, binary tree implementation, priority queue, binary heap, binary heap implementation, object-oriented programming, def, in BST, Binary search tree, Red-Black tree, Splay Tree, Problem-solving using Binary tree, problem-solving using BST, inorder, preorder, postorder
Linear search is a simple algorithm that sequentially checks each element of a data collection one by one until a match is found or the end is reached. It involves traversing the array using a for loop, comparing the target value to the current array element in each iteration, returning the index if a match occurs or -1 if no match, and explaining its implementation through pseudocode steps. The document provides an overview of linear search and how to implement it through an example and algorithm.
Linkedlist with each and every step Single. doublyakshmineshpatel
The document discusses sorting algorithms like bubble sort and insertion sort. It provides examples to explain how each algorithm works in detail with step-by-step examples. Bubble sort works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order, while insertion sort iterates through an array and inserts each element into the sorted position.
The document discusses various searching and sorting algorithms. It begins by defining searching as finding an item in a list. It describes sequential and binary search techniques. For sorting, it covers bubble, selection, insertion, merge, quick and heap sorts. It provides pseudocode examples and analyzes the time complexity of algorithms like selection sort and quicksort. Key aspects covered include the divide and conquer approach of quicksort and the efficiency of various sorting methods.
The document discusses various searching and sorting algorithms. It begins by defining searching as finding an item in a list. It describes sequential and binary search techniques. For sorting, it covers bubble, selection, insertion, merge, quick and heap sorts. It provides pseudocode examples and analyzes the time complexity of algorithms like selection sort and quicksort. Key aspects covered include the divide and conquer approach of quicksort and the efficiency of various sorting methods.
An Introduction to Reinforcement Learning - The Doors to AGIAnirban Santara
Reinforcement Learning (RL) is a genre of Machine Learning in which an agent learns to choose optimal actions in different states in order to reach its specified goal, solely by interacting with the environment through trial and error. Unlike supervised learning, the agent does not get examples of "correct" actions in given states as ground truth. Instead, it has to use feedback from the environment (which can be sparse and delayed) to improve its policy over time. The formulation of the RL problem closely resembles the way in which human beings learn to act in different situations. Hence it is often considered the gateway to achieving the goal of Artificial General Intelligence.
The motivation of this talk is to introduce the audience to key theoretical concepts like formulation of the RL problem using Markov Decision Process (MDP) and solution of MDP using dynamic programming and policy gradient based algorithms. State-of-the-art deep reinforcement learning algorithms will also be covered. A case study of the application of reinforcement learning in robotics will also be presented.
Searching algorithms are used to find elements within datasets. Sequential search linearly checks each element until a match is found, taking O(n) time on average. Interval search algorithms like binary search target the center of a sorted structure and divide the search space in half at each step, taking O(log n) time on average. Jump search checks fewer elements than linear search by jumping ahead by a fixed number of steps or block size, typically the square root of the list length. Interpolation search may check non-middle indexes based on the searched value, working best for uniformly distributed sorted data.
This document summarizes a massive open online course on Udemy about fundamental data structures and algorithms using the C language. The 15-hour course covers key topics like stacks, queues, linked lists, trees, recursion, and analyzing algorithm efficiency. It aims to help students strengthen programming skills and prepare for technical interviews at top companies. The course consists of 14 sections and includes weekly quizzes on the Udemy platform.
The document discusses quick sort, an algorithm for sorting arrays. Quick sort works by choosing a pivot element and partitioning the array around the pivot so that all elements less than the pivot come before it and all elements greater than the pivot come after. It then recursively applies the same approach to the sub-arrays on each side of the pivot. The document explains that the middle element is often used as the pivot and describes the partition algorithm in detail.
The document discusses quicksort, an algorithm for sorting arrays. It explains that quicksort works by choosing a pivot element and partitioning the array around that pivot so that all elements lower than the pivot come before it and all elements higher come after. It notes that choosing a good pivot is important for performance. The document then describes the partition algorithm in detail, explaining how it iterates through the array, swapping elements to get all elements less than the pivot before it and greater elements after it.
This document summarizes two common searching algorithms: linear search and binary search. Linear search sequentially checks each element of an array to find a target value, making it suitable for unsorted data. Binary search divides a sorted array in half at each step to quickly locate a value. Pseudocode is provided for implementations of both search types. Key differences are that linear search has O(n) time complexity while binary search has O(log n) time for sorted data. Visualizations are included to illustrate how each search approach works.
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
This document discusses various searching and sorting algorithms. It begins by defining searching as finding an element in a given list. Linear search and binary search are described as two common searching algorithms. Linear search has O(n) time complexity while binary search has O(log n) time complexity but requires a sorted list. The document also discusses different sorting algorithms like bubble sort, insertion sort, and merge sort, and defines key concepts related to sorting like stability, efficiency, and passes.
IRJET- A Survey on Different Searching AlgorithmsIRJET Journal
The document summarizes and compares several common search algorithms:
- Binary search has the best average time complexity of O(log n) but only works on sorted data. Linear search has average time complexity of O(n) and works on any data but is less efficient.
- Hybrid search combines linear and binary search to search unsorted arrays more efficiently than linear search. Interpolation search is an improvement on binary search that may search in different locations based on the search key value.
- Jump search works on sorted data by jumping in blocks of size sqrt(n) and doing a linear search within blocks. It has better average performance than linear search but only works on sorted data.
The greedy choice at each step is to select the talk that ends earliest among the compatible options. This maximizes the chance of fitting additional talks into the schedule.
The document discusses various searching techniques used in computer science. It describes linear search, binary search, jump search, interpolation search, and Fibonacci search. For each search method, it provides details on the algorithm, time complexity, and examples. It also presents problems to solve using jump search and interpolation search and concludes with questions about the different search techniques.
linear search and binary search, Class lecture of Data Structure and Algorithms and Python.
Stack, Queue, Tree, Python, Python Code, Computer Science, Data, Data Analysis, Machine Learning, Artificial Intellegence, Deep Learning, Programming, Information Technology, Psuedocide, Tree, pseudocode, Binary Tree, Binary Search Tree, implementation, Binary search, linear search, Binary search operation, real-life example of binary search, linear search operation, real-life example of linear search, example bubble sort, sorting, insertion sort example, stack implementation, queue implementation, binary tree implementation, priority queue, binary heap, binary heap implementation, object-oriented programming, def, in BST, Binary search tree, Red-Black tree, Splay Tree, Problem-solving using Binary tree, problem-solving using BST, inorder, preorder, postorder
Linear search is a simple algorithm that sequentially checks each element of a data collection one by one until a match is found or the end is reached. It involves traversing the array using a for loop, comparing the target value to the current array element in each iteration, returning the index if a match occurs or -1 if no match, and explaining its implementation through pseudocode steps. The document provides an overview of linear search and how to implement it through an example and algorithm.
Linkedlist with each and every step Single. doublyakshmineshpatel
The document discusses sorting algorithms like bubble sort and insertion sort. It provides examples to explain how each algorithm works in detail with step-by-step examples. Bubble sort works by repeatedly comparing adjacent elements and swapping them if they are in the wrong order, while insertion sort iterates through an array and inserts each element into the sorted position.
The document discusses various searching and sorting algorithms. It begins by defining searching as finding an item in a list. It describes sequential and binary search techniques. For sorting, it covers bubble, selection, insertion, merge, quick and heap sorts. It provides pseudocode examples and analyzes the time complexity of algorithms like selection sort and quicksort. Key aspects covered include the divide and conquer approach of quicksort and the efficiency of various sorting methods.
The document discusses various searching and sorting algorithms. It begins by defining searching as finding an item in a list. It describes sequential and binary search techniques. For sorting, it covers bubble, selection, insertion, merge, quick and heap sorts. It provides pseudocode examples and analyzes the time complexity of algorithms like selection sort and quicksort. Key aspects covered include the divide and conquer approach of quicksort and the efficiency of various sorting methods.
An Introduction to Reinforcement Learning - The Doors to AGIAnirban Santara
Reinforcement Learning (RL) is a genre of Machine Learning in which an agent learns to choose optimal actions in different states in order to reach its specified goal, solely by interacting with the environment through trial and error. Unlike supervised learning, the agent does not get examples of "correct" actions in given states as ground truth. Instead, it has to use feedback from the environment (which can be sparse and delayed) to improve its policy over time. The formulation of the RL problem closely resembles the way in which human beings learn to act in different situations. Hence it is often considered the gateway to achieving the goal of Artificial General Intelligence.
The motivation of this talk is to introduce the audience to key theoretical concepts like formulation of the RL problem using Markov Decision Process (MDP) and solution of MDP using dynamic programming and policy gradient based algorithms. State-of-the-art deep reinforcement learning algorithms will also be covered. A case study of the application of reinforcement learning in robotics will also be presented.
Searching algorithms are used to find elements within datasets. Sequential search linearly checks each element until a match is found, taking O(n) time on average. Interval search algorithms like binary search target the center of a sorted structure and divide the search space in half at each step, taking O(log n) time on average. Jump search checks fewer elements than linear search by jumping ahead by a fixed number of steps or block size, typically the square root of the list length. Interpolation search may check non-middle indexes based on the searched value, working best for uniformly distributed sorted data.
This document summarizes a massive open online course on Udemy about fundamental data structures and algorithms using the C language. The 15-hour course covers key topics like stacks, queues, linked lists, trees, recursion, and analyzing algorithm efficiency. It aims to help students strengthen programming skills and prepare for technical interviews at top companies. The course consists of 14 sections and includes weekly quizzes on the Udemy platform.
The document discusses quick sort, an algorithm for sorting arrays. Quick sort works by choosing a pivot element and partitioning the array around the pivot so that all elements less than the pivot come before it and all elements greater than the pivot come after. It then recursively applies the same approach to the sub-arrays on each side of the pivot. The document explains that the middle element is often used as the pivot and describes the partition algorithm in detail.
The document discusses quicksort, an algorithm for sorting arrays. It explains that quicksort works by choosing a pivot element and partitioning the array around that pivot so that all elements lower than the pivot come before it and all elements higher come after. It notes that choosing a good pivot is important for performance. The document then describes the partition algorithm in detail, explaining how it iterates through the array, swapping elements to get all elements less than the pivot before it and greater elements after it.
This document summarizes two common searching algorithms: linear search and binary search. Linear search sequentially checks each element of an array to find a target value, making it suitable for unsorted data. Binary search divides a sorted array in half at each step to quickly locate a value. Pseudocode is provided for implementations of both search types. Key differences are that linear search has O(n) time complexity while binary search has O(log n) time for sorted data. Visualizations are included to illustrate how each search approach works.
Binary Search is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(Log n).
This document discusses various searching and sorting algorithms. It begins by defining searching as finding an element in a given list. Linear search and binary search are described as two common searching algorithms. Linear search has O(n) time complexity while binary search has O(log n) time complexity but requires a sorted list. The document also discusses different sorting algorithms like bubble sort, insertion sort, and merge sort, and defines key concepts related to sorting like stability, efficiency, and passes.
Similar to Unit 2 Searching and Sorting Technique.pptx (20)
Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapte...University of Maribor
Slides from talk presenting:
Aleš Zamuda: Presentation of IEEE Slovenia CIS (Computational Intelligence Society) Chapter and Networking.
Presentation at IcETRAN 2024 session:
"Inter-Society Networking Panel GRSS/MTT-S/CIS
Panel Session: Promoting Connection and Cooperation"
IEEE Slovenia GRSS
IEEE Serbia and Montenegro MTT-S
IEEE Slovenia CIS
11TH INTERNATIONAL CONFERENCE ON ELECTRICAL, ELECTRONIC AND COMPUTING ENGINEERING
3-6 June 2024, Niš, Serbia
Embedded machine learning-based road conditions and driving behavior monitoringIJECEIAES
Car accident rates have increased in recent years, resulting in losses in human lives, properties, and other financial costs. An embedded machine learning-based system is developed to address this critical issue. The system can monitor road conditions, detect driving patterns, and identify aggressive driving behaviors. The system is based on neural networks trained on a comprehensive dataset of driving events, driving styles, and road conditions. The system effectively detects potential risks and helps mitigate the frequency and impact of accidents. The primary goal is to ensure the safety of drivers and vehicles. Collecting data involved gathering information on three key road events: normal street and normal drive, speed bumps, circular yellow speed bumps, and three aggressive driving actions: sudden start, sudden stop, and sudden entry. The gathered data is processed and analyzed using a machine learning system designed for limited power and memory devices. The developed system resulted in 91.9% accuracy, 93.6% precision, and 92% recall. The achieved inference time on an Arduino Nano 33 BLE Sense with a 32-bit CPU running at 64 MHz is 34 ms and requires 2.6 kB peak RAM and 139.9 kB program flash memory, making it suitable for resource-constrained embedded systems.
ACEP Magazine edition 4th launched on 05.06.2024Rahul
This document provides information about the third edition of the magazine "Sthapatya" published by the Association of Civil Engineers (Practicing) Aurangabad. It includes messages from current and past presidents of ACEP, memories and photos from past ACEP events, information on life time achievement awards given by ACEP, and a technical article on concrete maintenance, repairs and strengthening. The document highlights activities of ACEP and provides a technical educational article for members.
Advanced control scheme of doubly fed induction generator for wind turbine us...IJECEIAES
This paper describes a speed control device for generating electrical energy on an electricity network based on the doubly fed induction generator (DFIG) used for wind power conversion systems. At first, a double-fed induction generator model was constructed. A control law is formulated to govern the flow of energy between the stator of a DFIG and the energy network using three types of controllers: proportional integral (PI), sliding mode controller (SMC) and second order sliding mode controller (SOSMC). Their different results in terms of power reference tracking, reaction to unexpected speed fluctuations, sensitivity to perturbations, and resilience against machine parameter alterations are compared. MATLAB/Simulink was used to conduct the simulations for the preceding study. Multiple simulations have shown very satisfying results, and the investigations demonstrate the efficacy and power-enhancing capabilities of the suggested control system.
We have compiled the most important slides from each speaker's presentation. This year’s compilation, available for free, captures the key insights and contributions shared during the DfMAy 2024 conference.
6th International Conference on Machine Learning & Applications (CMLA 2024)ClaraZara1
6th International Conference on Machine Learning & Applications (CMLA 2024) will provide an excellent international forum for sharing knowledge and results in theory, methodology and applications of on Machine Learning & Applications.
1. SANJIVANI K. B. P. POLYTECHNIC, KOPARGAON
With NBA ACCREDIATED programs , Approved by AICTE, New Delhi,
Recognized by Govt. of Maharashtra, Affiliated to Maharashtra State Board of Technical Education, Mumbai,
ISO 9001:2015 Certified Institute
Department:- Computer Technology Class:- CM3I
Name of Subject:- Data Structures Using 'C‘ MSBTE Subject Code:- 22317
Name of Faculty: Prof. Vaibhav A. Parjane
2. Searching and Sorting
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
3. Unit Outcome
After going through this unit, the student will be able to:
2a. Explain working of the given search method with an example.
2b. Write an algorithm to search the given key using binary Search method
2c. Write an Algorithm to sort data using a specified sorting method.
2d. Explain the working of given sorting method step by-step with an
example and small data set.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
4. Introduction: (Searching)
• Searching: Searching is a process of finding the location of a particular
element in an array is called searching.
• There are two types of searching:
Linear Search:
Binary Search:
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
5. Linear Search
• Linear search or sequential search is a method for finding a particular
value in a list that consists of checking every one of its elements
• One element at a time and in sequence, until the desired one is
found.
• It is simplest search algorithm
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
6. Linear Search
• Consider an array of 20 elements.
• Suppose, element 8 is to be search in the array.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
7. Linear Search (Algorithm)
Let us start with an array or list, L which may have the item in question.
Step 1: If the list L is empty, then the list has nothing. The list does not
have the item in question. Stop here.
Step 2: Otherwise, look at all the elements in the list L.
Step 3: For each element: If the element equals the item in question,
the list contains the item in question. Stop here. Otherwise, go onto
next element.
Step 4: The list does not have the item in question.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
10. Binary Search
• Also known as half-interval search algorithm.
• Finds the position of a specified input value within an array sorted by
key value.
• For binary search, the array should be arranged in ascending or
descending order.
• The binary search is based on the divide-and-conquer approach.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
11. Binary Search
• In this technique, the element to be searched (say, item) is compared
with the middle element of the array.
• If item is equal to the middle element, then search is successful.
• If the item is smaller than the middle element, then item is searched
in the segment of the array before the middle element. If the item is
greater than the middle element, item is searched in the array
segment after the middle element.
• This process will be in iteration until the element is found or the array
segment is reduced to a single element that is not equal to item.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
12. Binary Search
• Consider an sorted array of 11 elements as shown in figure below.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
Suppose we want to search the element 55 from the array of elements.
13. Binary Search
• For this we will take 3 variables Start, End and Middle
• Which will keep track of the status of start, end and middle value of the portion
of the array, in which we will search the element.
• The value of the middle will be as:
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
𝑴𝒊𝒅𝒅𝒍𝒆 =
𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅
2
• Initially, 𝑺𝒕𝒂𝒓𝒕 = 𝟏 𝑬𝒏𝒅 = 𝟏𝟏 and 𝑴𝒊𝒅𝒅𝒍𝒆 = (𝟏 + 𝟏𝟏) / 𝟐 = 𝟔.
• The key element 55 is to be compared with the Middle value.
• The value at index 6 is 48 and it is smaller than the target value i.e. (55).
14. Binary Search
• If the key is less than the value at Middle then the key element is
present in the first half else in other half;
• In our case, the key is on the right half.
• Hence, now Start = Middle + 1 = 6 + 1 =7.
• Calculate the middle index of the second half
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 7 + 11 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 9
15. Binary Search
• Again, the Middle divides the second half into the two parts as shown
in fig below
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
• The key element 55 is lesser than the value at Middle which is 72
• Hence it is present in the left half i.e., towards the left of 72.
• Hence now, End = Middle – 1 = 8. Start will remain 7
16. Binary Search
• At last, the Middle is calculated as
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
• Now if key element 55 is compared with the value at middle, then it is found
that they are equal.
• The key element is searched successfully and it is in 7th location
𝑴𝒊𝒅𝒅𝒍𝒆 = 𝑺𝒕𝒂𝒓𝒕 + 𝑬𝒏𝒅 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 7 + 8 2
𝑴𝒊𝒅𝒅𝒍𝒆 = 7
17. Binary Search (Algorithm)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
1.Start with the middle element:
•If the target value is equal to the middle element of the array, then return the index of the
middle element.
•If not, then compare the middle element with the target value,
•If the target value is greater than the number in the middle index, then pick the elements to the
right of the middle index, and start with Step 1.
•If the target value is less than the number in the middle index, then pick the elements to the left of
the middle index, and start with Step 1.
2.When a match is found, return the index of the element matched.
3.If no match is found, then return -1
20. Introduction (Sorting)
• Sorting: Sorting refers to arranging data in a particular format.
• Sorting means arranging the data in ascending or descending order.
• There are the several sorting techniques:
Bubble Sort
Selection Sort
Insertion Sort
Quick Sort
Radix Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
21. Bubble Sort (Algorithm)
Following are the steps involved in bubble sort(for sorting a given array
in ascending order):
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 Step1.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
22. Bubble Sort
Let's consider an array with values {5, 1, 6, 2, 4, 3}
Next, we have a pictorial representation of how bubble sort will sort
the given array.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
32. Selection Sort
• Selection sort is conceptually the most simplest sorting algorithm.
• This algorithm will first find the smallest element in the array and
swap it with the element in the first position.
• Then it will find the second smallest element and swap it with the
element in the second position
• It will repeat this until the entire array is sorted.
• It is called selection sort because it repeatedly selects the next-
smallest element and swaps it into the right place.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
33. Selection Sort (Algorithm)
Following are the steps involved in selection sort(for sorting a given
array in ascending order):
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
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
34. Selection Sort (Algorithm)
Following are the steps involved in selection sort(for sorting a given array in ascending
order):
1. Set the first element as minimum
2. Compare minimum with the second element.
3. If the second element is smaller than minimum, assign the second element as
minimum.
4. Compare minimum with the third element.
5. Again, if the third element is smaller, then assign minimum to the third element
otherwise do nothing.
6. The process goes on until the last element.
7. After each iteration, minimum is placed in the front of the unsorted list.
8. For each iteration, indexing starts from the first unsorted element.
9. Step 1 to 3 are repeated until all the elements are placed at their correct positions.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
35. Step 1
21 11 8 13 1
21 11 8 13 1
21 11 8 13 1
21 11 8 13 1
1 11 8 13 21
Swapping
𝒊 = 𝟎
𝒊 = 𝟏
𝒊 = 𝟐
𝒊 = 𝟑
Min value at index 1
Min value at index 2
Min value at index 2
Min value at index 4
First Iteration
Selection Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
36. Step 1
𝒊 = 𝟎
𝒊 = 𝟏
𝒊 = 𝟐
Min value at index 2
Min value at index 2
Min value at index 2
Second Iteration
1 11 8 13 21
1 11 8 13 21
1 11 8 13 21
1 8 11 13 21
Swapping
Selection Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
37. Step 1
𝒊 = 𝟎
𝒊 = 𝟏
Min value at index 2
Min value at index 2
Already in Place
Third Iteration
1 8 11 13 21
1 8 11 13 21
1 8 11 13 21
Selection Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
38. Step 1
𝒊 = 𝟎
𝒊 = 𝟏
Min value at index 3
Fourth Iteration
1 8 11 13 21
1 8 11 13 21
Selection Sort
Already in Place
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
42. Insertion Sort
• This is an in-place comparison-based sorting algorithm.
• In this, 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 'inserted’ in this sorted sub-list, has to find
its appropriate place and then it has to be inserted there.
• Hence the name, insertion sort.
• Insertion sort is a simple sorting algorithm that works the way we sort
playing cards in our hands.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
43. Insertion Sort (Characteristics)
1. It is efficient for smaller data sets, but very inefficient for larger lists.
2. Insertion Sort is adaptive, that means it reduces its total number of
steps if a partially sorted array is provided as input, making it
efficient.
3. It is better than Selection Sort and Bubble Sort algorithms.
4. Its space complexity is less. Like bubble Sort, insertion sort also
requires a single additional memory space.
5. It is a stable sorting technique, as it does not change the relative
order of elements which are equal.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
44. Insertion Sort (Algorithm)
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index
1, the key. The key element here is the new card that we need to add to our
existing sorted set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case,
element at index 0:
o If the key element is less than the first element, we insert the key element before the first
element.
o If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with
elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
45. Insertion Sort (Algorithm)
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index
1, the key. The key element here is the new card that we need to add to our
existing sorted set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case,
element at index 0:
o If the key element is less than the first element, we insert the key element before the first
element.
o If the key element is greater than the first element, then we insert it after the first element.
3. Then, we make the third element of the array as key and will compare it with
elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
46. 5 1 6 2 4
Start with second
element as key
3
5 1 6 2 4 3
Reach the front
enter 1 here
1 < 5
1 5 6 2 4 3
6 > 1 6 > 5
No Change in order
1 5 6 2 4 3
2 > 1 2 < 5
Insert 2 before 5 and
after 1
2 < 6
Insertion Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
47. 1 2 5 6 4 3
4 > 1 4 < 5
Insert 4 before 5 and
after 2
4 < 6
4 > 2
1 2 4 5 6 3
3 > 1 3 < 5
Insert 3 before 4 and
after 2
3 < 6
3 > 2 3 < 4
1 2 3 4 5 6 Sorted Array
Insertion Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
50. Quick Sort
• Quick Sort is a Divide and Conquer algorithm.
• It picks an element as pivot and partitions the given array around the
picked pivot.
• There are many different versions of Quick Sort that pick pivot in different
ways.
o Always pick first element as pivot.
o Always pick last element as pivot
o Pick a random element as pivot.
o Pick median as pivot.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
51. Quick Sort (Algorithm)
Following are the steps involved in quick sort algorithm:
• After selecting an element as pivot, which is the last index of the array in
our case, we divide the array for the first time.
• In quick sort, the array elements are so positioned that all the elements
smaller than the pivot will be on the left side of the pivot and all the
elements greater than the pivot will be on the right side of it.
• And the pivot element will be at its final sorted position.
• The elements to the left and right, may not be sorted.
• Then we pick subarrays, elements on the left of pivot and elements on the
right of pivot, and we perform partitioning on them by choosing a pivot in
the subarrays.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
52. Quick Sort (Algorithm)
• Partition considers the array elements a[first] through a[last].
• All items with keys less than the pivot are moved to lower positions in
the array
• All items with keys greater than the pivot are moved to higher
positions.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
53. Quick Sort (Algorithm)
• Partition considers the array elements a[first] through a[last].
• All items with keys less than the pivot are moved to lower positions in
the array
• All items with keys greater than the pivot are moved to higher
positions.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
54. 90 100 20 60 80 110 120 40 10 30 50 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
All are ≤ 70 All are > 70
First Last
The Partition algorithm yields
Quick Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
55. 90 100 20 60 80 110 120 40 10 30 50 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
The algorithm increments Leftpos until the item at that position is greater than or equal to the
pivot value
It decrements Rightpos until the item at that position is less than or equal to the pivot value
Then it swaps the values at those two positions
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
56. 90 100 20 60 80 110 120 40 10 30 50 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
90 > 70 and 50 < 70 (swap)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
57. 50 100 20 60 80 110 120 40 10 30 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
100 > 70 and 30 < 70 (swap)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
58. 50 30 20 60 80 110 120 40 10 100 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
80 > 70 and 10 < 70 (swap)
increment Leftpos until a[Leftpos] >= pivot
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
59. 50 30 20 60 10 110 120 40 80 100 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos Rightpos
110 > 70 and 40 < 70 (swap)
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
60. Rightpos
50 30 20 60 10 40 120 110 80 100 90 70
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos
Eventually this stops when the two variables cross paths
The Partition algorithm finishes by swapping the pivot at position last with the item at
Leftpos
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
61. Rightpos
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
70
Pivot
First Last
Quick Sort
Leftpos
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
62. Quicksort starts by partitioning the array. For the example above, the result of the first
partition is
50 30 20 60 10 40 70 110 80 100 90 120
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
All are ≤ 70 All are > 70
The pivot value, 70, holds the same position it would if the array were sorted.
All items to the left of it are smaller and all items to the right of it are larger.
Quick Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
63. • The pivot value, 70, holds the same position it would if the array were
sorted.
• All items to the left of it are smaller and all items to the right of it are
larger.
• Thus, the 70 is in the right spot and need not be considered further.
• Quicksort can continue by simply applying itself recursively to the two
segments of the array above and below the pivot.
Quick Sort
Sanjivani K.B.P. Polytechnic, Kopargaon Department of
Computer Technology Subject Faculty: V. A. Parjane
71. Radix Sort
• Radix sort is one of the sorting algorithms used to sort a list of integer
numbers in order.
• In radix sort algorithm, a list of integer numbers will be sorted based on
the digits of individual numbers.
• Sorting is performed from least significant digit to the most significant
digit.
• Radix sort algorithm requires the number of passes which are equal to
the number of digits present in the largest number among the list of
numbers.
• For example, if the largest number is a 3 digit number then that list is
sorted with 3 passes.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
72. Radix Sort (Algorithm)
The Radix sort algorithm is performed using the following steps...
• Step 1 - Define 10 queues each representing a bucket for each digit from 0 to 9.
• Step 2 - Consider the least significant digit of each number in the list which is to
be sorted.
• Step 3 - Insert each number into their respective queue based on the least
significant digit.
• Step 4 - Group all the numbers from queue 0 to queue 9 in the order they have
inserted into their respective queues.
• Step 5 - Repeat from step 3 based on the next least significant digit.
• Step 6 - Repeat from step 2 until all the numbers are grouped based on the most
significant digit.
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
73. Radix Sort (Algorithm)
Example
Consider the following list of unsorted integer numbers
82, 901, 100, 12, 150, 77, 55, 23
Step 1: Define 10 queues each represents a bucket for digit from 0 to 9
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
queue 0 1 2 3 4 5 6 7 8 9
74. Radix Sort (Algorithm)
Step 2: Insert all the numbers of the list into respective queue based on the Least
Significant Digit (once placed digit) of every number
82, 901, 100, 12, 150, 77, 55, 23
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
150
100 901
12
82 23 55 77
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
100, 150, 901, 82, 12, 23, 55, 77
75. Radix Sort (Algorithm)
Step 2: Insert all the numbers of the list into respective queue based on the next
Least Significant Digit (tens placed digit) of every number
100, 150, 901, 82, 12, 23, 55, 77
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
901
100 12 23
55
150 77 82
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
100, 901, 12, 23, 150, 55, 77, 82
76. Radix Sort (Algorithm)
Step 2: Insert all the numbers of the list into respective queue based on the next
Least Significant Digit (hundreds placed digit) of every number
100, 901, 12, 23, 150, 55, 77, 82
Sanjivani K.B.P. Polytechnic, Kopargaon Department of Computer Technology Subject Faculty: V. A. Parjane
82
77
55
23
12
150
100 901
queue 0 1 2 3 4 5 6 7 8 9
Group all the numbers from queue 0 to queue 9 in the order they have inserted & consider
the list for next step as input list
12, 23, 55, 77, 82, 100, 150, 901
List got sorted in the increasing order