In C, you can use the built-in qsort command:

int compare( const void* a, const void* b)
{
     int int_a = * ( (int*) a );
     int int_b = * ( (int*) b );
     
     if ( int_a == int_b ) return 0;
     else if ( int_a < int_b ) return -1;
     else return 1;
}

qsort( a, 6, sizeof(int), compare )

See: https://cplusplus.com/reference/cstdlib/qsort/


To answer the second part of your question: an optimal (comparison based) sorting algorithm is one that runs with O(n log(n)) comparisons. There are several that have this property (including quick sort, merge sort, heap sort, etc.), but which one to use depends on your use case.

As a side note, you can sometime do better than O(n log(n)) if you know something about your data โ€” see the Wikipedia article on Radix Sort.

Answer from Alex Reece on Stack Overflow
Top answer
1 of 5
71

In C, you can use the built-in qsort command:

int compare( const void* a, const void* b)
{
     int int_a = * ( (int*) a );
     int int_b = * ( (int*) b );
     
     if ( int_a == int_b ) return 0;
     else if ( int_a < int_b ) return -1;
     else return 1;
}

qsort( a, 6, sizeof(int), compare )

See: https://cplusplus.com/reference/cstdlib/qsort/


To answer the second part of your question: an optimal (comparison based) sorting algorithm is one that runs with O(n log(n)) comparisons. There are several that have this property (including quick sort, merge sort, heap sort, etc.), but which one to use depends on your use case.

As a side note, you can sometime do better than O(n log(n)) if you know something about your data โ€” see the Wikipedia article on Radix Sort.

2 of 5
14

In your particular case the fastest sort is probably the one described in this answer. It is exactly optimized for an array of 6 ints and uses sorting networks. It is 20 times (measured on x86) faster than library qsort. Sorting networks are optimal for sort of fixed length arrays. As they are a fixed sequence of instructions they can even be implemented easily by hardware.

Generally speaking there is many sorting algorithms optimized for some specialized case. The general purpose algorithms like heap sort or quick sort are optimized for in place sorting of an array of items. They yield a complexity of O(n.log(n)), n being the number of items to sort.

The library function qsort() is very well coded and efficient in terms of complexity, but uses a call to some comparizon function provided by user, and this call has a quite high cost.

For sorting very large amount of datas algorithms have also to take care of swapping of data to and from disk, this is the kind of sorts implemented in databases and your best bet if you have such needs is to put datas in some database and use the built in sort.

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ c-program-to-sort-an-array-in-ascending-order
C Program to Sort an Array in Ascending Order - GeeksforGeeks
July 12, 2025 - The easiest way to sort an array in C is by using qsort() function. This function needs a comparator to know how to compare the values of the array.
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ dotnet โ€บ api โ€บ system.array.sort
Array.Sort Method (System) | Microsoft Learn
The example does this by defining a custom IComparer implementation that reverses the default sort order and performs case-insensitive string comparison. This method uses the introspective sort (introsort) algorithm as follows: If the partition size is less than or equal to 16 elements, it uses an insertion sort algorithm. If the number of partitions exceeds 2 * LogN, where N is the range of the input array, it uses a Heapsort algorithm.
๐ŸŒ
Simon Fraser University
www2.cs.sfu.ca โ€บ CourseCentral โ€บ 125 โ€บ tjd โ€บ basic_sorting_C.html
Sorting and Searching (in C) โ€” 125 Summer 2020 1 documentation
For simplicity, in these notes weโ€™ll stick to sorting arrays of ints. ... Notice that finding that min and max value of a sorted list is easy: the first value is the min, and the last value is the max. Hundreds of different sorting algorithms have been created.
๐ŸŒ
MDN Web Docs
developer.mozilla.org โ€บ en-US โ€บ docs โ€บ Web โ€บ JavaScript โ€บ Reference โ€บ Global_Objects โ€บ Array โ€บ sort
Array.prototype.sort() - JavaScript | MDN
Zero or NaN indicates that a and b are considered equal. To memorize this, remember that (a, b) => a - b sorts numbers in ascending order. If omitted, the array elements are converted to strings, then sorted according to each character's Unicode code point value.
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ c program โ€บ sorting of array in c
Sorting of Array in C | PrepInsta
September 17, 2025 - ... Take the size of the array from the user. Declare an array of given input size. Take the input of all elements of the array. Now run a for loop from 0 to size-1. And for every element check it from all the next elements to it.
Find elsewhere
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ c-program-to-sort-an-array-in-an-ascending-order
C program to sort an array in an ascending order
March 15, 2026 - #include <stdio.h> int main() { int num[20]; int i, j, temp, n; printf("Enter number of elements in an array: "); scanf("%d", &n); printf("Enter the elements:<br>"); for (i = 0; i < n; i++) { scanf("%d", &num[i]); } // Bubble sort algorithm for (i = 0; i < n; i++) { for (j = i + 1; j < n; j++) { if (num[i] > num[j]) { // Swap elements temp = num[i]; num[i] = num[j]; num[j] = temp; } } } printf("The numbers in ascending order:<br>"); for (i = 0; i < n; i++) { printf("%d ", num[i]); } printf("<br>"); return 0; } Enter number of elements in an array: 5 Enter the elements: 64 34 25 12 22 The numbers in ascending order: 12 22 25 34 64
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to sort in c language
r/learnprogramming on Reddit: How to sort in C language
November 24, 2023 -

basically i'm a freshman with no background with programming so i'm really struggling. my professor gave us our final project which is only asking the user for a time inputs for their activities. and i'm having a hard time sorting these hours (ex. 2hrs and 5mins) for each activity especially when there are 10 activities needed. all i can think of using is if-else statements since we are still onto the basic ones (can't use arrays and other complicated stuff) but using if-else is too long and inefficient and idk if for-loops can be applied. any advice?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ qsort-function-in-c
qsort() Function in C - GeeksforGeeks
August 11, 2026 - The qsort() function in C is a standard library function used to sort the elements of an array.
๐ŸŒ
Sanfoundry
sanfoundry.com โ€บ c-program-sort-array-ascending-order
C Program to Sort an Array in Ascending Order - Sanfoundry
September 22, 2022 - Iterating through for loops (from [0 to N) ), take integers as input from user and print them. These input are the elements of the array. 4. Now, create a nested for loop with i and j as iterators. 5. Start the sorting in ascending order by extracting each element at position i of outer loop.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_array_sort.asp
NumPy Sorting Arrays
ufunc Intro ufunc Create Function ufunc Simple Arithmetic ufunc Rounding Decimals ufunc Logs ufunc Summations ufunc Products ufunc Differences ufunc Finding LCM ufunc Finding GCD ufunc Trigonometric ufunc Hyperbolic ufunc Set Operations ... Sorting means putting elements in an ordered sequence.
๐ŸŒ
Microsoft Support
support.microsoft.com โ€บ en-us โ€บ office โ€บ vlookup-function-0bbc8083-26fe-4963-8ab8-93a18ad188a1
VLOOKUP function | Microsoft Support
See Get data insights with Copilot in Excel. Use the VLOOKUP function to look up a value in a table. Syntax ยท VLOOKUP (lookup_value, table_array, col_index_num, [range_lookup]) For example: =VLOOKUP(A2,A10:C20,2,TRUE) =VLOOKUP("Fontana",B2:E7,2,FALSE) =VLOOKUP(A2,'Client Details'!A:F,3,FALSE) There are four pieces of information that you will need in order to build the VLOOKUP syntax: The value you want to look up, also called the lookup value.
๐ŸŒ
Python
docs.python.org โ€บ 3 โ€บ builtins โ€บ functions.html
Built-in functions โ€” Python 3.15.0 documentation
The key argument specifies a one-argument ordering function like that used for list.sort(). The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError is raised. If multiple items are minimal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc)[0] and heapq.nsmallest(1, iterable, key=keyfunc).
๐ŸŒ
Rebus Community
press.rebus.community โ€บ programmingfundamentals โ€บ chapter โ€บ sorting-arrays
Sorting Arrays โ€“ Programming Fundamentals
December 15, 2018 - The most frequently used orders are numerical order and lexicographical order.[1] Most current programming languages include built-in or standard library functions for sorting arrays.
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ howto โ€บ sorting.html
Sorting Techniques โ€” Python 3.15.0 documentation
The original items do not have to be comparable because the ordering of the decorated tuples will be determined by at most the first two items. So for example the original list could contain complex numbers which cannot be sorted directly.
๐ŸŒ
Learn Java
javatutoring.com โ€บ c-program-sort-array-elements-in-ascending-order
C Program To Sort Array Elements In Ascending Order | 4 Ways
August 24, 2026 - The main() calls the sort() to sort the array elements in ascending order by passing array a[], array size as arguments.
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to sort array in ascending or descending order
C program to sort array in ascending or descending order - Codeforwin
July 20, 2025 - To select each element from array, run an outer loop from 0 to size - 1. The loop structure must look like for(i=0; i<size; i++). Run another inner loop from i + 1 to size - 1 to place currently selected element at its correct position.
๐ŸŒ
Pressbooks
harpercollege.pressbooks.pub โ€บ programmingfundamentals โ€บ chapter โ€บ sorting-arrays
Sorting Arrays โ€“ Programming Fundamentals
December 15, 2018 - The most frequently used orders are numerical order and lexicographical order.[1] Most current programming languages include built-in or standard library functions for sorting arrays.
๐ŸŒ
C# Corner
c-sharpcorner.com โ€บ home โ€บ technologies โ€บ c# โ€บ how to sort array in c#
How To Sort Array In C#
March 2, 2022 - To sort an array in descending order, we can use Sort.Reverse method. This method also takes an array as an input and sorts its elements in descending order. The following code snippet reverses an array. ... Letโ€™s learn how to sort an array of int in C#.