首页 > 代码库 > 旋转数组的最小数字

旋转数组的最小数字

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


}