Algorithm-Brute Force method

Source: Internet
Author: User

---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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.