A Go implementation of parallel algorithms and data processing techniques, featuring optimized sorting, merging, and weather-based mood calculation algorithms.
This project demonstrates various parallel programming techniques in Go, including:
- Parallel Merge Algorithm: Efficiently merges two sorted arrays using concurrent processing
- Happy Days Algorithm: Calculates daily moods based on weather patterns using parallel processing
- Adaptive Parallel QuickSort: An optimized parallel sorting algorithm with adaptive thresholds
- Merges two sorted arrays in parallel using goroutines
- Uses binary search to find optimal cut points for parallel processing
- Handles edge cases for empty arrays efficiently
- Calculates daily moods based on weather conditions
- Supports three weather types:
sunny,rainy,cloudy - Uses parallel processing to handle large weather datasets
- Mood rules:
sunny→happyrainy→unhappycloudy→ maintains previous mood
- Implements an adaptive parallel sorting algorithm
- Uses sampling technique to select optimal pivots
- Automatically switches to serial sorting for small arrays (threshold: 2000 elements)
- Configurable concurrency level (default: 100 goroutines)
- Includes custom semaphore implementation for concurrency control
- Custom semaphore implementation for managing concurrent operations
- Provides
Acquire(),Release(), andWait()methods for goroutine synchronization
hpc-questions/
├── main.go # Main entry point with test functions
├── go.mod # Go module definition
├── src/ # Source code directory
│ ├── parallel_merge.go # Parallel merge implementation
│ ├── happy_days.go # Weather-based mood calculation
│ ├── optimized_quicksort.go # Adaptive parallel sorting
│ └── utils.go # Utility functions and semaphore
└── README.md # This file
- Go 1.24.6 or later
- No external dependencies
- Clone the repository:
git clone <repository-url>
cd hpc-questions- Build the project:
go build -o hpc-questions main.goExecute the main program to see all algorithms in action:
go run main.goThis will run three test functions:
testParallelMerge(): Demonstrates parallel array mergingtestHappyDays(): Shows weather-based mood calculationtestAdaptiveParallelSort(): Tests the parallel sorting algorithm
A := []int{0, 1, 3, 5, 7, 9}
B := []int{2, 4, 6, 8}
mergedArray := src.ParallelMerge(A, B)weather := []string{"sunny", "rainy", "cloudy", "sunny"}
moods := src.HappyDays(weather)data := []int{5, 2, 8, 1, 9, 3}
sorted := src.AdaptiveParallelSort(data)- Time Complexity: O(log n) for finding cut points, O(n) for merging
- Space Complexity: O(n)
- Concurrency: Uses 2 goroutines for parallel processing
- Time Complexity: O(n) with parallel processing
- Space Complexity: O(n)
- Concurrency: Recursive parallel processing with mood state propagation
- Time Complexity: O(n log n) average case
- Space Complexity: O(n)
- Concurrency: Configurable (default: 100 goroutines)
- Adaptive: Switches to serial sorting for arrays < 2000 elements
You can modify the following constants in optimized_quicksort.go:
const (
NumConcurrency = 100 // Number of concurrent goroutines
Threshold = 2000 // Switch to serial sorting threshold
)- The parallel algorithms are most effective on large datasets
- Small arrays may perform better with serial algorithms due to goroutine overhead
- Memory usage scales with input size and concurrency level
- The adaptive threshold helps balance parallel efficiency with overhead
- Author: chengxiang.luo
- Email: chengxiang.luo@foxmail.com
- Project: knowledge-forge
This project is part of the knowledge-forge project. Please refer to the main project for licensing information.
Contributions are welcome! Please feel free to submit issues and pull requests to improve the algorithms or add new parallel processing techniques.