Data Structure BASICS (2): Data Structure Basics

Source: Internet
Author: User

Data Structure BASICS (2): Data Structure Basics
Sequential search

Applicability:

No data sequence for sorting

Disadvantages:

The speed is very slow and the efficiency is O (N)

// Implement template <typename Type> Type * sequenceSearch (Type * begin, Type * end, const Type & searchValue) throw (std: range_error) {if (begin = end) | (begin = NULL) | (end = NULL) throw std: range_error ("pointer unavailable "); for (Type * index = begin; index <end; ++ index) {if (* index = searchValue) return index;} return end ;} template <typename Type> Type * sequenceSearch (Type * array, int length, const Type & searchValue) throw (std: range_error) {return sequenceSearch (array, array + length, searchValue );}

Iterative Binary Search

Application Scope:

Data must be sorted first before binary search can be applied. The efficiency is (logN)

Algorithm idea:

For example, if the Array {1, 2, 3, 4, 5, 6, 7, 8, 9} and search element 6 are executed using the binary search algorithm, the order is:

1. the first step is to find the intermediate element, that is, 5. Because 5 <6, 6 must be included in the array element after 5, so it is searched in {6, 7, 8, 9,

2. Search for the median of {6, 7, 8, 9}, which is 7> 6. Then 6 should be in the array element on the left of 7, and only 6 is left.

The binary search algorithm is to continuously split the array into half, and compare the intermediate element with the target element each time.

// Implementation: iteration binary template <typename Type> Type * binarySearch (Type * begin, Type * end, const Type & searchValue) throw (std: range_error) {if (begin = end) | (begin = NULL) | (end = NULL) throw std: range_error ("pointer unavailable "); /** Note: Here high is end-1, not because the following operations (* high) may be performed in the subsequent search process ), or an equivalent operation should access the last element at this time. You must note that the array cannot be accessed out of bounds! */Type * low = begin, * high = end-1; while (low <= high) {// calculate the intermediate element Type * mid = low + (high-low) /2; // if the value of the intermediate element is = the value to be searched, if (* mid = searchValue) return mid; // if the number to be searched is greater than that of the intermediate element, search for else if (searchValue> * mid) low = mid + 1 in the second half of the array; // if the number to be searched is smaller than the intermediate element, search for else high = mid-1;} return end;} in the first half of the array ;} template <typename Type> Type * binarySearch (Type * array, int length, const Type & searchValue) throw (std: range_error) {return binarySearch (array, array + length, searchValue );}

Introduction to recursion

Recursion is recursion... (self-called). recursion is God, and iteration is human;

 

Comparison between Recursion and non-recursion

// Recursively solve the Fibonacci sequence unsigned long ficonacciRecursion (int n) {if (n = 1 | n = 2) return 1; else return ficonacciRecursion (n-1) + ficonacciRecursion (n-2 );}
// Non-recursive solution for the Fibonacci sequence unsigned long ficonacciLoop (int n) {if (n = 1 | n = 2) return 1; unsigned long first = 1, second = 1; unsigned long ans = first + second; for (int I = 3; I <= n; ++ I) {ans = first + second; first = second; second = ans;} return ans ;}

Recursive Binary Search

The algorithm IDEA is like iterative binary search;

// Implement template <typename Type> Type * binarySearchByRecursion (Type * front, Type * last, const Type & searchValue) throw (std: range_error) {if (front = NULL) | (last = NULL) throw std: range_error ("pointer unavailable"); if (front <= last) {Type * mid = front + (last-front)/2; if (* mid = searchValue) return mid; else if (searchValue> * mid) return binarySearchByRecursion (mid + 1, last, searchValue); else return binarySearchByRecursion (front, mid-1, searchValue);} return NULL ;} template <typename Type> int binarySearchByRecursion (Type * array, int left, int right, const Type & searchValue) throw (std: range_error) {if (array = NULL) throw std: range_error ("pointer unavailable"); if (left <= right) {int mid = left + (right-left)/2; if (array [mid] = searchValue) return mid; else if (searchValue <array [mid]) return binarySearchByRecursion (array, left, mid-1, searchValue ); else return binarySearchByRecursion (array, mid + 1, right, searchValue);} return-1 ;}

Summary:

In fact, C ++ STL has implemented std: binary_search (). We only need to call it when using it, but the idea of the binary algorithm is very important, when solving some complicated problems, we can often see the second point.

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.