assignment 2
assignment 2
ASSIGNMENT - 2
Topic On :
Compare the searching algorithms (Linear & Binary), sorting algorithms (Bubble,
Selection, Insertion, Merge, Quick, Bucket, Count, Radix, & Heap) on the basis of
following terms:
Deadline: 25/11/2024
Comparison of Searching and Sorting Algorithms
Searching Algorithms
1. Linear Search
Pros:
- Simple and easy to implement.
- Works well for small datasets or unsorted arrays.
- Does not require the dataset to be sorted.
Cons:
- Slow for large datasets (O(n)).
- Inefficient when the dataset grows in size.
Applications:
- Suitable for small arrays or where unsorted data is common.
- Searching in linked lists.
2. Binary Search
Pros:
- Much faster than linear search (O(log n)).
- Ideal for sorted datasets.
- Divide-and-conquer approach makes it efficient.
Cons:
- Dataset must be sorted.
- Slightly more complex to implement than linear search.
Applications:
- Searching in sorted arrays or datasets.
- Used in applications like dictionary lookup, finding roots in numerical analysis.
1|Page
Sorting Algorithms
Algorithm Pros Cons Applications
Bubble Sort Simple, easy to Very slow for large Used in educational purposes
understand, and datasets (O(n^2)). and small datasets where
implement. simplicity is valued.
Selection Does not require Inefficient for large Useful for small datasets
Sort additional memory, datasets (O(n^2)). where memory is limited.
and swaps are
minimized.
Insertion Efficient for small Inefficient for large, Used in online algorithms
Sort datasets or nearly randomly ordered (data arrives in a stream) and
sorted data (O(n) datasets (O(n^2)). nearly sorted datasets like
for almost-sorted library books.
datasets).
Merge Sort Very efficient for Requires additional External sorting (e.g., sorting
large datasets, and memory (O(n)) for data stored on a disk),
has a predictable temporary arrays. scenarios requiring stable
runtime (O(n log sorting like sorting names by
n)). alphabetical order.
Quick Sort Extremely efficient Worst-case General-purpose sorting
on average (O(n performance is O(n^2) where efficiency is key, used
log n)) and uses in- (can be avoided with in libraries like Python's
place sorting. good pivot selection). sorted().
Heap Sort Efficient for large Comparatively slower Used in scenarios where
datasets, and does than quick sort in memory usage needs to be
not require practice. minimized, and stability is
additional memory not required.
(O(1) auxiliary
space).
Bucket Sort Very fast for data Requires prior Used for datasets with
uniformly knowledge of the known distributions (e.g.,
distributed over a distribution, and sorting numbers in a specific
range (O(n + k)). performance depends range).
on bucket count.
Counting Fast and efficient Not suitable for large Suitable for digit-based
Sort for integer keys in range keys, and not problems like counting
a limited range comparison-based frequencies or specific
(O(n + k)). (cannot sort non- datasets (e.g., age groups in a
integer data). population).
Radix Sort Works well for Limited to numeric or Used for large numeric
data with fixed- fixed-width data. datasets, sorting phone
length numbers, or other datasets
representations with fixed lengths.
(O(nk)).
2|Page
Detailed Comparisons
1. Linear Search vs Binary Search:
- Binary search is far more efficient for sorted datasets, as it reduces the search space
exponentially.
- Linear search is versatile since it works for unsorted data but is inefficient for larger
datasets.
3|Page