Sorting 100 unique numbers by using 40bytes of memory
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Sorting 100 unique numbers using just 40 bytes of memory is a challenge that requires efficient use of space and clever algorithm design. This article will focus on how such a task can be accomplished, leveraging bit manipulation techniques and low-memory data structures, with the primary goal of understanding the constraints and exploring potential solutions.
Problem Analysis and Constraints
Given the constraints of only 40 bytes (320 bits) to sort 100 unique numbers, traditional sorting algorithms such as QuickSort or MergeSort are impractical due to their need for significant auxiliary space. The constraints force us to explore approaches that minimize memory usage, potentially at the cost of increased computational complexity or use of non-traditional data structures.
Key Considerations
- Range and Representation of Numbers: If the range of numbers spans from 1 to 100, we can leverage data structures based on bit manipulation. Each bit can represent the presence (1) or absence (0) of a number in this range.
- Memory Layout: With 320 bits available, we need to efficiently represent the existence of numbers in this limited space.
- I/O limitations: Without sufficient space to store all numbers simultaneously, input and output operations will become critical and may impact overall performance.
Approach: Bitwise Sieve Method
The concept of a "bit-sieve" can be applied here. This approach is inspired by the Sieve of Eratosthenes, usually used for finding prime numbers, but adapted for representing the presence of numbers.
Step-by-Step Implementation
- Define the Bit Array: Use an array of 4 integers (32-bit each), which totals 128 bits. Each bit in this array will represent a number from 1 to 100.
- Space Complexity: The method uses 128 bits for the bit array, which is well within the 320-bit limit.
- Time Complexity: Both insertion and output operations run in constant time per item, making the method efficient.
- Advanced Compression: Explore techniques like run-length encoding or adaptive Huffman coding to handle data in case additional range constraints or properties are known.
Related reading
- Sorting 10GB Data in 1 GB memory. How will I do it?
- Sorting 1 million 8-decimal-digit numbers with 1 MB of RAM
- Sorting 1 trillion integers
- sorting a doubly linked list with merge sort
- Sorting a linked list- why not?
- Sorting a list with stream.sorted in Java
- Sorting a permutation with minimum cost
- Sorting a point array efficiently in C?

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.