File tree Expand file tree Collapse file tree 7 files changed +73
-13
lines changed
src/main/java/com/thealgorithms/sorts Expand file tree Collapse file tree 7 files changed +73
-13
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,13 @@ class BubbleSort implements SortAlgorithm {
1010 /**
1111 * Implements generic bubble sort algorithm.
1212 *
13+ * Time Complexity:
14+ * - Best case: O(n) – array is already sorted.
15+ * - Average case: O(n^2)
16+ * - Worst case: O(n^2)
17+ *
18+ * Space Complexity: O(1) – in-place sorting.
19+ *
1320 * @param array the array to be sorted.
1421 * @param <T> the type of elements in the array.
1522 * @return the sorted array.
Original file line number Diff line number Diff line change 11package com .thealgorithms .sorts ;
22
33/**
4- * Heap Sort Algorithm Implementation
4+ * Heap Sort algorithm implementation.
5+ *
6+ * Heap sort converts the array into a max-heap and repeatedly extracts the maximum
7+ * element to sort the array in increasing order.
8+ *
9+ * Time Complexity:
10+ * - Best case: O(n log n)
11+ * - Average case: O(n log n)
12+ * - Worst case: O(n log n)
13+ *
14+ * Space Complexity: O(1) – in-place sorting
515 *
616 * @see <a href="https://en.wikipedia.org/wiki/Heapsort">Heap Sort Algorithm</a>
17+ * @see SortAlgorithm
718 */
819public class HeapSort implements SortAlgorithm {
920
Original file line number Diff line number Diff line change 11package com .thealgorithms .sorts ;
22
3+ /**
4+ * Generic Insertion Sort algorithm.
5+ *
6+ * Standard insertion sort iterates through the array and inserts each element into its
7+ * correct position in the sorted portion of the array.
8+ *
9+ * Sentinel sort is a variation that first places the minimum element at index 0 to
10+ * avoid redundant comparisons in subsequent passes.
11+ *
12+ * Time Complexity:
13+ * - Best case: O(n) – array is already sorted (sentinel sort can improve slightly)
14+ * - Average case: O(n^2)
15+ * - Worst case: O(n^2) – array is reverse sorted
16+ *
17+ * Space Complexity: O(1) – in-place sorting
18+ *
19+ * @see SortAlgorithm
20+ */
321class InsertionSort implements SortAlgorithm {
422
523 /**
Original file line number Diff line number Diff line change @@ -13,11 +13,16 @@ class MergeSort implements SortAlgorithm {
1313 private Comparable [] aux ;
1414
1515 /**
16- * Generic merge sort algorithm implements .
16+ * Generic merge sort algorithm.
1717 *
18- * @param unsorted the array which should be sorted.
19- * @param <T> Comparable class.
20- * @return sorted array.
18+ * Time Complexity:
19+ * - Best case: O(n log n)
20+ * - Average case: O(n log n)
21+ * - Worst case: O(n log n)
22+ *
23+ * Space Complexity: O(n) – requires auxiliary array for merging.
24+ *
25+ * @see SortAlgorithm
2126 */
2227 @ Override
2328 public <T extends Comparable <T >> T [] sort (T [] unsorted ) {
Original file line number Diff line number Diff line change 88class QuickSort implements SortAlgorithm {
99
1010 /**
11- * This method implements the Generic Quick Sort
11+ * Generic Quick Sort algorithm.
1212 *
13- * @param array The array to be sorted Sorts the array in increasing order
13+ * Time Complexity:
14+ * - Best case: O(n log n) – pivot splits array roughly in half each time.
15+ * - Average case: O(n log n)
16+ * - Worst case: O(n^2) – occurs when pivot consistently produces unbalanced splits.
17+ *
18+ * Space Complexity: O(log n) – recursion stack, in-place sorting.
19+ *
20+ * @see SortAlgorithm
1421 */
1522 @ Override
1623 public <T extends Comparable <T >> T [] sort (T [] array ) {
Original file line number Diff line number Diff line change 22
33public class SelectionSort implements SortAlgorithm {
44 /**
5- * Sorts an array of comparable elements in increasing order using the selection sort algorithm.
5+ * Generic Selection Sort algorithm.
66 *
7- * @param array the array to be sorted
8- * @param <T> the class of array elements
9- * @return the sorted array
7+ * Time Complexity:
8+ * - Best case: O(n^2)
9+ * - Average case: O(n^2)
10+ * - Worst case: O(n^2)
11+ *
12+ * Space Complexity: O(1) – in-place sorting.
13+ *
14+ * @see SortAlgorithm
1015 */
1116 @ Override
1217 public <T extends Comparable <T >> T [] sort (T [] array ) {
Original file line number Diff line number Diff line change 1111 * a linked list. A Directed Graph is proven to be acyclic when a DFS or Depth First Search is
1212 * performed, yielding no back-edges.
1313 *
14- * https://en.wikipedia.org/wiki/Topological_sorting
14+ * Time Complexity: O(V + E)
15+ * - V: number of vertices
16+ * - E: number of edges
1517 *
16- * @author Jonathan Taylor (https://github.com/Jtmonument)
18+ * Space Complexity: O(V + E)
19+ * - adjacency list and recursion stack in DFS
20+ *
21+ * Reference: https://en.wikipedia.org/wiki/Topological_sorting
22+ *
23+ * Author: Jonathan Taylor (https://github.com/Jtmonument)
1724 * Based on Introduction to Algorithms 3rd Edition
1825 */
1926public final class TopologicalSort {
You can’t perform that action at this time.
0 commit comments