Sorting an array in C?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
In C, the usual answer to “how do I sort an array” is to use the standard library function qsort. It already handles arbitrary element types as long as you provide the array length, element size, and a correct comparison function. The real skill is not memorizing qsort. It is writing a comparator that is correct, safe, and appropriate for the data being sorted.
Use qsort for Ordinary Array Sorting
For an integer array, qsort looks like this.
This is the right default because it is portable, tested, and already part of the C standard library.
Write the Comparator Carefully
The comparator is where many bugs happen. A common beginner implementation returns left - right. That looks compact, but it can overflow if the integers are large.
The safer pattern is:
It still returns negative, zero, or positive values as qsort expects, but it avoids subtraction-based overflow.
A comparator also has to be consistent. If it says a < b and b < c, it must not later imply c < a. Inconsistent comparators can make sorting behavior undefined.
Descending Order Is Just a Different Comparator
Sorting descending is not a different API. It is a different ordering rule.
That small change is enough to reverse the sort order.
Sorting Structs Is Where Comparator Design Matters More
Real programs often sort arrays of structs rather than raw numbers. In that case, your comparator usually expresses a primary sort key and then a tie-breaker.
This is often the difference between a toy example and production-quality sorting logic.
Custom Sorting Algorithms Are Usually the Exception
It is possible to write your own insertion sort, merge sort, or quicksort. Sometimes that is useful for learning or for a very specialized requirement such as guaranteed stability. But for most application code, replacing qsort with hand-written sorting before measuring a real problem is unnecessary risk.
If you do need a custom sort, write it because you have a clear requirement, not because using the standard library feels too simple.
Understand What qsort Does Not Guarantee
qsort does not promise stability. If two elements compare equal, their relative order may change. If your program depends on equal elements staying in original order, you need a stable algorithm or a comparator that includes a tie-breaker field.
That is a subtle but important design point. Sorting correctness is not only about whether the final array is ordered. It is also about which ordering properties the application actually relies on.
Common Pitfalls
- Returning
left - rightfrom the comparator and risking overflow. - Passing the wrong element size to
qsort. - Writing an inconsistent comparator.
- Assuming
qsortis stable when equal elements need predictable order. - Replacing
qsortwith a custom algorithm before confirming that sorting is actually the bottleneck.
Summary
- In C,
qsortis the standard first choice for sorting arrays. - A safe comparator matters more than clever sorting code.
- Use explicit comparator logic for ascending, descending, and struct-based sorting.
- Add tie-breakers when application logic needs deterministic ordering among equal values.
- Write a custom sort only when you have a real requirement that
qsortdoes not satisfy.
Related reading
- Sorting an array in minimum cost
- Sorting an Array in Random Order
- Sorting an Array in TensorFlow
- Sorting an array of filenames containing strings with numbers
- Sorting an array of objects by property values
- Sorting an array with minimal number of comparisons
- Sorting zipped locked containers in C using boost or the STL
- Source code for Xiaolin Wu's line algorithm 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 courseTrack 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.