Time and Space Complexity of Selection Sort

Video Tutorial
FREE
Selection Sort Code thumbnail
This video belongs to
Java DSA Course - Master the Fundamentals and Beyond
12 modules
Certificate
Topics Covered

Time complexity measures the execution time of an algorithm's instructions. For the selection sort, the average, worst-case, and best-case time complexity of the selection sort are all O(N2)O(N^2), and the space complexity is O(1), indicating it requires a constant amount of extra storage space.

Time Complexity of Selection Sort

The Time complexity of Selection Sort is calculated based on the number of comparisons and swaps performed during the sorting process. Let's break down the calculation for better understanding:

StepDescriptionCount of Operations
InitializationStart the sort operation and initialize the loop variables.Constant, negligible in large N
Outer Loop (Iterations)Runs for each array element to find the minimum/maximum for sorting.N iterations
Inner Loop (Comparisons)For each outer loop iteration, the inner loop compares elements to find the minimum/maximum.((N-1) + (N-2) + .... + 1 = (N*(N-1))/2 comparisons
SwapAfter each outer loop iteration, swap the minimum/maximum element found with the current position.(N-1) swaps (maximum)
  • Best Case: Even in the best case where the array is already sorted, the algorithm still performs (N*(N-1))/2 comparisons to maintain the order. Because of this, the time complexity of selection sort in the best case is O(N2).
  • Average Case: On average, every iteration of the inner loop makes about half the maximum number of comparisons, still resulting in O(N^2) complexity.
  • Worst Case: In the worst case, the inner loop performs the maximum number of comparisons, yielding a time complexity of O(N^2).

Space Complexity of Selection Sort

The space complexity of Selection Sort is O(1). This indicates that the algorithm requires constant additional space irrespective of the input size.

Explore Scaler Topics Data Structures Tutorial and enhance your DSA skills with Reading Tracks and Challenges.

Which of the following is a characteristic of the selection sort algorithm?

A) It is a divide-and-conquer algorithm. B) It always performs O(N^2) comparisons, regardless of input order. C) It requires additional memory proportional to the input size. D) It is faster than quicksort for large datasets. Answer: B) It always performs O(N^2) comparisons, regardless of input order.

Conclusion

  • Central to understanding its efficiency, what is the time complexity of selection sort? Reveals a consistent O(N^2) time complexity across best, average, and worst-case.
  • The algorithm divides the array into sorted and unsorted sub-arrays, methodically selecting the minimum element from the unsorted section and placing it at the start.
  • Despite the array's initial state, selection sort performs a quadratic number of comparisons to ensure sorting, making its efficiency predictable.
  • It is distinguished by its O(1) space complexity, requiring minimal additional memory. For tracking purposes, it is limited to a few variables.
  • Selection sort's simplicity and fixed space usage make it suitable for datasets with constrained memory despite its quadratic time complexity for larger arrays.