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

Leftovers As Exercises in Class

The document discusses several data structures and algorithms topics like binary search, sorting algorithms including bubble sort, insertion sort, selection sort and merge sort. It provides implementation details and compares time complexities of these algorithms.

Uploaded by

Siddharth Jindal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Leftovers As Exercises in Class

The document discusses several data structures and algorithms topics like binary search, sorting algorithms including bubble sort, insertion sort, selection sort and merge sort. It provides implementation details and compares time complexities of these algorithms.

Uploaded by

Siddharth Jindal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

DATA STRUCTURES AND ALGORITHMS

Few exercises given in the class and leftovers


Binary Search
Binary Search-Iterative Algorithm
● Steps for Binary Search:

– If the target value is equal to the middle element of the array, then return the
index of the middle element
– Compare target with middle indexed value
– If target is greater than the middle value, then target can only lie in the right
half subarray, so the procedure (step 1 & 2) will be repeated for right subarray.
– If target is smaller than the middle value, then target can only lie in the left half
subarray, so the procedure (step 1 & 2) will be repeated for the left subarray.

● Condition: The array must be sorted before applying the algorithm


Binary Search-Iterative Algorithm
Binary Search-Iterative Algorithm-Implementation
#include <iostream>
using namespace std; int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int binarySearch(int array[], int x, int low, int x = 4;
int high) { int n = sizeof(array);
int result = binarySearch(array, x, 0, n - 1);
// Repeat until the pointers low and high meet each other
if (result == -1)
while (low <= high) {
printf("Not found");
int mid = low + (high - low) / 2;
else
printf("Element is found at index %d",
if (array[mid] == x)
result);
return mid;
}
if (array[mid] < x)
low = mid + 1;

else
high = mid - 1;
}

return -1;
}
Binary Search-Time Complexity

● Time Complexities

● Best case complexity: O(1)


● Average case complexity: O(log n)
● Worst case complexity: O(log n)
Sorting
Bubble sort

● Time Complexity

● Complexity (Worst case) = O(n2)


● Complexity (Best case) = O (n) (When we use the flag to
check whether the array is already sorted or not)
Insertion Sort
● What are the Boundary Cases of the Insertion Sort algorithm?
– Insertion sort takes maximum time to sort if elements are sorted in reverse
order. And it takes minimum time (Order of n) when elements are already
sorted.
● Is Insertion Sort an in-place sorting algorithm?
– Yes, insertion sort is an in-place sorting algorithm.
● When is the Insertion Sort algorithm used?
– Insertion sort is used when number of elements is small. It can also be
useful when input array is almost sorted, only few elements are misplaced
in complete big array.
Insertion Sort

● Time Complexity:
● Worst case = O(n2)
● Average case = O(n2)
● Best case = O (n)
Selection Sort

● Time Complexity:
● Worst case = O(n2)
● Average case = O(n2)
● Best case = O (n2)
Selection Sort

● Disadvantage:
● Advantage:
● Poor efficiency with large list
● Works well on small lists
● It takes n2 no of steps for sorting of n
● No additional storage is needed
elements

● Selection Sort Applications: The selection


sort is used when

● a small list is to be sorted


● cost of swapping does not matter
● checking of all the elements is compulsory
Merge Sort

● Time complexity

– Merge Sort is a recursive algorithm and time complexity can


be expressed as recurrence relation.
– Time complexity of Merge Sort is O(nLogn) in all 3 cases
(worst, average and best).
– Merge sort always divides the array into two halves (O(logn))
and take linear time (O(n)) to merge two halves.
Stable Sorting Algorithms

● What is a stable sorting algorithm?


– A sorting algorithm that preserves the relative order of elements with
equal keys.
● If two elements with the same key appear in the input list in a particular
order, they will also appear in the same order in the output list.
– Examples: Merge Sort, Insertion Sort
Stable sorting
In-Place Sorting Algorithms

● What is an in-place sorting algorithm?


– A sorting algorithm that sorts the input array in place, without using
any additional memory.
● The algorithm does not need to create a new array to store the sorted
elements.
– In-place sorting algorithms are often used in applications where
memory is limited.
– Examples: Bubble Sort, Selection Sort
In-place sorting algorithm
Thank you

You might also like