If you look on the web you will find a whole host of different sorting algorithms, some more complicated than others.
If you plan to make one by yourself you will eventually realise the easiest sorting algo requires a 'nested for loop'. This is the sorting algorithm outlined in 'bubble sort' I think. Its easy to understand however, some algorithms are extremely difficult to understand such as the 'quick sort' algorithm.
You can find this algorithm on the net but I have written the code here for you. All you have to do is copy and paste into your programme. The quick sort is especially useful because of all the algo's its the fastest to sort elements into ascending order:
Code:
#include <stdio.h>
#include <iostream.h>
int quickSort(int numbers[],int);
int q_sort(int numbers[],int,int);
int main()
{
int array[81];
for (int a=0; a<10; a++)
{
cout<<"Enter an integer value:";
cin>>array[a];
}
// call the function
quickSort(array,10);
//print values
for (int b=0; b<10; b++)
{
cout<<array[b]<<endl;
}
int stop;
cin>>stop;
}
int quickSort(int numbers[], int array_size)
{
q_sort(numbers, 0, array_size - 1);
}
int q_sort(int numbers[], int left, int right)
{
int pivot, l_hold, r_hold;
l_hold = left;
r_hold = right;
pivot = numbers[left];
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right))
right--;
if (left != right)
{
numbers[left] = numbers[right];
left++;
}
while ((numbers[left] <= pivot) && (left < right))
left++;
if (left != right)
{
numbers[right] = numbers[left];
right--;
}
}
numbers[left] = pivot;
pivot = left;
left = l_hold;
right = r_hold;
if (left < pivot)
q_sort(numbers, left, pivot-1);
if (right > pivot)
q_sort(numbers, pivot+1, right);
}