Using qsort().
#include <iostream>
#include <stdlib.h>
using namespace std;
int sortFunction( const void *intOne, const void *intTwo);
const int length = 10;
int main(void)
{
int i,intArray[length];
for (i = 0; i < length; i++)
{
cout << "Enter a number: ";
cin >> intArray[i];
}
cout << "\n";
qsort((void *)intArray, length, sizeof(intArray[0]), sortFunction);
for (i = 0; i < length; i++)
cout << "intArray [" << i << "]: " << intArray[i] << endl;
cout << "Done." << endl;
return 0;
}
int sortFunction( const void *a, const void *b)
{
int intOne = *((int*)a);
int intTwo = *((int*)b);
if (intOne < intTwo)
return -1;
if (intOne == intTwo)
return 0;
return 1;
}
Related examples in the same category