KT algorithm (2) -- find the specified element in a circular ordered array; kt Algorithm

Source: Internet
Author: User

KT algorithm (2) -- find the specified element in a circular ordered array; kt Algorithm
Problem description

An array in a circular order is an array such as "12, 16, 18, 20, 41,100, 6, 9.

Problem Analysis

A simple definition of a circular ordered array is as follows:

A circular ordered array divides an ordered array into two segments and obtains the reference block content by switching its position.

For example, split 41,100, 41,100, and at the positions 9 and 12 to get two segments, then, the position of the two segments is exchanged to obtain the starting cyclic ordered array.

Another strict definition is:

For a cyclically ordered array {A1, A2 ,...... An}, there is An I that satisfies 1 <I <n, so that {A1, A2 ,...... Ai} and {Ai, Ai + 1 ,...... An} is the same as monotonic not decreasing, or monotonic not increasing arrays. And {A1, A2 ,...... Any element of Ai} Evergrande and equals or Heng are less than or equal to {Ai, Ai + 1 ,...... Any element in.

Algorithm Design

For such an array with a certain monotonicity, the O (n) algorithm that is the easiest to think of is obviously not the best result. Taking into account the search algorithm of ordered series, the time complexity of O (log n) can be realized through the binary method. Therefore, we try to modify the binary method to implement O (log n) to find the specified Element Algorithm.

Theoretical Basis

The essence of the dichotomy is that only half of the series needs to be searched each time, and it is easy to determine which half of the series needs to be searched.

In this example, we can still search for only half of the series at a time. However, it is not as obvious as the traditional binary method to select which half of the series needs to be searched.

However, the cyclic ordered array has two excellent properties:

1. Split a cyclically ordered array into two parts to get an ordered array and another cyclically ordered array.
2. A circular ordered array with a length not greater than 2 is actually an ordered array.

I have no strict mathematical proof of the above two points. Readers can take the initial cyclic ordered array as an example or re-give an example to verify it on their own.

These two properties provide a theoretical basis for the binary search of a cyclic ordered array: for a given element to be searched, the original array is split into two parts, because it is difficult to determine the cyclic ordered array by code, we only need to determine which array is an ordered array and whether to search for it.

Solutions

First, we need to determine whether the array is increased or reduced. That is to say, if we use the previous definition to determine whether the original array of this circular ordered array is monotonous or not increasing.

This is not difficult. You only need to compare the first element and the last element of the array. That is to say, if A1> An, A must be An incremental cyclic ordered array. If A1 <An, A must be An incremental cyclic ordered array. A1 = An is a very troublesome situation. We can compare A2 and An-1 to get the result. I didn't consider it here.

Once we determine whether the cyclic ordered array is incremental or reduced, we need to determine which one is ordered on the left and the other half on the right. Here we use the example of adding a type to reduce the type.

If A [middle]> = A [begin], the left side must be ordered. If the left side is cyclically ordered, the maximum point must appear on the left side, and the constant number on the left side of the maximum point is greater than the number on the right side of the maximum point. This is in conflict with A [middle]> A [begin. And vice versa.

After determining the ordered side, you need to determine whether the search is performed on this side of the facade. This judgment is very simple, as long as you determine whether the value of the number to be searched is between the two endpoint values of the ordered series.

Finally, through the loop, we can find the location of the number to be searched, similar to the binary method.

Code Implementation
Int main (int argc, const char * argv []) {// int a [] = {41,100 }; int a [] = {,}; int target = 20; int arrayLength = sizeof (a)/sizeof (a [0])-1; int index = getIndex (a, target, arrayLength); index =-1? Printf ("not found"): printf ("index = % d", index); return 0;} int getIndex (int a [], int target, int arrayLength) {int beginPos = 0; int endPos = arrayLength; if (a [beginPos]> = a [endPos]) {while (beginPos <= endPos) {int middlePos = beginPos + (endPos-beginPos)/2; int middleValue = a [middlePos]; // indicates that this is an added cyclic ordered array if (middleValue> = a [beginPos]) {// monotonically incrementing if (target = a [middlePos]) {return middlePos;} else if (target <a [middlePos] & target> = a [beginPos]) {// always search for endPos = middlePos-1 ;} else {// find beginPos = middlePos + 1 ;}} else {// monotonically incrementing on the right side. Similarly, if (target = a [middlePos]) {return middlePos ;} else if (target> a [middlePos] & target <= a [endPos]) {// you must find beginPos = middlePos + 1 on the right ;} else {// search for endPos = middlePos-1 ;}}// the element return-1 was not found;} else {while (beginPos <= endPos) {int middlePos = beginPos + (endPos-beginPos)/2; int middleValue = a [middlePos]; // It indicates that this is a reduced cyclic ordered array if (middleValue> = a [beginPos]) {// monotonically decreasing if (target = a [middlePos]) {return middlePos;} else if (target <a [middlePos] & target> = a [endPos]) {// you must find beginPos = middlePos + 1 on the right ;} else {// search for endPos = middlePos-1;} else {// monotonically decreasing on the left side. Similarly, if (target = a [middlePos]) {return middlePos ;} else if (target <= a [beginPos] & target> a [middlePos]) {// you must find endPos = middlePos-1 on the left ;} else {// find beginPos = middlePos + 1 ;}}/// the element return-1 was not found ;}}
Conclusion

When A0 = An occurs, we continue to judge the sizes of A1 and An-1. Therefore, it can be implemented through loops. Therefore, in the worst case, this algorithm may still become a linear algorithm. Of course, it would be much simpler if the same element does not appear in the cyclic ordered array.

Since only a few pieces of data are tested, code 100% is not guaranteed to be correct. You are welcome to test the above Code and tell me possible bugs. Thank you.

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.