Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/com/thealgorithms/sorts/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ class BubbleSort implements SortAlgorithm {
/**
* Implements generic bubble sort algorithm.
*
* Time Complexity:
* - Best case: O(n) – array is already sorted.
* - Average case: O(n^2)
* - Worst case: O(n^2)
*
* Space Complexity: O(1) – in-place sorting.
*
* @param array the array to be sorted.
* @param <T> the type of elements in the array.
* @return the sorted array.
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/com/thealgorithms/sorts/HeapSort.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
package com.thealgorithms.sorts;

/**
* Heap Sort Algorithm Implementation
* Heap Sort algorithm implementation.
*
* Heap sort converts the array into a max-heap and repeatedly extracts the maximum
* element to sort the array in increasing order.
*
* Time Complexity:
* - Best case: O(n log n)
* - Average case: O(n log n)
* - Worst case: O(n log n)
*
* Space Complexity: O(1) – in-place sorting
*
* @see <a href="https://en.wikipedia.org/wiki/Heapsort">Heap Sort Algorithm</a>
* @see SortAlgorithm
*/
public class HeapSort implements SortAlgorithm {

Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/thealgorithms/sorts/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
package com.thealgorithms.sorts;

/**
* Generic Insertion Sort algorithm.
*
* Standard insertion sort iterates through the array and inserts each element into its
* correct position in the sorted portion of the array.
*
* Sentinel sort is a variation that first places the minimum element at index 0 to
* avoid redundant comparisons in subsequent passes.
*
* Time Complexity:
* - Best case: O(n) – array is already sorted (sentinel sort can improve slightly)
* - Average case: O(n^2)
* - Worst case: O(n^2) – array is reverse sorted
*
* Space Complexity: O(1) – in-place sorting
*
* @see SortAlgorithm
*/
class InsertionSort implements SortAlgorithm {

/**
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/thealgorithms/sorts/MergeSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ class MergeSort implements SortAlgorithm {
private Comparable[] aux;

/**
* Generic merge sort algorithm implements.
* Generic merge sort algorithm.
*
* @param unsorted the array which should be sorted.
* @param <T> Comparable class.
* @return sorted array.
* Time Complexity:
* - Best case: O(n log n)
* - Average case: O(n log n)
* - Worst case: O(n log n)
*
* Space Complexity: O(n) – requires auxiliary array for merging.
*
* @see SortAlgorithm
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] unsorted) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/thealgorithms/sorts/QuickSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@
class QuickSort implements SortAlgorithm {

/**
* This method implements the Generic Quick Sort
* Generic Quick Sort algorithm.
*
* @param array The array to be sorted Sorts the array in increasing order
* Time Complexity:
* - Best case: O(n log n) – pivot splits array roughly in half each time.
* - Average case: O(n log n)
* - Worst case: O(n^2) – occurs when pivot consistently produces unbalanced splits.
*
* Space Complexity: O(log n) – recursion stack, in-place sorting.
*
* @see SortAlgorithm
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/java/com/thealgorithms/sorts/SelectionSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

public class SelectionSort implements SortAlgorithm {
/**
* Sorts an array of comparable elements in increasing order using the selection sort algorithm.
* Generic Selection Sort algorithm.
*
* @param array the array to be sorted
* @param <T> the class of array elements
* @return the sorted array
* Time Complexity:
* - Best case: O(n^2)
* - Average case: O(n^2)
* - Worst case: O(n^2)
*
* Space Complexity: O(1) – in-place sorting.
*
* @see SortAlgorithm
*/
@Override
public <T extends Comparable<T>> T[] sort(T[] array) {
Expand Down
11 changes: 9 additions & 2 deletions src/main/java/com/thealgorithms/sorts/TopologicalSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@
* a linked list. A Directed Graph is proven to be acyclic when a DFS or Depth First Search is
* performed, yielding no back-edges.
*
* https://en.wikipedia.org/wiki/Topological_sorting
* Time Complexity: O(V + E)
* - V: number of vertices
* - E: number of edges
*
* @author Jonathan Taylor (https://github.com/Jtmonument)
* Space Complexity: O(V + E)
* - adjacency list and recursion stack in DFS
*
* Reference: https://en.wikipedia.org/wiki/Topological_sorting
*
* Author: Jonathan Taylor (https://github.com/Jtmonument)
* Based on Introduction to Algorithms 3rd Edition
*/
public final class TopologicalSort {
Expand Down