Famous Sorting Algorithms
only top 5 known
sorting algoritms just the basic logic in 2-3 points and their complexity and which peroms based in
which situation and compare in table at last andalso add which one is best in time wise and space
wise at last
Here are the top 5 known sorting algorithms along with their basic logic, time and space complexities,
and which situations they perform best in:
1. Quick Sort
Logic: Divide-and-conquer algorithm. Choose a pivot, partition the array around it, and recursively
sort the sub-arrays.
Time Complexity:
Best/Average: O(n log n)
Worst: O(n²) (if pivot is not well-chosen)
Space Complexity: O(log n)
Best for: Large datasets with a good average case performance; commonly used due to its
efficient average time complexity.
2. Merge Sort
Logic: Divide the array into two halves, recursively sort each half, then merge the sorted halves.
Time Complexity: O(n log n) for all cases (best, average, and worst).
Space Complexity: O(n) (due to the extra space required for merging).
Best for: Stable sorting and when guaranteed O(n log n) performance is needed.
3. Bubble Sort
Logic: Repeatedly compare adjacent elements and swap them if they are in the wrong order. This
process is repeated until the array is sorted.
Time Complexity:
Best: O(n) (if already sorted)
Worst/Average: O(n²)
Space Complexity: O(1)
Best for: Small datasets or when simplicity is more important than efficiency.
4. Insertion Sort
Logic: Build a sorted portion of the array one element at a time by inserting each element into its
correct position within the sorted portion.
Time Complexity:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/2
Best: O(n) (if already sorted)
Worst/Average: O(n²)
Space Complexity: O(1)
Best for: Small or nearly sorted datasets.
5. Heap Sort
Logic: Build a max heap, then repeatedly extract the maximum element from the heap and place it
in the sorted array.
Time Complexity: O(n log n) for all cases (best, average, and worst).
Space Complexity: O(1)
Best for: When in-place sorting with O(n log n) time complexity is required.
Comparison Table:
Time Complexity Time Complexity Space
Algorithm Best Use Case
(Best) (Worst) Complexity
Large datasets, average case
Quick Sort O(n log n) O(n²) O(log n)
sorting
Stable sort, guaranteed O(n log
Merge Sort O(n log n) O(n log n) O(n)
n) performance
Bubble Sort O(n) O(n²) O(1) Small or nearly sorted datasets
Insertion
O(n) O(n²) O(1) Small or nearly sorted datasets
Sort
In-place sorting with O(n log n)
Heap Sort O(n log n) O(n log n) O(1)
complexity
Best Sorting Algorithms (Time-wise & Space-wise):
Best in Time: Quick Sort (Average case is O(n log n)).
Best in Space: Heap Sort (O(1) space complexity).
Quick Sort is often preferred for its fast average-case performance, while Heap Sort is favored when
space efficiency is crucial.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/2