Sorting
Sorting
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.
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
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.
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.
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.
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.