数据结构——递归法求解最大值和最小值

【递归法求解最大值和最小值】
问题描述:若一个无序的线性表A[MaxSize]采用顺序存储方式,元素类型为整型数。试写出递归算法求出A中的最大元素和最小元素。
要求: 顺序表的数据通过调用算法initRandomize()随机产生。

#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define TRUE 1
#define FALSE 0
#define  MAXSIZE       100

typedef  struct Stack {
	int    data[MAXSIZE];	    // 栈
	int         top;            // 栈顶指针(实际是数组的下标值)
} SqStack;

int InitStack(SqStack &S)
{
	S.top=0;
}

int Push(SqStack &S,int e)
{
	if(S.top>=MAXSIZE) return ERROR;
	S.data[S.top++]=e;
	return OK;
}


int Pop(SqStack &S,int &e)
{
	if(S.top == 0)  return ERROR;
	e=S.data[--S.top];//e=*(S.data+S.top-1)
	return OK;
}

int GetHead(SqStack &S,int &e)
{
	if(S.top == 0)  return ERROR;
	e=S.data[S.top-1];//e=*(S.data+S.top-1)
	return OK;
}

int Empty(SqStack &S)
{
	if(S.top == 0)  return 1;
	else return 0;
 } 

int Full(SqStack S)
{
	if(S.top == MAXSIZE)  return 1;
	else return 0;
}

#include <time.h>
void initRandomize(int *arr, int n, int min, int max)
{
    int i = 0;
    srand(time(0));  			/*设置种子,并生成伪随机序列*/
    for (i = 0; i < n; ++i) {
        arr[i] = rand()% (max - min + 1) + min;  /*得到从[min, max]之间的随机数*/
       //arr[i] =rand();
        //printf("%d ", arr[i]);
    }
    //printf("\n");
}

int bubble_max(SqStack &S)//这里用引用会导致真实S顺序变化 
{
	int i,j,temp;
	for(i=1;i<=S.top-1;i++)//外层遍历元素长度数-1 
		for(j=0;j<S.top-i;j++)//内层遍历数从n-1、n-2......2、1(共n-1次,次数有外层控制) 
		{
			if(S.data[j]>S.data[j+1])
			{
				temp=S.data[j];
				S.data[j]=S.data[j+1];
				S.data[j+1]=temp; 
			}
		}
}

int bubble_min(SqStack &S)//这里不用引用会导致真实S顺序不改变 
{
	int i,j,temp;
	for(i=1;i<=S.top-1;i++)//外层遍历元素长度数-1 
		for(j=0;j<S.top-i;j++)//内层遍历数从n-1、n-2......2、1(共n-1次,次数有外层控制) 
		{
			if(S.data[j]<S.data[j+1])
			{
				temp=S.data[j];
				S.data[j]=S.data[j+1];
				S.data[j+1]=temp; 
			}
		}
}
int recursion_max(SqStack &S,int i,int max)
{
	if(i==S.top) return max;
	if(S.data[i]>max)
	{
		max=S.data[i];
		return recursion_max(S,++i,max);
	}
	return recursion_max(S,++i,max);
}


int recursion_min(SqStack &S,int i,int min)
{
	if(i==S.top) return min;
	if(S.data[i]<min)
	{
		min=S.data[i];
		return recursion_min(S,++i,min);
	}
	return recursion_min(S,++i,min);
}


void test_bubble(SqStack S,int a[])
{
	//Sleep(1000);
	//initRandomize(&a[0], 11, 1, 1002);
	int e;
	for(int i=0;i<10;i++)
	{
		Push(S,a[i]);
	}
	bubble_min(S);
	GetHead(S,e);
	printf("最小值是%d \n",e);
	bubble_max(S);
	GetHead(S,e);
	printf("最大值是%d \n",e);
}

void test_recursion(SqStack S,int a[]) 
{
	int e;
	for(int i=0;i<10;i++)
	{
		Push(S,a[i]);
	}
		printf("最大值是%d \n",recursion_max(S,1,S.data[0]));
		printf("最小值是%d \n",recursion_min(S,1,S.data[0]));
	while(!Empty(S))
	{
		Pop(S,e);
		printf("%d ",e);
	}
	printf("\n");
}


int main()
{
	SqStack S;
	int a[11],i,e;	
	initRandomize(&a[0], 10, 1, 1002);	
	InitStack(S);
	for(i=0;i<10;i++)
	{
		Push(S,a[i]);
	}	
	while(!Empty(S))
	{
		Pop(S,e);
		printf("%d ",e);
	}
	printf("\n");
	//test_bubble(S,a);
    test_recursion(S,a) ;
}

猜你喜欢

转载自blog.csdn.net/qq_41856733/article/details/84197839
今日推荐