adding report
This commit is contained in:
36
README.md
36
README.md
@@ -17,7 +17,9 @@ MSCS532_Assignment5/
|
||||
│ ├── quicksort_comparison.png # Comparative performance (line plots)
|
||||
│ ├── quicksort_comparison_bar.png # Deterministic vs randomized comparison
|
||||
│ ├── quicksort_scalability.png # Scalability on random inputs
|
||||
│ └── quicksort_worst_case.png # Worst-case behavior on sorted inputs
|
||||
│ ├── quicksort_worst_case.png # Worst-case behavior on sorted inputs
|
||||
│ ├── quicksort_3way_comparison.png # Three-way vs standard on duplicates (line plots)
|
||||
│ └── quicksort_3way_bar.png # Three-way vs standard on duplicates (bar chart)
|
||||
├── examples/
|
||||
│ ├── quicksort_demo.py # Usage demonstrations
|
||||
│ ├── comparison_demo.py # Benchmark walkthrough
|
||||
@@ -52,18 +54,18 @@ MSCS532_Assignment5/
|
||||
|
||||
| Scenario | Deterministic Quicksort | Randomized Quicksort | Notes |
|
||||
|---------------|-------------------------|----------------------|-------|
|
||||
| Best Case | $\(O(n \log n)\)$ | $\(O(n \log n)\)$ | Balanced partitions from median pivots |
|
||||
| Average Case | $\(O(n \log n)\)$ | $\(O(n \log n)\)$ | Expected logarithmic recursion depth |
|
||||
| Worst Case | $\(O(n^2)\)$ | $\(O(n^2)\)$ | Occurs with highly unbalanced splits |
|
||||
| Best Case | $O(n \log n)$ | $O(n \log n)$ | Balanced partitions from median pivots |
|
||||
| Average Case | $O(n \log n)$ | $O(n \log n)$ | Expected logarithmic recursion depth |
|
||||
| Worst Case | $O(n^2)$ | $O(n^2)$ | Occurs with highly unbalanced splits |
|
||||
|
||||
- **Average-case intuition:** Balanced partitions of size \(n/2\) produce the recurrence $\(T(n) = 2T(n/2) + O(n)\)$, which resolves to $\(O(n \log n)\)$.
|
||||
- **Worst-case intuition:** Consistently poor pivots reduce the problem by one element, yielding $\(T(n) = T(n - 1) + O(n)\)$ and $\(O(n^2)\)$ behavior.
|
||||
- **Space complexity:** $\(O(\log n)\)$ expected stack depth for balanced recursion, $\(O(n)\)$ in the worst case. Randomized pivot selection significantly decreases the probability of worst-case depth on adversarial inputs.
|
||||
- **Average-case intuition:** Balanced partitions of size $n/2$ produce the recurrence $T(n) = 2T(n/2) + O(n)$, which resolves to $O(n \log n)$.
|
||||
- **Worst-case intuition:** Consistently poor pivots reduce the problem by one element, yielding $T(n) = T(n - 1) + O(n)$ and $O(n^2)$ behavior.
|
||||
- **Space complexity:** $O(\log n)$ expected stack depth for balanced recursion, $O(n)$ in the worst case. Randomized pivot selection significantly decreases the probability of worst-case depth on adversarial inputs.
|
||||
|
||||
## 3. Randomized Quicksort
|
||||
|
||||
- Randomization chooses pivots uniformly at random, ensuring that any specific pivot ordering is unlikely.
|
||||
- While the theoretical worst case remains $\(O(n^2)\)$, the probability of encountering it drops exponentially with input size.
|
||||
- While the theoretical worst case remains $O(n^2)$, the probability of encountering it drops exponentially with input size.
|
||||
- The implementation exposes an optional `seed` to guarantee repeatable experimental runs while retaining stochastic behavior by default.
|
||||
|
||||
## 4. Empirical Analysis
|
||||
@@ -78,7 +80,7 @@ MSCS532_Assignment5/
|
||||
### Key Observations
|
||||
|
||||
- Randomized Quicksort consistently outperforms deterministic Quicksort on sorted and reverse-sorted arrays by avoiding degenerate partitions.
|
||||
- Both versions exhibit $\(O(n \log n)\)$ scaling on random inputs, aligning with theoretical expectations.
|
||||
- Both versions exhibit $O(n \log n)$ scaling on random inputs, aligning with theoretical expectations.
|
||||
- Deterministic Quicksort degrades toward quadratic performance as inputs approach worst-case ordering; randomization flattens this curve.
|
||||
- Three-way Quicksort (explored in examples/tests) provides strong performance on datasets with heavy duplication.
|
||||
|
||||
@@ -90,9 +92,9 @@ In some visualizations (particularly `quicksort_worst_case.png` and `quicksort_c
|
||||
|
||||
**Why execution times become infinite:**
|
||||
|
||||
1. **Worst-case complexity:** On sorted or reverse-sorted inputs, deterministic Quicksort (using the last element as pivot) creates highly unbalanced partitions, resulting in $\(O(n^2)\)$ time complexity.
|
||||
1. **Worst-case complexity:** On sorted or reverse-sorted inputs, deterministic Quicksort (using the last element as pivot) creates highly unbalanced partitions, resulting in $O(n^2)$ time complexity.
|
||||
|
||||
2. **Recursion depth:** For large arrays (typically ≥ 1,000 elements), the algorithm requires $\(O(n)\)$ recursive calls, which can exceed Python's default recursion limit (usually 1,000) and raise a `RecursionError`.
|
||||
2. **Recursion depth:** For large arrays (typically ≥ 1,000 elements), the algorithm requires $O(n)$ recursive calls, which can exceed Python's default recursion limit (usually 1,000) and raise a `RecursionError`.
|
||||
|
||||
3. **Timeout behavior:** Even when recursion limits are increased, the quadratic time complexity means execution times grow prohibitively large. For arrays of size 5,000 or 10,000, deterministic Quicksort may take minutes or hours to complete, making it impractical for benchmarking.
|
||||
|
||||
@@ -112,16 +114,24 @@ In some visualizations (particularly `quicksort_worst_case.png` and `quicksort_c
|
||||
|
||||

|
||||
|
||||
<sub>*Figure 2. Runtime comparison on random, sorted, and reverse-sorted arrays (n = 5,000). Missing bars for deterministic Quicksort on sorted/reverse-sorted inputs indicate execution failures due to worst-case \(O(n^2)\) performance.*</sub>
|
||||
<sub>*Figure 2. Runtime comparison on random, sorted, and reverse-sorted arrays (n = 5,000). Missing bars for deterministic Quicksort on sorted/reverse-sorted inputs indicate execution failures due to worst-case $O(n^2)$ performance.*</sub>
|
||||
|
||||

|
||||
|
||||
<sub>*Figure 3. Log-log visualization of scalability on random inputs with \(O(n \log n)\) reference.*</sub>
|
||||
<sub>*Figure 3. Log-log visualization of scalability on random inputs with $O(n \log n)$ reference.*</sub>
|
||||
|
||||

|
||||
|
||||
<sub>*Figure 4. Worst-case analysis contrasting sorted and reverse-sorted distributions. Missing bars for deterministic Quicksort at larger sizes (≥1,000) indicate execution failures due to recursion limits and quadratic time complexity.*</sub>
|
||||
|
||||

|
||||
|
||||
<sub>*Figure 5. Three-way Quicksort vs Standard Quicksort on duplicate-heavy data. Performance comparison across three duplicate configurations: 10 unique values, 5 unique values, and all equal elements. Three-way Quicksort demonstrates superior performance, especially as duplicate frequency increases.*</sub>
|
||||
|
||||

|
||||
|
||||
<sub>*Figure 6. Detailed bar chart comparison of Three-way Quicksort vs Standard Quicksort on duplicate-heavy data at specific array sizes. Shows the performance advantage of three-way partitioning when dealing with many duplicate elements.*</sub>
|
||||
|
||||
## Getting Started
|
||||
|
||||
### Prerequisites
|
||||
|
||||
Reference in New Issue
Block a user