This project focuses on algorithm optimization, analysis, and practical implementations using sorting and searching algorithms. It includes benchmarking and visualization tools to analyze the performance of different algorithms in various scenarios.
- Introduction
- Features
- Installation
- macOS-Specific-installation
- Usage
- Detailed Analysis
- Results
- Conclusion
- Project Overview
- Problem Statement
- Questions
- Contributing
- License
The aim of this project is to investigate commonly used methods for optimizing algorithms and how these methods are classified. It delves into how mathematics is used to analyze algorithms and their performance, specifically focusing on Merge Sort, Quick Sort, and Binary Search algorithms.
The project stems from the need to understand how algorithm analysis and optimization can save time and computational resources. Algorithms are fundamental in solving problems efficiently, and optimizing them is crucial in practical applications like search engines, data processing, and more.
Relevant resources for learning about algorithm optimization include:
- GeeksforGeeks - Analysis of Algorithms (Theta Notation)
- Wikiwand - Time Complexity
- Khan Academy - Analysis of Quick Sort
- Sorting Algorithms Implementation: C++ implementations of Merge Sort and Quick Sort with different pivot strategies.
- Searching Algorithm Implementation: C++ implementation of Binary Search.
- Benchmarking Tool: Measures the performance of sorting and searching algorithms under Best, Average, and Worst-case scenarios.
- Visualization: Python script using
matplotlibto animate sorting processes and highlight code. - Profiling: C++ code profiles algorithm performance and outputs the results to JSON files compatible with Chrome Tracing.
Learn more about sorting and searching algorithm concepts at:
- C++ Compiler: Compatible with C++11 or higher (e.g., GCC, Clang, MSVC).
- Python 3.x: Along with the following packages:
numpymatplotlib
To understand benchmarking better, check:
-
Clone the Repository
git clone https://github.com/mbn-code/Algoritmeanalyse-og-optimering.git
-
Build the C++ Project
-
On macOS/Linux (using Makefile)
cd Algoritmeanalyse-og-optimering make -
On Windows (using Visual Studio)
- Open the solution file in Visual Studio.
- Build the project from the Build menu.
-
-
Install Python Dependencies
pip install numpy matplotlib
This section provides detailed instructions for setting up and compiling the project specifically on macOS.
Before building the project, ensure you have the following installed:
-
Xcode Command Line Tools (includes GCC/Clang compiler)
xcode-select --install
-
Homebrew (package manager for macOS)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" -
raylib (graphics library)
brew install raylib
-
pkg-config (required to find raylib packages)
brew install pkg-config
-
Bear (to generate compile_commands.json)
brew install bear
-
Python 3.x with required packages
pip install numpy matplotlib
Navigate to the project directory and run the following compilation command:
cd /Users/mbn/Documents/GitHub/Algorithmic-Analysis-and-Optimization/Algoritmeanalyse-og-optimering && \
g++ -std=c++17 -o Algoritmeanalyse-og-optimering *.cpp \
-I. \
$(pkg-config --cflags --libs raylib) \
-lm -lpthread; ./Algoritmeanalyse-og-optimeringExplanation:
-std=c++17: Uses the C++17 standard-o Algoritmeanalyse-og-optimering: Names the output executable*.cpp: Compiles all C++ source files-I.: Includes the current directory for headers$(pkg-config --cflags --libs raylib): Automatically includes raylib compilation flags and libraries-lm: Links the math library-lpthread: Links the pthread library for threading support
To generate the compile_commands.json file needed for code analysis tools and IDE language servers, use the bear command:
cd /Users/mbn/Documents/GitHub/Algorithmic-Analysis-and-Optimization/Algoritmeanalyse-og-optimering && \
bear -- g++ -std=c++17 -o Algoritmeanalyse-og-optimering *.cpp \
-I. \
$(pkg-config --cflags --libs raylib) \
-lm -lpthreadThis command wraps your compilation command with bear -- and generates a compile_commands.json file in the current directory. This file is essential for:
- Code intelligence and autocomplete in IDEs (VS Code, CLion, etc.)
- Static analysis tools
- Language server protocol (LSP) support
After successful compilation, the executable will automatically run. If you only want to compile without running:
cd /Users/mbn/Documents/GitHub/Algorithmic-Analysis-and-Optimization/Algoritmeanalyse-og-optimering && \
g++ -std=c++17 -o Algoritmeanalyse-og-optimering *.cpp \
-I. \
$(pkg-config --cflags --libs raylib) \
-lm -lpthreadThen run the executable separately:
./Algoritmeanalyse-og-optimering- "raylib not found": Ensure raylib is installed (
brew install raylib) and pkg-config can find it (pkg-config --cflags --libs raylib). - Compilation errors: Verify you have Xcode Command Line Tools installed (
xcode-select --install). - Permission denied: Make sure the executable has execute permissions (
chmod +x Algoritmeanalyse-og-optimering).
-
Quick Sort: An efficient, in-place sorting algorithm using the divide-and-conquer approach. It selects a 'pivot' element and partitions the array around the pivot. While it has an average-case time complexity of O(n log n), the worst-case performance is O(n²).
For further reading, refer to:
-
Merge Sort: A stable, comparison-based sorting algorithm that consistently performs at O(n log n) time complexity for all cases. It divides the array into halves, sorts them recursively, and then merges the sorted halves.
Additional resource:
-
Binary Search: An efficient algorithm for finding an item in a sorted array with a time complexity of O(log n).
Learn more about Binary Search:
Benchmarking results and profiling data are available for both sorting and searching algorithms.
- Sorting Results: View Sorting Results
- Searching Results: View Searching Results
For detailed benchmarks, check:
In this project, we explored algorithm analysis and optimization, focusing on sorting and searching algorithms. By implementing and benchmarking Quick Sort, Merge Sort, and Binary Search, we gained insights into their performance characteristics in various scenarios. Visualizing the algorithms helped in understanding their behavior and the importance of choosing the right algorithm for a specific problem to optimize time and space complexity.