zoukankan      html  css  js  c++  java
  • 旋转数组的最小数字

    自己补充的整个程序,包含排序,旋转,查找
    #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;
    
    
    }


  • 相关阅读:
    模拟hadoop-rpc通信
    IOUtils方式上传下载文件
    HDFS基本操作的API
    HDFS基本命令行操作及上传文件的简单API
    gcj_2016_Round1_B
    hiho_1070_RMQ
    hiho_1068_RMQ_st算法
    hiho_1067_最近公共祖先2
    hiho_1062_最近公共祖先
    hiho_1066_并查集
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4343698.html
Copyright © 2011-2022 走看看