Sorting_and_Data_Analysis_Algorithms
Sorting_and_Data_Analysis_Algorithms
Selection Sort works by repeatedly finding the minimum element from the unsorted portion
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].
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
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).
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.