|
| 1 | +# Sorting Algorithms |
| 2 | + |
| 3 | +This project demonstrates the implementation of multiple sorting algorithms. Sorting algorithms are essential in computer science for ordering data, making it easier to analyze, search, or optimize resources. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +Sorting algorithms take a collection of items (often numbers or strings) and arrange them in a specific order. In this project, each algorithm is implemented in a generic way to allow for sorting different types of data, provided that they implement the `Comparable` interface. |
| 8 | + |
| 9 | +The key sorting algorithms included are: |
| 10 | + |
| 11 | +- **Bubble Sort** |
| 12 | +- **Selection Sort** |
| 13 | +- **Insertion Sort** |
| 14 | +- **Merge Sort** |
| 15 | +- **Quick Sort** |
| 16 | + |
| 17 | +Each algorithm is analyzed in terms of its: |
| 18 | +- **Time Complexity**: How fast it performs relative to the input size. |
| 19 | +- **Space Complexity**: The amount of additional memory it uses. |
| 20 | +- **Stability**: Whether it preserves the order of equal elements. |
| 21 | + |
| 22 | +## Algorithms |
| 23 | + |
| 24 | +### 1. Bubble Sort |
| 25 | +A simple, comparison-based algorithm that repeatedly steps through the list, compares adjacent items, and swaps them if they are out of order. |
| 26 | + |
| 27 | +- **Time Complexity**: O(n^2) in the worst and average cases, O(n) in the best case when the array is already sorted. |
| 28 | +- **Space Complexity**: O(1) |
| 29 | +- **Stability**: Stable |
| 30 | + |
| 31 | +### 2. Selection Sort |
| 32 | +Selects the minimum element from the unsorted portion of the list and swaps it with the first unsorted element. |
| 33 | + |
| 34 | +- **Time Complexity**: O(n^2) for all cases. |
| 35 | +- **Space Complexity**: O(1) |
| 36 | +- **Stability**: Not stable (may require modifications for stability) |
| 37 | + |
| 38 | +### 3. Insertion Sort |
| 39 | +Builds the final sorted list one element at a time by comparing each new element to the sorted elements and inserting it in the correct position. |
| 40 | + |
| 41 | +- **Time Complexity**: O(n^2) in the worst and average cases, O(n) in the best case. |
| 42 | +- **Space Complexity**: O(1) |
| 43 | +- **Stability**: Stable |
| 44 | + |
| 45 | +### 4. Merge Sort |
| 46 | +A divide-and-conquer algorithm that divides the list into halves, sorts each half, and then merges the two sorted halves together. |
| 47 | + |
| 48 | +- **Time Complexity**: O(n log n) for all cases. |
| 49 | +- **Space Complexity**: O(n) due to the need for temporary arrays. |
| 50 | +- **Stability**: Stable |
| 51 | + |
| 52 | +### 5. Quick Sort |
| 53 | +A highly efficient divide-and-conquer algorithm that selects a "pivot" element, partitions the list around the pivot, and then recursively sorts the partitions. |
| 54 | + |
| 55 | +- **Time Complexity**: O(n log n) on average, O(n^2) in the worst case (can be optimized with random pivots). |
| 56 | +- **Space Complexity**: O(log n) on average due to recursion, O(n) in the worst case. |
| 57 | +- **Stability**: Not stable |
| 58 | + |
| 59 | +## Running the Project |
| 60 | + |
| 61 | +1. **Compile the Code**: Make sure you have Java and Maven installed, and then compile the code using Maven with the following command: |
| 62 | + |
| 63 | + ```bash |
| 64 | + mvn compile |
| 65 | + ``` |
| 66 | + |
| 67 | +2. **Run the Sorting Algorithms**: To run the main program, use Maven with the following command: |
| 68 | + |
| 69 | + ```bash |
| 70 | + mvn exec:java -Dexec.mainClass="com.brandoniscoding.Main" |
| 71 | + ``` |
| 72 | + |
| 73 | +3. **Run the Tests**: To execute the unit tests for the sorting algorithms, run the following Maven command: |
| 74 | + |
| 75 | + ```bash |
| 76 | + mvn test |
| 77 | + ``` |
| 78 | + |
| 79 | + This will execute all the tests defined in the project and show the results in the terminal. |
| 80 | + |
| 81 | + |
| 82 | +## Contributing |
| 83 | + |
| 84 | +1. **Fork the Repository**: Make your changes in a branch from `master`. |
| 85 | +2. **Commit Messages**: Follow conventions, e.g., `feat: add new sorting algorithm` or `fix: optimize quicksort partition`. |
| 86 | +3. **Pull Requests**: Ensure your code passes all tests before opening a pull request. |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
0 commit comments