mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-12-19 07:00:35 +08:00
Add Big-O time and space complexity comments for sorting algorithms (#7155)
Add time and space complexity comments for specific sorting algorithms Co-authored-by: Deniz Altunkapan <deniz.altunkapan@outlook.com>
This commit is contained in:
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
@@ -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 {
|
||||
|
||||
/**
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -8,9 +8,16 @@ package com.thealgorithms.sorts;
|
||||
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) {
|
||||
|
||||
@@ -2,11 +2,16 @@ package com.thealgorithms.sorts;
|
||||
|
||||
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) {
|
||||
|
||||
@@ -11,9 +11,16 @@ import java.util.LinkedList;
|
||||
* 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 {
|
||||
|
||||
Reference in New Issue
Block a user