旋轉數組的最小數字

來源:互聯網
上載者:User

標籤:c++   程式設計語言   

自己補充的整個程式,包括排序,旋轉,尋找
#include <iostream>using namespace std;void my_sort(int a[],int len){int temp;for (int i=0;i<len-1;i++){for (int j=0;j<len-1-i;j++){if (a[j]>a[j+1]){temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}}}void my_reverse(int a[],int m ,int len){int temp;while(m>0){temp = a[len-1];for (int i=len-2;i>=0;i--){a[i+1] = a[i]; }a[0] = temp;m--;}}void my_print(int a[],int len){for (int i=0;i<len;i++){cout<<a[i]<<" ";}cout<<endl;}int my_find(int a[],int low,int high){int min = a[low];for(int i=low+1;i<=high;i++){if (a[i]<min){min = a[i];}}return min;}int my_min(int a[],int len){int low = 0;int high = len-1;int mid = low;while(a[low]>=a[high]){if (high-low == 1){return a[high];}mid = (low+high)/2;if(a[low]==a[mid]&&a[mid]==a[high])return my_find(a,low,high);if (a[mid]>=a[low]){low = mid;}else if (a[mid]<=a[high]){high = mid;}}if(a[low]<a[high]){return a[low];}}int main(){int a[] = {1,1,1,1,0,1};int len = sizeof(a)/sizeof(a[0]);my_sort(a,len);my_print(a,len);cout<<"input the data for reverse:"<<endl;int m;cin>>m;my_reverse(a,m,len);my_print(a,len);int min = my_min(a,len);cout<<"min= "<<min<<endl;return 0;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.