This article mainly introduces, a simple sorting algorithm can have what kind of play (in order to choose for example, code please run under the CPP file)
Select Sort ... A summary of three steps is:
1. Find the maximum/minimum element within a range.
2. Swap the maximum/minimum element with the first value in the interval
3. Narrowing the search interval
What if you don't understand? Don't worry, please refer to: Choose Sort _ Baidu Encyclopedia: http://baike.baidu.com/item/Select Sort
Play 1: The first glimpse of the doorway
If you understand the algorithm of choosing sorting, and want to implement it into a piece of code, it is not a complex thing for a small partner with strong code ability, so the first realm we achieve is this:
Select Sort Simple instance (ascending) void Selectsort (int arr[],size_t sz) {for (size_t i=0; i<sz; ++i) {//Shrink to find interval int temp = arr[i];//record min/ Maximum size_t pos = i;//record the maximum/minimum value of the position for (size_t j=i+1; j<sz; ++j) {//In the (I,sz) interval to find the maximum/minimum value if (Temp>arr[j]) {temp = arr[j];/ /Update max/min pos = j;//Update max/min worth location}}arr[pos] = arr[i];//swap the maximum/minimum value in the interval with I for arr[i] = temp;}}
We can write this code, it means that our code skills have entered the realm of entry.
But this kind of code has the problem:
1. Only certain types of elements can be sorted. (This example is an integer type)
2. Only ascending or descending order can be performed. (In this case, in ascending order, if you want to implement descending, you have to write another piece of code)
Play 2: A Little Something
If you have a classmate who knows the function pointers and can skillfully apply the callback function, then you can write the code like this:
callback function, ascending control bool Asc (int left,int right) {return left>right;} callback function, descending control bool Desc (int left,int right) {return left<right;} typedef BOOL (*callback) (int,int);//Select Sort Simple instance (ascending) void Selectsort (int arr[],size_t sz,callback Compare) {for (size_t i=0; i<sz; ++i) {//shrinks the lookup interval int temp = arr[i];//record min/max size_t pos = i;//record the maximum/minimum position for (size_t j=i+1; j<sz; ++j) {////In the (I,sz) interval to find the maximum/ Minimum value if (compare (Temp,arr[j])) {//Use callback function, control l/Descending temp = arr[j];//update max/min pos = j;//Update max/min worth location}}arr[pos] = arr[i];// Swap the maximum/minimum value in the interval with I for arr[i] = temp;}}
In this case, we can control the ascending order of the selection by the callback function .
However, such code still does not have the versatility, because the integer type can only be sorted
Play 3: Presenting illegal weapons
There is a new feature in C + + that can enhance code reuse: Templates!
Of the six components of the STL, one component is called a function object.
So, the third realm, we can combine the template and the function object , so we write this code:
Ascending order Template<typename T>class asc{public:bool operator (T left,t right) {return left>right;}};/ /descending order Template<typename t>class desc{public:bool operator (T left,t right) {return left<right;}};/ /select Sort Template<typename t,typename com>void selectsort (T arr[],size_t sz) {for (size_t i=0; i<sz; ++i) {// Narrow the lookup interval int temp = arr[i];//record min/max value size_t pos = i;//record maximum/minimum position for (size_t j=i+1; j<sz; ++j) {//Find the maximum/minimum value within the (I,SZ) interval if ( Com () (Temp,arr[j])) {//Use object function, control l/Descending temp = arr[j];//update max/min pos = j;//Update max/min worth location}}arr[pos] = arr[i];//will range Max/ The minimum value is exchanged with I arr[i] = temp;}}
This code, reusability and versatility has been greatly improved!
Conclusion: It is boundless, keep on trying!
Three ways to easily sort data structures