Sorting and Data Analysis Algorithms
1. Selection Sort (with Example)
Selection Sort works by repeatedly finding the minimum element from the unsorted portion
of the array and swapping it with the first unsorted element.
Example:
Input: [64, 25, 12, 22, 11]
Steps:
1. Find the smallest element (11) and swap it with the first element: [11, 25, 12, 22, 64].
2. Find the next smallest element (12) and swap it with the second element: [11, 12, 25, 22,
64].
3. Continue until sorted: [11, 12, 22, 25, 64].
2. Heapsort, Heapify, Buildheap (with Example)
Heapsort:
1. Build a max heap from the input array.
2. Swap the root (largest) element with the last element of the heap.
3. Reduce the heap size by one and heapify the root.
4. Repeat until the heap is empty.
Heapify ensures the max-heap property for a subtree where the parent is greater than or
equal to its children.
Buildheap converts an entire array into a max heap by applying heapify starting from the
last non-leaf node.
Example:
Input: [4, 10, 3, 5, 1]
Steps:
1. Build Max Heap: [10, 5, 3, 4, 1]
2. Swap 10 with 1: [1, 5, 3, 4, 10], then heapify: [5, 4, 3, 1, 10].
3. Continue until sorted: [1, 3, 4, 5, 10].
3. Quick Sort (with Example)
Quick Sort works by selecting a "pivot" element, partitioning the array into two halves, and
recursively sorting each half.
Example:
Input: [10, 7, 8, 9, 1, 5]
Steps:
1. Pivot = 5. Partition: [1, 5, 8, 9, 7, 10].
2. Apply Quick Sort to [1] and [8, 9, 7, 10].
3. Continue recursively until sorted: [1, 5, 7, 8, 9, 10].
4. Stable Sorting
A sorting algorithm is stable if it maintains the relative order of equal elements.
Examples of Stable Sorting Algorithms:
- Bubble Sort
- Merge Sort
- Insertion Sort
Examples of Unstable Sorting Algorithms:
- Quick Sort
- Heap Sort
Example of Stability:
Input: [(A, 1), (B, 2), (C, 1)].
Stable sort by the second value results in [(A, 1), (C, 1), (B, 2)] (relative order preserved).
5. Difference Between Heapsort and Quicksort
| Aspect | Heapsort | Quicksort |
|---------------------|--------------------------------------|------------------------------------|
| Time Complexity | O(n log n) | Best: O(n log n), Worst: O(n^2) |
| Space Complexity | O(1) | O(log n) |
| Stability | Not stable | Not stable |
| Pivot Selection | Not required | Required |
| Performance | Slower in practice | Faster in most scenarios |
| Use Case | When guaranteed O(n log n) is needed| For faster average-case sorting |
6. Simple Data Analysis Algorithm: Mean, Median, Mode
Steps to Analyze Data:
1. Mean (Average):
- Add all the numbers in the dataset.
- Divide the sum by the total count of numbers.
- Example: Data = [2, 3, 5, 7] => Mean = (2 + 3 + 5 + 7) / 4 = 4.25.
2. Median:
- Sort the data.
- If the count is odd, the median is the middle value.
- If the count is even, the median is the average of the two middle values.
- Example: Data = [3, 1, 4, 2] => Sorted = [1, 2, 3, 4] => Median = (2 + 3) / 2 = 2.5.
3. Mode:
- The mode is the number that appears most frequently in the dataset.
- Example: Data = [4, 5, 5, 6, 7] => Mode = 5.