Algorithm design and analysis basics-Brute Force

Source: Internet
Author: User

We need to repeat the algorithm ideas and use the book "algorithm design and analysis basics" as a teaching material. Each chapter of this book introduces an algorithm design idea. Start with the simplest one today and lay a good foundation. In the future, we will gradually explore more in-depth algorithms.

The brute force method is the simplest, most intuitive, and easiest way to understand the problem. Although it is simple, it may not work in practical applications because of efficiency, however, you still cannot ignore it. As stated by the author in the book, it is also a method to solve small-scale problems and is also the basis of more complex algorithms.

1. Select sorting
Solve the Sorting Problem with the simplest way of thinking. For arrays of n elements, we scan the array n times and select the smallest element to the correct position each time, the sorting is completed after N scans.

/* Select the sort method to sort the input array into a non-decreasing array: to be sorted array n: array size, that is, [0, n-1] */void selectionsort (INT array [], unsigned int N) {int min; For (INT I = 0; I <n-1; I ++) {min = I; for (Int J = I + 1; j <n; j ++) {If (array [J] <array [Min]) min = J;} if (I! = Min) {int temp = array [I]; array [I] = array [Min]; array [Min] = temp ;}}// selectionsort


This is the worst sorting method. It is O (n * n) time complexity for any input. However, its biggest advantage is the least number of exchanges.

Ii. Bubble Sorting

It is also a classic brute force sorting algorithm. Here I just made some improvements to the original bubble. If the algorithm has sorted the order, the algorithm will scan it once to complete the sorting.

/* The Brute Force Method-Bubble Sorting (slightly optimized) sorts the input array into a non-decreasing array: to be sorted array n: array size, that is, [0, n-1] */void bubblesort (INT array [], unsigned int N) {int I = n-1; int last; while (I> 0) {last = 0; for (Int J = 0; j <I; j ++) {If (array [J]> array [J + 1]) {int temp = array [J]; array [J] = array [J + 1]; array [J + 1] = temp; last = J; // record the location of the last exchange value} I = last ;}// bubblesort


However, in the worst case, it is still the time complexity of O (N * n.

Iii. Sequential search and string brute force matching

Sequential search is a simple search algorithm, which is an application of brute force thinking. And the string's brute force matching.

/* Brute force method-sequential query: searches for the location of a given key in the array: array to be sorted N: array size, that is, [0, n-1] K: search key */INT sequentialsearch (INT array [], unsigned int N, int K) {int I = 0; while (array [I]! = K) I ++; if (I <n) return I; elsereturn-1;} // sequentialsearch

--------------------------------------

/* Brute force method-string brute force matching text [0, n-1]: search text pattern [0 M-1]: search string returns match first position, -1 */INT bruteforcesstringmatch (char text [], unsigned int N, char pattern [], unsigned m) {Int J; For (INT I = 0; I <= N-m; I ++) {J = 0; while (j <M & pattern [J] = text [I + J]) J ++; if (j = m) return I;} return-1;} // bruteforcesstringmatch


Code for the brute force thinking used to find the "most recently correct" Problem and the backpack problem. I personally feel that this code is too meaningless and I just skipped it. And other sub-governance ideas. Okay. I used several simple algorithms to review the concept of brute force.

By: Hong gunwei

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.