Skip to content

Commit 7fd3292

Browse files
feat: (#8) Initialize stack module and define Stack interface for various implementations
1 parent 5b974ee commit 7fd3292

File tree

4 files changed

+197
-0
lines changed

4 files changed

+197
-0
lines changed

stack/.gitignore

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
!**/src/main/**/target/
4+
!**/src/test/**/target/
5+
6+
### IntelliJ IDEA ###
7+
.idea/
8+
*.iws
9+
*.iml
10+
*.ipr
11+
12+
### Eclipse ###
13+
.apt_generated
14+
.classpath
15+
.factorypath
16+
.project
17+
.settings
18+
.springBeans
19+
.sts4-cache
20+
21+
### NetBeans ###
22+
/nbproject/private/
23+
/nbbuild/
24+
/dist/
25+
/nbdist/
26+
/.nb-gradle/
27+
build/
28+
!**/src/main/**/build/
29+
!**/src/test/**/build/
30+
31+
### VS Code ###
32+
.vscode/
33+
34+
### Mac OS ###
35+
.DS_Store

stack/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.

stack/pom.xml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.brandoniscoding</groupId>
8+
<artifactId>sorts</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>22</maven.compiler.source>
13+
<maven.compiler.target>22</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>org.junit.jupiter</groupId>
20+
<artifactId>junit-jupiter-api</artifactId>
21+
<version>5.11.2</version>
22+
<scope>test</scope>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.junit.jupiter</groupId>
27+
<artifactId>junit-jupiter-engine</artifactId>
28+
<version>5.11.2</version>
29+
<scope>test</scope>
30+
</dependency>
31+
</dependencies>
32+
33+
<build>
34+
<plugins>
35+
<plugin>
36+
<groupId>org.apache.maven.plugins</groupId>
37+
<artifactId>maven-surefire-plugin</artifactId>
38+
<version>3.5.1</version>
39+
</plugin>
40+
41+
</plugins>
42+
</build>
43+
44+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.brandoniscoding.algorithms;
2+
3+
public interface StackImpl<T> {
4+
5+
// Method to push an element onto the stack
6+
void push(T value);
7+
8+
// Method to pop an element onto the stack
9+
T pop();
10+
11+
// Method to pop at the top element without remove it
12+
T peek();
13+
14+
// Method ti check if the is empty
15+
boolean isEmpty();
16+
17+
// Method to get a current size of the stack
18+
int size();
19+
20+
// Method to clear all elements from the stack
21+
void clear();
22+
23+
// Method to search for an element in the stack and return its position from the top (1-based index)
24+
int search(T element);
25+
26+
// Method to check if a specific element is present in the stack
27+
boolean contains(T element);
28+
}

0 commit comments

Comments
 (0)