"C language" Enter a rotation of an incrementally sorted array, outputting the smallest element in the rotated array

Source: Internet
Author: User
Tags assert

The smallest number//title of the rotated array: Moves the first number of elements of an array to the end of the array, which we call the rotation of the array. Enter a rotation of an incrementally sorted array, outputting the smallest element in the rotated array. For example: the array {3,4,5,1,2} is a rotation of {1,2,3,4,5}, and the minimum element is 1. #include <stdio.h> #include <assert.h>int min_equ (int *src, int left, int. right) {int i = 0;int ret = Src[left]; ASSERT (src! = NULL); for (; I < right; ++i) {if (ret >src[i]) return src[i];} return ret;} int find_a_num (int *src, int len) {int left = 0;int right = Len-1;int mid = Left;assert (src! = NULL); while (Src[left] = Src[right]) {if (Right-left = = 1) {mid = Right;break;} Mid = (left + right)/2;if (Src[left] = [Src[right] && src[mid] = = Src[right]) {return min_equ (src, left, right);} if (Src[mid]>src[left]) {left = mid;} if (Src[mid]<src[right]) {right = mid;}} return src[mid];} int main () {int src[] = {3, 4, 5, 1, 2};int dst[] = {1, 0, 1, 1, 1};int len = sizeof (SRC)/sizeof (src[0]); int len1 = s Izeof (DST)/sizeof (dst[0]);p rintf ("%d\n", Find_a_num (SRC, len));p rintf ("%d\n", Find_a_num (DST, len1)); return 0;}







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

"C language" Enter a rotation of an incrementally sorted array, outputting the smallest element in the rotated array

Related Article

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.