Skip to content

Commit

Permalink
Merge pull request #1941 from jainaryan04/main
Browse files Browse the repository at this point in the history
blog on comparing sorting algorithms
  • Loading branch information
ajay-dhangar authored Nov 10, 2024
2 parents 8475d5d + 8b9b36c commit a25741e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,4 @@ Here are some resources that may be helpful as you contribute to Algo:
<a href="#top">
<img src="https://img.shields.io/badge/Back%20to%20Top-000000?style=for-the-badge&logo=github&logoColor=white" alt="Back to Top">
</a>
</div>
</div>
98 changes: 98 additions & 0 deletions blog/comparing-sortings.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
---
slug: comparing-sorting-algorithms
title: Comparing the different Sorting Algorithms
authors: [Aryan-Jain]
tags: [ARYAN-JAIN, algo, dsa, algorithms, sorting, time-complexity]
---

# Comparison of Sorting Algorithms

Sorting algorithms are essential for organizing data, making it easier to analyze, search, and manipulate. Here’s an overview of some commonly used sorting algorithms, comparing their time complexity, efficiency, and applications.

## 1. Bubble Sort
**Description**: Bubble Sort repeatedly steps through the list, compares adjacent elements, and swaps them if needed until the list is sorted.

- **Time Complexity**: $O(n^2)$ average and worst-case; $O(n)$ best-case (already sorted)
- **Space Complexity**: $O(1)$
- **Pros**: Simple and easy to implement
- **Cons**: Inefficient for large datasets
- **Use Case**: Suitable for small or nearly sorted datasets

## 2. Selection Sort
**Description**: Selection Sort divides the list into sorted and unsorted parts, finding the minimum element in the unsorted part and placing it in order.

- **Time Complexity**: $O(n^2)$ for all cases
- **Space Complexity**: $O(1)$
- **Pros**: Simple implementation; fewer swaps
- **Cons**: Inefficient for large datasets
- **Use Case**: Useful for small datasets or when memory writes are costly

## 3. Insertion Sort
**Description**: Insertion Sort builds a sorted list by taking one item from the unsorted portion and inserting it into the correct position.

- **Time Complexity**: $O(n^2)$ average and worst-case; $O(n)$ best-case (already sorted)
- **Space Complexity**: $O(1)$
- **Pros**: Efficient for small or nearly sorted lists
- **Cons**: Not suitable for large unsorted datasets
- **Use Case**: Good for small or mostly sorted datasets

## 4. Merge Sort
**Description**: Merge Sort is a divide-and-conquer algorithm that splits the list into halves, sorts each half, and then merges them.

- **Time Complexity**: $O(n \log n)$ for all cases
- **Space Complexity**: $O(n)$
- **Pros**: Consistent performance; stable sort
- **Cons**: Requires extra space
- **Use Case**: Effective for large datasets and linked lists

## 5. Quick Sort
**Description**: Quick Sort is a divide-and-conquer algorithm that selects a "pivot" element, partitions the array around it, and recursively sorts partitions.

- **Time Complexity**: $O(n \log n)$ average; $O(n^2)$ worst-case
- **Space Complexity**: $O(\log n)$
- **Pros**: Fast in practice due to in-place sorting
- **Cons**: Worst-case time can be $O(n^2)$; not stable
- **Use Case**: Suitable for large datasets, especially when optimizing for speed

## 6. Heap Sort
**Description**: Heap Sort uses a binary heap to sort by repeatedly extracting the largest element (root) and adjusting the heap.

- **Time Complexity**: $O(n \log n)$
- **Space Complexity**: $O(1)$
- **Pros**: Efficient, in-place sorting
- **Cons**: Not stable; slower in practice than Quick Sort
- **Use Case**: Used when in-place sorting is preferred and stability isn't essential

## 7. Radix Sort
**Description**: Radix Sort is a non-comparative algorithm that sorts integers by processing digits, usually using Counting Sort as a subroutine.

- **Time Complexity**: $O(d \cdot (n + k))$, where $d$ is the digit count and $k$ is the digit range
- **Space Complexity**: $O(n + k)$
- **Pros**: Fast for fixed-digit data; stable
- **Cons**: Limited to numbers or strings; extra space needed
- **Use Case**: Ideal for sorting large lists of integers or strings of uniform length

## 8. Counting Sort
**Description**: Counting Sort counts occurrences of each element and calculates their sorted positions.

- **Time Complexity**: $O(n + k)$, where $k$ is the input range
- **Space Complexity**: $O(n + k)$
- **Pros**: Fast and stable for specific data
- **Cons**: Limited to data with a small range; not for general-purpose sorting
- **Use Case**: Effective for lists with a limited value range

## Comparison Summary

| Algorithm | Best Time Complexity | Worst Time Complexity | Space Complexity | Stable | Use Cases |
|-----------------|----------------------|------------------------|------------------|--------|-----------------------------------------|
| Bubble Sort | $O(n)$ | $O(n^2)$ | $O(1)$ | Yes | Small or mostly sorted datasets |
| Selection Sort | $O(n^2)$ | $O(n^2)$ | $O(1)$ | No | Small datasets, costly memory writes |
| Insertion Sort | $O(n)$ | $O(n^2)$ | $O(1)$ | Yes | Small or nearly sorted datasets |
| Merge Sort | $O(n \log n)$ | $O(n \log n)$ | $O(n)$ | Yes | Large datasets, stable sorting needed |
| Quick Sort | $O(n \log n)$ | $O(n^2)$ | $O(\log n)$ | No | Large datasets, in-place and fast sort |
| Heap Sort | $O(n \log n)$ | $O(n \log n)$ | $O(1)$ | No | Large datasets, stability not required |
| Radix Sort | $O(n \cdot d)$ | $O(n \cdot d)$ | $O(n + k)$ | Yes | Large lists of numbers, fixed-length |
| Counting Sort | $O(n + k)$ | $O(n + k)$ | $O(n + k)$ | Yes | Small integer range, limited data range |

## Conclusion
Each sorting algorithm has unique strengths and weaknesses, and the choice depends on the dataset's characteristics. Quick Sort and Merge Sort work well with large datasets, while Bubble Sort, Selection Sort, and Insertion Sort are best for smaller or partially sorted data. Radix and Counting Sort are efficient for data like integers in a limited range.

0 comments on commit a25741e

Please sign in to comment.