Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the minimum number of rotation Array in C++

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the C++ how to achieve the minimum number of rotation array related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this C++ article on how to achieve the minimum number of rotation array will have a harvest, let's take a look.

Topic: move the first elements of an array to the end of the array, which is called the rotation of the array.

Input: a rotation of an incrementally sorted array

Output: rotates the smallest element of the array.

For example, the array {3pm 4pm 5je 1je 2} is a rotation of {1pm 2pm 3pm 4pm 5}, and the minimum value of this array is 1.

Analysis:

1) the rotated array can be divided into two sorted subarrays, and the elements of the front subarray are greater than those of the subsequent subarray.

2) the smallest element is the dividing line between the two word arrays. In sorted arrays, you can use binary lookup (Ologn)

3) in a special case, when the two pointers point to the same number and the numbers among them are the same, it is impossible to determine whether the middle number is in the front subarray or the latter subarray, that is, it is impossible to move two pointers to narrow the search. A sequential search should be used at this time.

Int Min (int* numbers,int length) {if (numbers==nullptr | | length= numbers [index2]) {

If (index2-idnex1==1) / / when two pointers are adjacent to each other, pointer 2 is the minimum {

IndexMid=index2; break;} indexMid= (index1+index2) / 2; / / if the table below shows that index1/index2/indexMid points to the same number, look for if (numbers [index1] = = numbers [indexMid] & & numbers [indexMid] = numbers [index2]) return MinInOrder (numbers,index1,index2); if (numbers [indexMid] > = numbers [index1]) index1=indexMid Else if (numbers [idnexMid]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report