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.
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.
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.
How can I sort an array in C while keeping track of the indices?
How to sort in C language
At PSET 3, confused about how C works
Your example is just like saying
int arr[3];which declares an array that is 3 ints long. The compiler will allocate 3 ints worth of space on the stack to hold them. You might want to review the arrays short if this is unclear.The function declaration for sort is
void sort(int values[], int n)When you callsort(haystack,size)haystackis the location in memory of array of numbers, so when you sort the values stored there, they are sorted in the original array. This will become clearer in Week 4 of the course, when you learn about pointers. For now, just know that the original haystack will be sorted.
pset3 - find.c - How do I swap two values in an array?
To see what is happening in cases like this it may help to try a simple test case and work out each iteration on paper. The highest number that i will go to in your loop is the last index in the array. On this final iteration of the loop you will attempt to access a point beyond the end of the array, i.e., values[i + 1]. I would recommend watching the Section for Week 3 to get a good idea of how to perform a sort.
int values[4];More on reddit.com
for (int i = 0; i < 4; i++)
{
// last iteration attempts to access values[4] but values[3] is the last position in the array.
values[3 + 1] = ...
}
Letโs say I want to sort the array in descending order, but I still want to know each index where landed. How can I manage to do that?
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?