We've learned a lot about sorting methods, bubble sort, hill sort, quick sort ... What I'm trying to say today is a sort of the fastest and easiest to apply--the bucket sort.
When you randomly enter a few numbers, order from small to large (from big to small), you will have a way.
This is just a one-dimensional array. Apply for a one-dimensional array of length n. is equivalent to the definition of n variables arr[0]~arr[n-1]; Initially initialized to 0, indicating that nothing is using it.
Down we add 1 to the value of the array of the following table for a number corresponding to the array. namely arr[0]=0,arr[1]=3,arr[5]=5......arr[n]=7; That corresponds to the number of occurrences of its subscript (a number appeared several times);
We call this method the bucket sort;
A bit like a hash table, but selects simple.
This algorithm, like having n buckets, numbered from 0~n-1, did not appear a number, just put an apple in the same place, the last number of apples in each bucket-the number of occurrences of each number.
Here is my Code implementation: for reference only
int main ()
{
int arr[11]={0};
int i=0;
int j=0;
int t;
for (; i<11;i++) //initializes the array to 0
arr[i]=0;
for (i=1;i<6;i++)
{
scanf ("%d", &t); Loop input 5 number
arr[t]++; Save 5 entries in the corresponding array and record the number of
} for
(i=0;i<11;i++)
{for
(j=1;j<=arr[i];j++) // Each element of the output array is
printf ("%d", I); The output of each array represents how much it is stored;
}
printf ("\ n");
System ("pause");
return 0;
}
You can extend .... hahaha