0% found this document useful (0 votes)
61 views

Binary Search

Binary search is a fast search algorithm that works by comparing the middle element of a sorted collection to the target value. If it matches, the index is returned. If the middle element is greater than the target, the target is searched in the left subarray. Otherwise, it is searched in the right subarray. This process continues recursively on smaller subarrays until the target is found or the subarray size reaches zero. Bubble sort is a simple sorting algorithm that works by comparing adjacent elements and swapping them if they are in the wrong order. It performs multiple passes over the array until it is fully sorted. Binary search has a runtime of O(log n) while bubble sort has a runtime of O(n^2).

Uploaded by

Naina Dhamecha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Binary Search

Binary search is a fast search algorithm that works by comparing the middle element of a sorted collection to the target value. If it matches, the index is returned. If the middle element is greater than the target, the target is searched in the left subarray. Otherwise, it is searched in the right subarray. This process continues recursively on smaller subarrays until the target is found or the subarray size reaches zero. Bubble sort is a simple sorting algorithm that works by comparing adjacent elements and swapping them if they are in the wrong order. It performs multiple passes over the array until it is fully sorted. Binary search has a runtime of O(log n) while bubble sort has a runtime of O(n^2).

Uploaded by

Naina Dhamecha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 44

Binary Search

• Binary search is a fast search algorithm with run-time complexity of


Ο(log n). This search algorithm works on the principle of divide and
conquer. For this algorithm to work properly, the data collection should
be in the sorted form.
• Binary search looks for a particular item by comparing the middle most
item of the collection. If a match occurs, then the index of item is
returned. If the middle item is greater than the item, then the item is
searched in the sub-array to the left of the middle item. Otherwise, the
item is searched for in the sub-array to the right of the middle item.
This process continues on the sub-array as well until the size of the
subarray reduces to zero.
Algorithm
LinearSearch(array, key) linearSearch(array, size, key)
Input − An array, size and the search key
for each item in the array Output − location of the key (if found),
if item == value otherwise wrong location.

return its index Begin


for i := 0 to size -1 do
if array[i] = key then
return i
done
return invalid location
End
Time Complexity
• In linear search, best-case complexity is O(1) where the element is
found at the first index. Worst-case complexity is O(n) where the
element is found at the last index or element is not present in the
array.
Linear Search
• In this search, a sequential search is made over all items one by one.
Every item is checked and if a match is found then that particular
item is returned, otherwise the search continues till the end of the
data collection.
• often called sequential search. In this type of searching, we simply
traverse the list completely and match each element of the list with
the item whose location is to be found. If the match found then
location of the item is returned otherwise the algorithm return NULL.
• Linear search is mostly used to search an unordered list in which the
items are not sorted.
Program
#include<stdio.h>
void main () else
{ flag = 0;
int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9}; }
int item, i, flag; if(flag != 0)
printf("\nEnter Item which is to be searched\n"); {
scanf("%d",&item); printf("\nItem found at location %d\n",flag);
for (i = 0; i< 10; i++) }
{ if(a[i] == item) else
{ {
flag = i+1; printf("\nItem not found\n");
break; }
} }
Program
#include<iostream> int main() {
using namespace std; int n, searchKey, loc;
cout << "Enter number of items: ";
int linSearch(int array[], int size, int key) { cin >> n;
for(int i = 0; i<size; i++) {
int arr[n]; //create an array of size n
if(array[i] == key) //search key
cout << "Enter items: " << endl;
return i; //location where key is found
for the first time
} for(int i = 0; i< n; i++) {
return -1; //when the key is not in list cin >> arr[i];
} }
cout << "Enter search key to search in the list: ";
cin >> searchKey;

if((loc = linSearch(arr, n, searchKey)) >= 0)


cout << "Item found at location: " << loc << endl;
else
cout << "Item is not found in the list." << endl;
}
Algorithm
• BINARY_SEARCH(A, lower_bound, • ELSE IF A[MID] > VAL
upper_bound, VAL) • SET END = MID - 1
• Step 1: [INITIALIZE] SET BEG = lower_bound • ELSE
• END = upper_bound, POS = - 1 • SET BEG = MID + 1
• Step 2: Repeat Steps 3 and 4 while BEG
• [END OF IF]
<=END
• [END OF LOOP]
• Step 3: SET MID = (BEG + END)/2
• Step 4: IF A[MID] = VAL • Step 5: IF POS = -1
• SET POS = MID • PRINT "VALUE IS NOT PRESENT IN THE
ARRAY"
• PRINT POS
• [END OF IF]
• Go to Step 6
• Step 6: EXIT
Procedure binary_search
A ← sorted array
n ← size of array if A[midPoint] < x
x ← value to be searched set lowerBound = midPoint + 1
Set lowerBound = 1
Set upperBound = n if A[midPoint] > x
set upperBound = midPoint - 1
while x not found
if upperBound < lowerBound if A[midPoint] = x
EXIT: x does not exists. EXIT: x found at location midPoint
end while
set midPoint = lowerBound + ( upperBound -
lowerBound ) / 2 end procedure
Binary Search Program using Recursion
#include<stdio.h> location = binarySearch(arr, 0, 9, item);
int binarySearch(int[], int, int, int);
if(location != -1)
void main () {
{ printf("Item found at location
%d",location);
int arr[10] = {16, 19, 20, 23, 45, 56,
78, 90, 96, 100}; }
else
int item, location=-1; {
printf("Enter the item which you printf("Item not found");
want to search "); }
scanf("%d",&item); }
int binarySearch(int a[], int beg, int end, int else if(a[mid] < item)
item) {
{ return binarySearch(a,mid+1,end,item);
int mid; }
if(end >= beg) else
{
{
return binarySearch(a,beg,mid-1,item);
mid = (beg + end)/2;
}
if(a[mid] == item)
{ }
return mid+1; return -1;
} }
Binary Search function using Iteration
int binarySearch(int a[], int beg, int end, int else if(a[mid] < item)
item) {
{ beg = mid + 1;
int mid; }
while(end >= beg) else
{ {
end = mid - 1;
mid = (beg + end)/2;
}
if(a[mid] == item)
{ }
return mid+1; return -1;
} }
Complexity
• The time complexity of the binary search algorithm is O(log n). The
best-case time complexity would be O(1) when the central index
would directly match the desired value. The worst-case scenario could
be the values at either extremity of the list or values not in the list.
Bubble Sort
• Bubble Sort is a simple algorithm which is used to sort a given set of n
elements provided in form of an array. Bubble Sort compares all the
element one by one and sort them based on their values.
• If the given array has to be sorted in ascending order, then bubble
sort will start by comparing the first element of the array with the
second element, if the first element is greater than the second
element, it will swap both the elements, and then move on to
compare the second and the third element, and so on.
• If we have total n elements, then we need to repeat this process for
n-1 times.
• In Bubble sort, Each element of the array is compared with its adjacent element.
The algorithm processes the list in passes. A list with n elements requires n-1
passes for sorting.
• In Pass 1, A[0] is compared with A[1], A[1] is compared with A[2], A[2] is
compared with A[3] and so on. At the end of pass 1, the largest element of the list
is placed at the highest index of the list.
• In Pass 2, A[0] is compared with A[1], A[1] is compared with A[2] and so on. At the
end of Pass 2 the second largest element of the list is placed at the second highest
index of the list.
• In pass n-1, A[0] is compared with A[1], A[1] is compared with A[2] and so on. At
the end of this pass. The smallest element of the list is placed at the first index of
the list.
Algorithm :
• Step 1: Repeat Step 2 For i = 0 to N-1
• Step 2: Repeat For j = 0 to N-1-i
• Step 3: IF A[j] > A[j+1]
• SWAP A[j] and A[j+1]
• [END OF INNER LOOP]
• [END OF OUTER LOOP]
• Step 4: EXIT
#include <stdio.h> if( arr[j] > arr[j+1])
{
void bubbleSort(int arr[], int n) // swap the elements
{ temp = arr[j];
int i, j, temp; arr[j] = arr[j+1];
for(i = 0; i < n; i++) arr[j+1] = temp;
{ }
for(j = 0; j < n-i-1; j++) }
{ }
// print the sorted array scanf("%d", &n);
printf("Sorted Array: "); // input elements if the array
for(i = 0; i < n; i++) for(i = 0; i < n; i++)
{ {
printf("%d ", arr[i]); printf("Enter element no. %d: ", i+1);
} scanf("%d", &arr[i]);
} }
int main() // call the function bubbleSort
{
bubbleSort(arr, n);
int arr[100], i, n, step, temp;
// ask user for number of elements to be sorted
return 0;
printf("Enter the num of elements to be sorted: ");
}
Optimized Bubble Sort Algorithm
• To optimize our bubble sort algorithm, we can introduce a flag to
monitor whether elements are getting swapped inside the inner for
loop.

• Hence, in the inner for loop, we check whether swapping of elements


is taking place or not, everytime.

• If for a particular iteration, no swapping took place, it means the array


has been sorted and we can jump out of the for loop, instead of
executing all the iterations.
int i, j, temp, flag=0; // if swapping happens update flag to 1
for(i = 0; i < n; i++) flag = 1;
{ }
for(j = 0; j < n-i-1; j++) }
{ // if value of flag is zero after all the
// introducing a flag to monitor swapping iterations of inner loop
if( arr[j] > arr[j+1]) // then break out
{ if(flag==0)
// swap the elements {
temp = arr[j]; break;
arr[j] = arr[j+1]; }
arr[j+1] = temp; }
Complexity Analysis of Bubble Sort
• Input: Given n input elements.
• Output: Number of steps incurred to sort a list.

• Logic: If we are given n elements, then in the first pass, it will do n-1
comparisons; in the second pass, it will do n-2; in the third pass, it will
do n-3 and so on. Thus, the total number of comparisons can be
found by;
Time Complexities:
• Therefore, the bubble sort algorithm encompasses a time complexity of
O(n2) and a space complexity of O(1) because it necessitates some extra
memory space for temp variable for swapping.
• Best Case Complexity: The bubble sort algorithm has a best-case time
complexity of O(n) for the already sorted array.
• Average Case Complexity: The average-case time complexity for the bubble
sort algorithm is O(n2), which happens when 2 or more elements are in
jumbled, i.e., neither in the ascending order nor in the descending order.
• Worst Case Complexity: The worst-case time complexity is also O(n2), which
occurs when we sort the descending order of an array into the ascending
order.
• Following are the Time and Space complexity for the Bubble Sort
algorithm.

• Worst Case Time Complexity [ Big-O ]: O(n2)


• Best Case Time Complexity [Big-omega]: O(n)
• Average Time Complexity [Big-theta]: O(n2)
• Space Complexity: O(1)
Selection Sort
• 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, and it will keep on doing 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.
• The selection sort enhances the bubble sort by making only a single
swap for each pass through the rundown. In order to do this, a
selection sort searches for the smallest value as it makes a pass and,
after finishing the pass, places it in the best possible area. Similarly, as
with a bubble sort, after the first pass, the biggest item is in the right
place. After the second pass, the following biggest is set up. This
procedure proceeds and requires n-1 goes to sort n item since the last
item must be set up after the (n-1) th pass.
• In 1st pass, smallest element of the array is to be found along with
its index pos. then, swap A[0] and A[pos]. Thus A[0] is sorted, we now
have n -1 elements which are to be sorted.
• In 2nd pas, position pos of the smallest element present in the sub-
array A[n-1] is found. Then, swap, A[1] and A[pos]. Thus A[0] and A[1]
are sorted, we now left with n-2 unsorted elements.
• In n-1th pass, position pos of the smaller element between A[n-1] and
A[n-2] is to be found. Then, swap, A[pos] and A[n-1].
• Therefore, by following the above explained process, the elements
A[0], A[1], A[2],...., A[n-1] are sorted.
Algorithm
• SELECTION SORT(ARR, N) SMALLEST (ARR, K, N, POS)

Step 1: [INITIALIZE] SET SMALL = ARR[K]


• Step 1: Repeat Steps 2 and 3 for Step 2: [INITIALIZE] SET POS = K
K = 1 to N-1 Step 3: Repeat for J = K+1 to N -1
• Step 2: CALL SMALLEST(ARR, K, IF SMALL > ARR[J]
N, POS) SET SMALL = ARR[J]
• Step 3: SWAP A[K] with ARR[POS] SET POS = J
[END OF IF]
• [END OF LOOP]
[END OF LOOP]
• Step 4: EXIT Step 4: RETURN POS
#include<stdio.h> printf("\nprinting sorted elements...\n");
int smallest(int[],int,int); for(i=0;i<10;i++)
void main ()
{
{
int a[10] = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23}; printf("%d\n",a[i]);
int i,j,k,pos,temp; }
for(i=0;i<10;i++) }
{ int smallest(int a[], int n, int i)
pos = smallest(a,10,i);
{
temp = a[i];
a[i]=a[pos];
int small,pos,j;
a[pos] = temp; small = a[i];
} pos = i;
for(j=i+1;j<10;j++)
{
if(a[j]<small)
{
small = a[j];
pos=j;
}
}
return pos;
}
Complexity Analysis of Selection Sort
• Input: Given n input elements.
• Output: Number of steps incurred to sort a list.

• Logic: If we are given n elements, then in the first pass, it will do n-1
comparisons; in the second pass, it will do n-2; in the third pass, it will
do n-3 and so on. Thus, the total number of comparisons can be
found by;
• Therefore, the selection sort algorithm encompasses a time complexity of
O(n2) and a space complexity of O(1) because it necessitates some extra
memory space for temp variable for swapping.
• In selection sort, the smallest value among the unsorted elements of the array
is selected in every pass and inserted to its appropriate position into the array.
• First, find the smallest element of the array and place it on the first position.
Then, find the second smallest element of the array and place it on the second
position. The process continues until we get the sorted array.
• The array with n elements is sorted by using n-1 pass of selection sort
algorithm.
Time Complexities:
• Best Case Complexity: The selection sort algorithm has a best-case time
complexity of O(n2) for the already sorted array.
• Average Case Complexity: The average-case time complexity for the selection
sort algorithm is O(n2), in which the existing elements are in jumbled ordered,
i.e., neither in the ascending order nor in the descending order.
• Worst Case Complexity: The worst-case time complexity is also O(n2), which
occurs when we sort the descending order of an array into the ascending order.
• In the selection sort algorithm, the time complexity is O(n2) in all three cases.
This is because, in each step, we are required to find minimum elements so that
it can be placed in the correct position. Once we trace the complete array, we
will get our minimum element.
Insertion Sort
• It sorts a single element at a particular instance. It is an intuitive
sorting technique.
• In this algorithm, we insert each element onto its proper place in the
sorted array.
• Suppose we have a set of cards in our hand, such that we want to
arrange these cards in ascending order. To sort these cards, we have a
number of intuitive ways.
• One such thing we can do is initially we can hold all of the cards in our
left hand, and we can start taking cards one after other from the left
hand, followed by building a sorted arrangement in the right hand.
Contd..
• Assuming the first card to be already sorted, we will select the next
unsorted card. If the unsorted card is found to be greater than the
selected card, we will simply place it on the right side, else to the left
side. At any stage during this whole process, the left hand will be
unsorted, and the right hand will be sorted.
• In the same way, we will sort the rest of the unsorted cards by placing
them in the correct position. At each iteration, the insertion algorithm
places an unsorted element at its right place.
How Insertion Sort Works
1. We will start by assuming the very first element of the array is already sorted. Inside
the key, we will store the second element.
Next, we will compare our first element with the key, such that if the key is found to be
smaller than the first element, we will interchange their indexes or place the key at the
first index. After doing this, we will notice that the first two elements are sorted.
2. Now, we will move on to the third element and compare it with the left-hand side
elements. If it is the smallest element, then we will place the third element at the first
index.
Else if it is greater than the first element and smaller than the second element, then we
will interchange its position with the third element and place it after the first element.
After doing this, we will have our first three elements in a sorted manner.
3. Similarly, we will sort the rest of the elements and place them in their correct position.
Technique
• Consider an array A whose elements are to be sorted. Initially, A[0] is the only
element on the sorted set. In pass 1, A[1] is placed at its proper index in the
array.
• All the elements from A[k-1] to A[j] need to be shifted and A[k] will be moved
to A[j+1].

• In pass 2, A[2] is placed at its proper index in the array. Likewise, in pass n-1,
A[n-1] is placed at its proper index into the array.
• To insert an element A[k] to its proper index, we must compare it with all
other elements i.e. A[k-1], A[k-2], and so on until we find an element A[j] such
that, A[j]<=A[k].
ALGORITHM: INSERTION SORT (A)
1. for j = 2 to A.length
2. key = A[j]
3. // Insert A[j] into the sorted sequence A[1.. j - 1]
4. i = j - 1
5. while i > 0 and A[i] > key
6. A[i + 1] = A[i]
7. i = i -1
8. A[i + 1] = key
#include <stdlib.h> int array[5] = {5, 1, 6, 2, 4, 3};
#include <iostream> // calling insertion sort function to sort
using namespace std; the array
insertionSort(array, 6);
//member functions declaration return 0;
void insertionSort(int arr[], int length);
}
void printArray(int array[], int size);

// main function void insertionSort(int arr[], int length)


int main() {
{ int i, j, key;
for (i = 1; i < length; i++) // print the sorted array
{ printArray(arr, length);
j = i; }
while (j > 0 && arr[j - 1] > arr[j]) // function to print the given array
{ void printArray(int array[], int size)
key = arr[j]; {
arr[j] = arr[j - 1]; int j;
arr[j - 1] = key; for (j = 0; j < size; j++)
j--; {
} cout <<" "<< array[j];
} }
cout << "Sorted Array: "; cout << endl;
}
#include<stdio.h> while(j>=0 && temp <= a[j])
void main () {
{ a[j+1] = a[j];
j = j-1;
int i,j, k,temp;
}
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78,
34, 23}; a[j+1] = temp;
}
printf("\nprinting sorted elements...\n");
for(i=0;i<10;i++)
for(k=1; k<10; k++)
{
{ printf("\n%d\n",a[i]);
temp = a[k]; }
j= k-1; }
Complexity Analysis of Insertion Sort
• Input: Given n input elements.
• Output: Number of steps incurred to sort a list.

• Logic: If we are given n elements, then in the first pass, it will make n-1
comparisons; in the second pass, it will do n-2; in the third pass, it will do n-3
and so on. Thus, the total number of comparisons can be found by;

• Output;
• (n-1) + (n-2) + (n-3) + (n-4) + ...... + 1
• = O(n2)
Time Complexities/ Space Complexity
• Best Case Complexity: The insertion sort algorithm has a best-case time complexity of O(n) for the
already sorted array because here, only the outer loop is running n times, and the inner loop is kept still.
• Average Case Complexity: The average-case time complexity for the insertion sort algorithm is O(n2),
which is incurred when the existing elements are in jumbled order, i.e., neither in the ascending order
nor in the descending order.
• Worst Case Complexity: The worst-case time complexity is also O(n2), which occurs when we sort the
ascending order of an array into the descending order.
• In this algorithm, every individual element is compared with the rest of the elements, due to which n-1
comparisons are made for every nth element.
• The insertion sort algorithm is highly recommended, especially when a few elements are left for sorting
or in case the array encompasses few elements.

• The insertion sort encompasses a space complexity of O(1) due to the usage of an extra variable key.
Contd..
• Worst Case Time Complexity [ Big-O ]: O(n2)

• Best Case Time Complexity [Big-omega]: O(n)

• Average Time Complexity [Big-theta]: O(n2)

• Space Complexity: O(1)


• https://www.studytonight.com/data-structures/bubble-sort
• https://www.studytonight.com/data-structures/selection-sorting
• https://www.studytonight.com/data-structures/insertion-sorting
• https://www.javatpoint.com/daa-bubble-sort
• https://www.javatpoint.com/daa-selection-sort
• https://www.javatpoint.com/daa-insertion-sort
• https://www.javatpoint.com/bubble-sort
• https://www.javatpoint.com/selection-sort
• https://www.javatpoint.com/insertion-sort

You might also like