Binary Search
Binary Search
• 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.
• 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);
• 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)