---restore content starts---
Characteristics:
First, unlike some other strategies, we can apply brute force methods to solveBroad Fieldof various problems. (Wide application area) second, for some important problems (such as sorting, finding, matrix multiplication and string matching), brute force method can produce some reasonable algorithms, they have some practical value, and do not limit the size of the instance. Thirdly, if the number of problems to be solved is not many, and the brute force method can be solved with an acceptable speed, then the cost of designing a more efficient algorithm is probably not worth it. (The cost is relatively small) IV, even if the efficiency is usually very low, can still use brute force method to solve some small-scale problem cases. (for small-scale) V, a brute force algorithm can beResearch or teachingPurpose of service.
Example of brute force method
One
Basic idea: Find the smallest (maximum) number in the unsorted number and the first (last) interchange in these numbers.
Complexity of Time: O (n^2)
Instance code:
#include <iostream.h>voidSelectsort (int* PData,intCount) { intiTemp; intIPos; for(intI=0; i<count-1; i++) {iTemp=Pdata[i]; IPos=i; for(intj=i+1; j<count;j++) { if(pdata[j]<iTemp) {ITemp=Pdata[j]; IPos=J; }} Pdata[ipos]=Pdata[i]; Pdata[i]=iTemp; }} voidMain () {intData[] = {Ten,9,8,7,6,5,4}; Selectsort (data,7); for(intI=0;i<7; i++) cout<<data[i]<<" "; cout<<"\ n";}
Two, bubble sort
The basic idea: the larger number and the small number are exchanged by the adjacent comparison, and finally the maximum (minimum) number is reached at the bottom (upper)
Instance code:
1#include <iostream.h>2 3 voidBubblesort (int* PData,intCount)4 {5 intiTemp;6 for(intI=1; i<count;i++)7 {8 for(intj=count-1; j>=i;j--)9 {Ten if(pdata[j]<pdata[j-1]) One { AITemp = pdata[j-1]; -pdata[j-1] =Pdata[j]; -PDATA[J] =iTemp; the } - } - } - } + - voidMain () + { A intData[] = {Ten,9,8,7,6,5,4}; atBubblesort (data,7); - for(intI=0;i<7; i++) -cout<<data[i]<<" "; -cout<<"\ n"; -}
---restore content ends---
Algorithm-Brute Force method