sorting algorithms
linear time
computer science
counting sort
algorithm efficiency

Sorting in linear time?

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Sorting is a fundamental operation in computer science, helping to organize data in a specific order. While the basic comparison-based sorting algorithms like Quick Sort, Merge Sort, and Heap Sort run in O(nlogn)O(n \log n) time, certain specialized algorithms can achieve linear time complexity, i.e., O(n)O(n). Linear time sorting algorithms are generally applicable in scenarios where the range of the data is limited. This article explores these linear time sorting algorithms, detailing their operation, use cases, and advantages.

Linear Time Sorting Algorithms

Counting Sort

Counting Sort is a non-comparative sorting algorithm suitable for sorting integers when the range of possible values is known. The process involves:

  1. Count the Occurrences: Create an array to count how many instances of each integer exist in the input array.
  2. Accumulate Counts: Transform the count array so that each element at index `i` contains the sum of previous counts. This step helps in placing elements directly into their final position.
  3. Place Elements: Use the count array to place elements in the correct position of the output array.

Example

For an array [4, 2, 2, 8, 3, 3, 1], here is how it would work:

  • Create a count array for the range of numbers in the original array.
  • Accumulate the counts to determine positions.
  • Construct the sorted array using the count array.

Radial Sort

Radix Sort is effective for sorting numbers and can also be extended to sort other data types like strings by processing individual digits or characters. It uses a stable sorting algorithm like Counting Sort as a subroutine to sort based on individual digit positions, starting from the least significant digit to the most significant.

Radix Sort Process:

  1. Find the Maximum Number: Determine the maximum number to know the number of digits.
  2. Sort Digit by Digit: Starting with the least significant digit, sort the array using Counting Sort.
  3. Repeat: Move to the next significant digit and repeat the sort until all digits are processed.

Example

Consider [170, 45, 75, 90, 802, 24, 2, 66]:

  1. Sort the list based on the least significant digit.
  2. Repeat the sorting process, moving to the next significant digit.
  3. Continue until the most significant digit is sorted.

Bucket Sort

Bucket Sort divides an array into a finite number of buckets, distributing the elements of the array into these buckets. Each bucket is then sorted individually, usually using a different sorting algorithm. This method is efficient when input is uniformly distributed.

Bucket Sort Process:

  1. Create Buckets: Distribute elements into different buckets.
  2. Sort Individual Buckets: Sort elements in each bucket individually.
  3. Concatenate Buckets: Merge sorted buckets back into a single sorted array.

Example

For an input array of decimals between 0 and 1, create buckets, assign numbers to these buckets, sort each bucket, and then concatenate the contents of all the buckets.

Summary Table

AlgorithmSuitable InputSpace ComplexityStabilityAdditional Notes
Counting SortSmall range of integersO(k+n)O(k + n)StableNot suitable for negative numbers or large ranges
Radix SortNumbers of fixed length, can be adapted to stringsO(n+k)O(n + k)StableCan be slower if the range is very large
Bucket SortUniformly distributed real numbersO(n)O(n)StableBest for inputs more uniformly distributed

Advantages and Disadvantages of Linear Time Sorting

Advantages

  • Efficiency: Greater efficiency than comparison-based sorts for certain inputs.
  • Simplicity: Concepts are straightforward and easy to implement where applicable.
  • Stability: Many linear sorts, like Radix, maintain stability, which is useful for multikey sorts.

Disadvantages

  • Limited Use Cases: Only practical when data meets specific constraints (e.g., range is fixed or small).
  • Extra Space: Some linear time sorting algorithms like Counting Sort require additional space.
  • Implementation Complexity: While simpler in theory, certain aspects like managing buckets or counts can become complex in practice.

Conclusion

Sorting in linear time presents powerful techniques for specific data types or ranges, proving particularly meaningful in performance-sensitive applications. However, the reliance on particular conditions and extra space requirements means they aren't universally applicable. Understanding these constraints and capabilities is crucial for leveraging these algorithms correctly and effectively within suitable contexts.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.