Today began the study of the "AHA algorithm" book. Generally speaking, this book is the algorithm of the small petition, the language is very popular, the introduction of the algorithm is relatively simple, now back to see the simple things will seem to waste time? Otherwise, I recently feel that the competition is not the ultimate way, the direction of learning is also changed to book as the object ("Specific mathematics" a column of the establishment can be seen), began to gradually improve the system of professional knowledge, and slowly do some practical things.
For the sorting problem, is the basic problem that the algorithm needs to solve, but the order also has many kinds, its different method also decided its different efficiency, below we look at several common sorts method.
Bucket sort:
Give a set of examples: Sort 5, 8, 3, 1, 7 from small to large.
The bucket sort is based on a one-dimensional array, given this sort method: Initialize array a[10] = 0, then a[5]++, a[8]++, a[3]++, a[1]++, a[7]++. Then we control the Loop statement, Access A[i], and I output A[I] times, and eventually we get a small to large arrangement. And this process, as its name describes, throws the data I into an array element labeled I a[i] in this bucket.
The order of the barrels skillfully the number I as the subscript of a one-dimensional array, and with a[i] record I occurrences of the number of times, using subscript from the first to the largest arrangement of the data to complete the arrangement. Its advantages are simple and clear, but the shortcomings are also obvious, we open the array length is limited by the memory space, so with the expansion of data, the sorting will be stretched.
Let's simply implement this sort of bucket.
#include <cstdio>intMain () {inta[ One], I, J, T; for(i =0; I <=Ten; i++) A[i]=0; for(i =1; I <=5; i++) {scanf ("%d",&t); A[t]++; } for(i =0; I <=Ten; i++) for(j =1; J <= a[i];j++) printf ("%d", i); return 0;}
"Aha algorithm"--sort