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

Sorting

The document provides an overview of sorting and searching algorithms, detailing their types, classifications, and key properties. It highlights various algorithms such as Quick Sort, Merge Sort, Linear Search, and Binary Search, along with their time complexities and use cases. The document concludes with recommendations for selecting appropriate algorithms based on specific scenarios.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Sorting

The document provides an overview of sorting and searching algorithms, detailing their types, classifications, and key properties. It highlights various algorithms such as Quick Sort, Merge Sort, Linear Search, and Binary Search, along with their time complexities and use cases. The document concludes with recommendations for selecting appropriate algorithms based on specific scenarios.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

1.

Introduction to Sorting
Sorting is the process of arranging elements in a specific order (ascending or descending). Efficient sorting
improves searching, data retrieval, and processing performance.

Types of Sorting Algorithms:


1. Comparison-Based Sorting (Relies on element comparisons)
○ Bubble Sort
○ Selection Sort
○ Insertion Sort
○ Merge Sort
○ Quick Sort
○ Heap Sort
2. Non-Comparison-Based Sorting (Sorts without direct comparisons)
○ Radix Sort
○ Counting Sort
○ Bucket Sort

2. Classification of Sorting Algorithms


Algorithm Best Case Worst Case Average Case Space Complexity Stable? In-place?
Bubble Sort O(n) O(n²) O(n²) O(1) ✅ Yes ✅ Yes
Selection Sort O(n²) O(n²) O(n²) O(1) ❌ No ✅ Yes
Insertion Sort O(n) O(n²) O(n²) O(1) ✅ Yes ✅ Yes
Merge Sort O(n log n) O(n log n) O(n log n) O(n) ✅ Yes ❌ No
Quick Sort O(n log n) O(n²) O(n log n) O(log n) ❌ No ✅ Yes
Heap Sort O(n log n) O(n log n) O(n log n) O(1) ❌ No ✅ Yes
Counting Sort O(n + k) O(n + k) O(n + k) O(k) ✅ Yes ❌ No
Radix Sort O(nk) O(nk) O(nk) O(n + k) ✅ Yes ❌ No
Stable Sorting: A sorting algorithm is stable if it preserves the order of duplicate elements.
In-place Sorting: An algorithm is in-place if it sorts without using extra space (O(1) additional memory).

3. Detailed Explanation of Key Sorting Algorithms


A. Bubble Sort
Concept: Repeatedly compares adjacent elements and swaps them if they are in the wrong order.
Best for: Small datasets, nearly sorted arrays.
Time Complexity:
• Best Case: O(n) (Already sorted)
• Worst Case: O(n²) (Reversed order)
Key Properties:
• Stable ✅
• In-place ✅
Drawback: Inefficient for large datasets.

B. Selection Sort
Concept: Finds the smallest element and swaps it with the first unsorted element.
Best for: Small datasets, when memory is constrained.
Time Complexity:
• Best Case: O(n²)
• Worst Case: O(n²)
Key Properties:
• Stable ❌
• In-place ✅
Drawback: Always runs in O(n²), even if the array is already sorted.

C. Insertion Sort
Concept: Builds the sorted array by inserting elements at their correct positions.
Best for: Small datasets, nearly sorted arrays.
Time Complexity:
• Best Case: O(n) (Nearly sorted)
• Worst Case: O(n²)
Key Properties:
• Stable ✅
• In-place ✅
Advantage: Efficient for small data and online sorting (processes elements as they arrive).

D. Merge Sort
Concept: Uses Divide and Conquer to recursively split and merge sorted subarrays.
Best for: Large datasets, linked lists.
Time Complexity:
• Best Case: O(n log n)
• Worst Case: O(n log n)
Key Properties:
• Stable ✅
• In-place ❌ (Uses O(n) extra space)
Advantage: Works well with external sorting (sorting large files).

E. Quick Sort

COPY FILE 2 Page 1


E. Quick Sort
Concept: Uses Divide and Conquer to partition the array around a pivot element.
Best for: Large datasets, general-purpose sorting.
Time Complexity:
• Best Case: O(n log n)
• Worst Case: O(n²) (If poorly chosen pivot)
Key Properties:
• Stable ❌
• In-place ✅
Advantage: Faster than Merge Sort in practice.
Optimization: Use random pivot selection to reduce the risk of O(n²) performance.

F. Heap Sort
Concept: Converts array into a heap data structure and extracts the max/min element.
Best for: Priority queues, large datasets.
Time Complexity:
• Best Case: O(n log n)
• Worst Case: O(n log n)
Key Properties:
• Stable ❌
• In-place ✅
Advantage: Efficient for in-place sorting, but slower than Quick Sort in practice.

G. Counting Sort (Non-Comparison Sorting)


Concept: Counts occurrences of elements and reconstructs the sorted array.
Best for: Small-range integer keys.
Time Complexity:
• Best Case: O(n + k)
• Worst Case: O(n + k)
Key Properties:
• Stable ✅
• In-place ❌
Advantage: Faster than comparison sorts when k is small.
Drawback: Not useful for large ranges or floating-point numbers.

H. Radix Sort (Non-Comparison Sorting)


Concept: Sorts numbers digit by digit using Counting Sort as a subroutine.
Best for: Large numbers, fixed-length strings.
Time Complexity:
• Best Case: O(nk)
• Worst Case: O(nk)
Key Properties:
• Stable ✅
• In-place ❌
Advantage: Efficient for large integers but not suitable for floating points or variable-length data.

4. Which Sorting Algorithm to Use?


Scenario Recommended Sorting Algorithm
Small dataset Insertion Sort
Large dataset Quick Sort / Merge Sort
Nearly sorted array Insertion Sort
Stable sorting needed Merge Sort
Limited memory Heap Sort
Sorting linked lists Merge Sort
Large numbers (integers) Radix Sort

5. Summary
• Quick Sort is fastest for general cases but not stable.
• Merge Sort is best for stability and linked lists.
• Insertion Sort is best for small datasets or nearly sorted arrays.
• Heap Sort is useful when constant memory is required.
• Counting & Radix Sort are non-comparison sorts used for specific cases.

Searching Algorithms - Notes


1. Introduction to Searching
Searching is the process of finding an element in a data structure. Efficient searching improves data retrieval,
processing speed, and decision-making.
Types of Searching Algorithms:
1. Linear Searching (Sequential search)
○ Linear Search
2. Binary Searching (Divide and Conquer approach)
○ Binary Search
○ Exponential Search
○ Interpolation Search
○ Jump Search
3. Hash-Based Searching (Uses hash tables)
○ Hashing

2. Classification of Searching Algorithms

COPY FILE 2 Page 2


2. Classification of Searching Algorithms
Algorithm Best Case Worst Case Average Case Space Precondition
Complexity
Linear Search O(1) O(n) O(n) O(1) No sorting required
Binary Search O(1) O(log n) O(log n) O(1) Requires sorted
array
Jump Search O(1) O(√n) O(√n) O(1) Requires sorted
array
Interpolation O(1) O(n) O(log log n) O(1) Requires sorted,
Search uniformly
distributed array
Exponential O(1) O(log n) O(log n) O(1) Requires sorted
Search array
Hashing O(1) O(n) O(1) O(n) Uses a hash
function
Comparison-based searches (Binary, Interpolation, Jump) require sorting.
Hashing provides the fastest search when properly implemented.

3. Detailed Explanation of Key Searching Algorithms


A. Linear Search
Concept: Searches for an element sequentially, checking each item.
Best for: Small datasets, unsorted arrays.
Time Complexity:
• Best Case: O(1) (Element found at the beginning)
• Worst Case: O(n) (Element at the end or not found)
Key Properties:
• Simple but slow
• No pre-sorting required
• Useful when data is constantly changing
Drawback: Inefficient for large datasets.

B. Binary Search
Concept: Repeatedly divides a sorted array into halves to find the target.
Best for: Large sorted datasets.
Time Complexity:
• Best Case: O(1) (Element found in the middle)
• Worst Case: O(log n)
Key Properties:
• Efficient for sorted data
• Much faster than Linear Search
• Cannot be used for unsorted data
Drawback: Requires sorting before searching.

C. Jump Search
Concept: Skips a fixed number of elements (√n steps) and performs Linear Search when close to the target.
Best for: Sorted arrays where random access is possible.
Time Complexity:
• Best Case: O(1)
• Worst Case: O(√n)
Key Properties:
• Faster than Linear Search, but slower than Binary Search
• Works well when the dataset is moderately large
Drawback: Less efficient for very large datasets.

D. Interpolation Search
Concept: Uses a formula to estimate the position of the target in a sorted array (similar to how people search for
words in a dictionary).
Best for: Sorted and uniformly distributed data.
Time Complexity:
• Best Case: O(1)
• Worst Case: O(n)
• Average Case: O(log log n) (Faster than Binary Search)
Key Properties:
• Faster than Binary Search for uniformly distributed data
• Efficient for large datasets
Drawback: Performs poorly if data is not uniformly distributed.

E. Exponential Search
Concept: Expands the search range exponentially (1, 2, 4, 8...) until it surpasses the target, then performs Binary
Search.
Best for: Large sorted datasets.
Time Complexity:
• Best Case: O(1)
• Worst Case: O(log n)
Key Properties:
• Faster than Binary Search when the target is near the beginning
• Used in unbounded lists (e.g., infinite-sized databases)
Drawback: Requires sorted data.

F. Hashing (Fastest Search Method)

COPY FILE 2 Page 3


F. Hashing (Fastest Search Method)
Concept: Uses a hash function to map keys to indices, allowing near constant-time search O(1).
Best for: Large datasets with unique keys (e.g., databases, caching).
Time Complexity:
• Best Case: O(1)
• Worst Case: O(n) (In case of too many collisions)
Key Properties:
• Super fast search speed
• Ideal for large datasets
• Does not require sorting
Drawback: Requires a good hash function to avoid collisions.

4. Which Searching Algorithm to Use?


Scenario Recommended Searching Algorithm
Unsorted data Linear Search
Large sorted dataset Binary Search
Very large dataset with uniform distribution Interpolation Search
Searching an unknown-sized dataset Exponential Search
Data with fast random access (sorted) Jump Search
Large dataset with key-value pairs Hashing

5. Summary
• Linear Search is the simplest but slowest search method.
• Binary Search is much faster than Linear Search but requires sorting.
• Jump Search & Interpolation Search optimize searching by skipping elements.
• Exponential Search is useful for unknown-length lists.
• Hashing is the fastest when implemented well, but requires extra memory.

Why Hashing?
• Efficient Searching: Faster than binary search (O(1) vs. O(log n)).
• Efficient Insertion & Deletion: Unlike arrays and trees, insertion/deletion is fast.
• Memory Efficient: Avoids storing large sorted datasets.

COPY FILE 2 Page 4

You might also like